dankmemer.py 0.1.0a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dankmemer_py-0.1.0a0/LICENSE +21 -0
- dankmemer_py-0.1.0a0/MANIFEST.in +3 -0
- dankmemer_py-0.1.0a0/PKG-INFO +20 -0
- dankmemer_py-0.1.0a0/README.rst +83 -0
- dankmemer_py-0.1.0a0/dankmemer/__init__.py +7 -0
- dankmemer_py-0.1.0a0/dankmemer/client.py +100 -0
- dankmemer_py-0.1.0a0/dankmemer/routes/items.py +282 -0
- dankmemer_py-0.1.0a0/dankmemer/utils.py +15 -0
- dankmemer_py-0.1.0a0/dankmemer.py.egg-info/PKG-INFO +20 -0
- dankmemer_py-0.1.0a0/dankmemer.py.egg-info/SOURCES.txt +15 -0
- dankmemer_py-0.1.0a0/dankmemer.py.egg-info/dependency_links.txt +1 -0
- dankmemer_py-0.1.0a0/dankmemer.py.egg-info/requires.txt +6 -0
- dankmemer_py-0.1.0a0/dankmemer.py.egg-info/top_level.txt +1 -0
- dankmemer_py-0.1.0a0/pyproject.toml +6 -0
- dankmemer_py-0.1.0a0/setup.cfg +31 -0
- dankmemer_py-0.1.0a0/setup.py +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rayyan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dankmemer.py
|
|
3
|
+
Version: 0.1.0a0
|
|
4
|
+
Summary: A lightweight async wrapper for the DankAlert API.
|
|
5
|
+
Home-page: https://github.com/RayyanW786/dankmemer.py
|
|
6
|
+
Author: RayyanW786
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: aiohttp>=3.7.4
|
|
16
|
+
Requires-Dist: rapidfuzz>=1.6.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
19
|
+
Requires-Dist: pytest-asyncio>=0.14.0; extra == "dev"
|
|
20
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
dankmemer.py
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
**Alpha Release Notice:**
|
|
5
|
+
-------------------------
|
|
6
|
+
|
|
7
|
+
**dankmemer.py** is currently in alpha. At this stage, only the items route is implemented. Future releases will include additional routes and enhanced features.
|
|
8
|
+
|
|
9
|
+
**dankmemer.py** is a simple and powerful asynchronous Python wrapper for the `DankAlert API <https://api.dankalert.xyz>`_ — giving you easy access to Dank Memer-related data like items, NPCs, skills, tools, and more.
|
|
10
|
+
|
|
11
|
+
----
|
|
12
|
+
|
|
13
|
+
🚀 Features
|
|
14
|
+
-----------
|
|
15
|
+
|
|
16
|
+
- Built-in caching with configurable TTL
|
|
17
|
+
- Powerful filtering with support for exact, fuzzy, and numeric range queries
|
|
18
|
+
|
|
19
|
+
----
|
|
20
|
+
|
|
21
|
+
📦 Installation
|
|
22
|
+
---------------
|
|
23
|
+
|
|
24
|
+
You can install the project using any of the following aliases:
|
|
25
|
+
|
|
26
|
+
.. code-block:: bash
|
|
27
|
+
|
|
28
|
+
pip install dankmemer
|
|
29
|
+
pip install dankmemer.py
|
|
30
|
+
----
|
|
31
|
+
|
|
32
|
+
Items Route
|
|
33
|
+
-----------
|
|
34
|
+
|
|
35
|
+
The Items Route provides access to the ``/items`` endpoint from the DankAlert API. It automatically caches responses for a configurable period, converts raw JSON into ``Item`` objects, and allows easy filtering via the ``ItemsFilter`` class and the ``Fuzzy`` helper.
|
|
36
|
+
|
|
37
|
+
Basic Example
|
|
38
|
+
~~~~~~~~~~~~~
|
|
39
|
+
|
|
40
|
+
.. code-block:: python
|
|
41
|
+
|
|
42
|
+
import asyncio
|
|
43
|
+
from dankmemer import DankMemerClient, ItemsFilter, Fuzzy
|
|
44
|
+
|
|
45
|
+
async def main():
|
|
46
|
+
async with DankMemerClient() as client:
|
|
47
|
+
# Retrieve all items without filtering.
|
|
48
|
+
all_items = await client.items.query()
|
|
49
|
+
print("All items:", all_items)
|
|
50
|
+
|
|
51
|
+
# Example: Filtering items using various criteria:
|
|
52
|
+
# - Fuzzy matching on the 'name' field (e.g. items with names similar to "trash")
|
|
53
|
+
# - Boolean filtering for 'hasUse' flag
|
|
54
|
+
# - Numeric range filtering for 'marketValue'
|
|
55
|
+
filter_obj = ItemsFilter(
|
|
56
|
+
name=Fuzzy("trash", cutoff=80), # Fuzzy match on name with a cutoff of 80%
|
|
57
|
+
hasUse=False, # Only items that are not usable
|
|
58
|
+
marketValue=(5000, 10000000) # Market value between 5,000 and 10,000,000
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
filtered_items = await client.items.query(filter_obj)
|
|
62
|
+
print("Filtered items:", filtered_items)
|
|
63
|
+
|
|
64
|
+
asyncio.run(main())
|
|
65
|
+
|
|
66
|
+
This basic example demonstrates:
|
|
67
|
+
|
|
68
|
+
- **Exact vs. Fuzzy Matching:** Use a plain string for exact matches (e.g. ``name="Trash"``) or wrap your string with the ``Fuzzy`` helper (e.g. ``name=Fuzzy("trash", cutoff=80)``) for fuzzy matching.
|
|
69
|
+
- **Numeric Range Filtering:** Supply a tuple ``(min, max)`` to filter numeric fields such as ``marketValue``.
|
|
70
|
+
- **Boolean Filtering:** Directly pass boolean values (e.g. ``hasUse=True``).
|
|
71
|
+
|
|
72
|
+
----
|
|
73
|
+
|
|
74
|
+
Documentation
|
|
75
|
+
-------------
|
|
76
|
+
|
|
77
|
+
Full documentation for dankmemer.py can be found at:
|
|
78
|
+
|
|
79
|
+
https://dankmemer-py.readthedocs.io
|
|
80
|
+
|
|
81
|
+
----
|
|
82
|
+
|
|
83
|
+
Feel free to test, report issues, and contribute to this alpha release!
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
from datetime import timedelta
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
|
|
8
|
+
from .routes import items
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DankMemerClient:
|
|
12
|
+
"""
|
|
13
|
+
An asynchronous client for accessing the DankAlert API.
|
|
14
|
+
|
|
15
|
+
This client manages and provides access to various API endpoints
|
|
16
|
+
(e.g. items, npcs, skills, tools) along with built-in caching. In addition, it supports
|
|
17
|
+
anti-rate-limit behavior: when 'useAntirateLimit' is enabled (the default), the client
|
|
18
|
+
ensures that no more than 60 requests are made every 60 seconds.
|
|
19
|
+
|
|
20
|
+
Recommended usage:
|
|
21
|
+
As a context manager:
|
|
22
|
+
async with DankMemerClient(cache_ttl_hours=24) as client:
|
|
23
|
+
items = await client.items.query()
|
|
24
|
+
|
|
25
|
+
Without a context manager:
|
|
26
|
+
client = DankMemerClient(cache_ttl_hours=24)
|
|
27
|
+
items = await client.items.query()
|
|
28
|
+
await client.session.close()
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
useAntirateLimit: bool = True,
|
|
35
|
+
base_url="https://api.dankalert.xyz/dank",
|
|
36
|
+
session: Optional[aiohttp.ClientSession] = None,
|
|
37
|
+
cache_ttl_hours: int = 24,
|
|
38
|
+
):
|
|
39
|
+
self.use_anti_ratelimit = useAntirateLimit
|
|
40
|
+
self.base_url = base_url.rstrip("/")
|
|
41
|
+
self.session = session or aiohttp.ClientSession()
|
|
42
|
+
if cache_ttl_hours < 1:
|
|
43
|
+
raise ValueError("cache_ttl_hours must be at least 1 hour")
|
|
44
|
+
self.cache_ttl = timedelta(hours=cache_ttl_hours)
|
|
45
|
+
|
|
46
|
+
# Rate-limiting (current API: 60 requests per 60 seconds)
|
|
47
|
+
self._rate_limit_lock = asyncio.Lock()
|
|
48
|
+
self.max_requests: int = 60
|
|
49
|
+
self.request_period: float = 60.0
|
|
50
|
+
self._request_times: list[float] = []
|
|
51
|
+
|
|
52
|
+
self.items = items.ItemsRoute(self, self.cache_ttl)
|
|
53
|
+
|
|
54
|
+
async def __aenter__(self):
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
async def __aexit__(self, exc_type, exc, tb):
|
|
58
|
+
if self.session:
|
|
59
|
+
await self.session.close()
|
|
60
|
+
|
|
61
|
+
async def request(self, route: str, params: dict = None):
|
|
62
|
+
"""
|
|
63
|
+
Makes an HTTP GET request to the specified route with the given query parameters,
|
|
64
|
+
while enforcing the API's rate limiting if useAntirateLimit is enabled.
|
|
65
|
+
|
|
66
|
+
:param route: The API route to request (appended to the base URL).
|
|
67
|
+
:param params: Optional dictionary of query parameters.
|
|
68
|
+
:return: The parsed JSON response.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
# Rate-limiting check:
|
|
72
|
+
# We perform a single check on the _request_times list and sleep if needed.
|
|
73
|
+
# Because all access to _request_times is serialized using an asyncio lock,
|
|
74
|
+
# and our expected usage pattern doesn't involve huge bursts of concurrent requests,
|
|
75
|
+
# a single check is sufficient to enforce the rate limit.
|
|
76
|
+
# If the system were to experience significant bursts, a while loop could be added
|
|
77
|
+
# to continuously re-check the condition after sleeping. However, given our design,
|
|
78
|
+
# the one-time check and sleep approach provides a simpler and maintainable solution.
|
|
79
|
+
|
|
80
|
+
if self.use_anti_ratelimit:
|
|
81
|
+
async with self._rate_limit_lock:
|
|
82
|
+
now: float = time.monotonic()
|
|
83
|
+
self._request_times = [
|
|
84
|
+
t for t in self._request_times if now - t < self.request_period
|
|
85
|
+
]
|
|
86
|
+
if len(self._request_times) >= self.max_requests:
|
|
87
|
+
wait_time: float = self.request_period - (
|
|
88
|
+
now - self._request_times[0]
|
|
89
|
+
)
|
|
90
|
+
await asyncio.sleep(wait_time)
|
|
91
|
+
now = time.monotonic()
|
|
92
|
+
self._request_times = [
|
|
93
|
+
t for t in self._request_times if now - t < self.request_period
|
|
94
|
+
]
|
|
95
|
+
self._request_times.append(now)
|
|
96
|
+
|
|
97
|
+
url = f"{self.base_url}/{route}"
|
|
98
|
+
async with self.session.get(url, params=params) as response:
|
|
99
|
+
response.raise_for_status()
|
|
100
|
+
return await response.json()
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from datetime import datetime, timedelta, timezone
|
|
4
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
|
|
5
|
+
|
|
6
|
+
from rapidfuzz import fuzz
|
|
7
|
+
|
|
8
|
+
from dankmemer.utils import Fuzzy
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from dankmemer.client import DankMemerClient
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Item:
|
|
16
|
+
"""
|
|
17
|
+
Represents an individual item obtained from the DankAlert API.
|
|
18
|
+
|
|
19
|
+
This immutable class encapsulates all relevant attributes that describe an item,
|
|
20
|
+
such as its identifier, name, details, and various numeric properties. It is designed
|
|
21
|
+
to allow users to interact with item data in a Pythonic way.
|
|
22
|
+
|
|
23
|
+
Attributes:
|
|
24
|
+
id (int): The unique identifier of the item.
|
|
25
|
+
name (str): The name of the item.
|
|
26
|
+
details (str): Additional details or description of the item.
|
|
27
|
+
emoji (str): An emoji representation associated with the item.
|
|
28
|
+
flavor (str): A short flavor text providing context or description for the item.
|
|
29
|
+
hasUse (bool): Indicates whether the item has a usable function.
|
|
30
|
+
imageURL (str): The URL pointing to the item's image.
|
|
31
|
+
itemKey (str): A unique string key for the item (e.g. "trash").
|
|
32
|
+
marketValue (int): The market value of the item.
|
|
33
|
+
netValue (int): The net value of the item.
|
|
34
|
+
rarity (str): The rarity classification of the item (e.g. "Common", "Rare").
|
|
35
|
+
skins (Dict[str, Any]): A dictionary of available skins for the item.
|
|
36
|
+
tags (Dict[str, Any]): A dictionary of tags associated with the item.
|
|
37
|
+
type (str): The type or category of the item (e.g. "Sellable", "Loot Box").
|
|
38
|
+
value (int): The intrinsic value or base price of the item.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
id: int
|
|
42
|
+
name: str
|
|
43
|
+
details: str
|
|
44
|
+
emoji: str
|
|
45
|
+
flavor: str
|
|
46
|
+
hasUse: bool
|
|
47
|
+
imageURL: str
|
|
48
|
+
itemKey: str
|
|
49
|
+
marketValue: int
|
|
50
|
+
netValue: int
|
|
51
|
+
rarity: str
|
|
52
|
+
skins: Dict[str, Any]
|
|
53
|
+
tags: Dict[str, Any]
|
|
54
|
+
type: str
|
|
55
|
+
value: int
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
def from_dict(cls, data: Dict[str, Any]) -> "Item":
|
|
59
|
+
return cls(
|
|
60
|
+
id=data.get("id"),
|
|
61
|
+
name=data.get("name"),
|
|
62
|
+
details=data.get("details"),
|
|
63
|
+
emoji=data.get("emoji"),
|
|
64
|
+
flavor=data.get("flavor"),
|
|
65
|
+
hasUse=data.get("hasUse"),
|
|
66
|
+
imageURL=data.get("imageURL"),
|
|
67
|
+
itemKey=data.get("itemKey"),
|
|
68
|
+
marketValue=data.get("marketValue"),
|
|
69
|
+
netValue=data.get("netValue"),
|
|
70
|
+
rarity=data.get("rarity"),
|
|
71
|
+
skins=data.get("skins", {}),
|
|
72
|
+
tags=data.get("tags", {}),
|
|
73
|
+
type=data.get("type"),
|
|
74
|
+
value=data.get("value"),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class ItemsFilter:
|
|
79
|
+
"""
|
|
80
|
+
Filter for /items data.
|
|
81
|
+
For string fields, supply a raw string for an exact match or wrap the string in Fuzzy
|
|
82
|
+
for fuzzy matching. Numeric fields accept a single number for exact matching or a tuple
|
|
83
|
+
(min, max) for range filtering.
|
|
84
|
+
|
|
85
|
+
Supported filterable attributes:
|
|
86
|
+
- id: Filter by item ID.
|
|
87
|
+
- name: Filter by item name.
|
|
88
|
+
- details: Filter by details.
|
|
89
|
+
- emoji: Filter by emoji.
|
|
90
|
+
- flavor: Filter by flavor.
|
|
91
|
+
- hasUse: Filter by use flag.
|
|
92
|
+
- imageURL: Filter by image URL.
|
|
93
|
+
- itemKey: Filter by item key.
|
|
94
|
+
- marketValue: Filter by market value.
|
|
95
|
+
- netValue: Filter by net value.
|
|
96
|
+
- rarity: Filter by rarity.
|
|
97
|
+
- skins: Filter by skins (exact match).
|
|
98
|
+
- tags: Filter by tags (exact match).
|
|
99
|
+
- type: Filter by type.
|
|
100
|
+
- value: Filter by value.
|
|
101
|
+
- limit: Maximum number of results returned.
|
|
102
|
+
|
|
103
|
+
Examples:
|
|
104
|
+
.. code-block:: python
|
|
105
|
+
|
|
106
|
+
from dankmemer import Fuzzy, ItemsFilter
|
|
107
|
+
|
|
108
|
+
# Fuzzy matching example for the 'name' field: match items with a name similar
|
|
109
|
+
# to "trash" with a cutoff of 80.
|
|
110
|
+
filter_fuzzy = ItemsFilter(name=Fuzzy("trash", cutoff=80))
|
|
111
|
+
|
|
112
|
+
# Exact string matching for the 'name' field.
|
|
113
|
+
filter_exact = ItemsFilter(name="Trash")
|
|
114
|
+
|
|
115
|
+
# Numeric filtering: filtering 'marketValue' to be within a range.
|
|
116
|
+
filter_range = ItemsFilter(marketValue=(5000, 10000000))
|
|
117
|
+
|
|
118
|
+
# Numeric filtering: filtering 'netValue' for an exact match.
|
|
119
|
+
filter_numeric = ItemsFilter(netValue=100)
|
|
120
|
+
|
|
121
|
+
# Boolean filtering: filtering by the 'hasUse' flag.
|
|
122
|
+
filter_bool = ItemsFilter(hasUse=True)
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def __init__(
|
|
126
|
+
self,
|
|
127
|
+
id: Optional[int] = None,
|
|
128
|
+
name: Optional[Union[str, Fuzzy]] = None,
|
|
129
|
+
details: Optional[Union[str, Fuzzy]] = None,
|
|
130
|
+
emoji: Optional[Union[str, Fuzzy]] = None,
|
|
131
|
+
flavor: Optional[Union[str, Fuzzy]] = None,
|
|
132
|
+
hasUse: Optional[bool] = None,
|
|
133
|
+
imageURL: Optional[Union[str, Fuzzy]] = None,
|
|
134
|
+
itemKey: Optional[Union[str, Fuzzy]] = None,
|
|
135
|
+
marketValue: Optional[Union[int, Tuple[int, int]]] = None,
|
|
136
|
+
netValue: Optional[Union[int, Tuple[int, int]]] = None,
|
|
137
|
+
rarity: Optional[Union[str, Fuzzy]] = None,
|
|
138
|
+
skins: Optional[Dict[str, Any]] = None,
|
|
139
|
+
tags: Optional[Dict[str, Any]] = None,
|
|
140
|
+
type: Optional[Union[str, Fuzzy]] = None,
|
|
141
|
+
value: Optional[Union[int, Tuple[int, int]]] = None,
|
|
142
|
+
limit: Optional[int] = None,
|
|
143
|
+
) -> None:
|
|
144
|
+
self.id: Optional[int] = id
|
|
145
|
+
self.name: Optional[Union[str, Fuzzy]] = name
|
|
146
|
+
self.details: Optional[Union[str, Fuzzy]] = details
|
|
147
|
+
self.emoji: Optional[Union[str, Fuzzy]] = emoji
|
|
148
|
+
self.flavor: Optional[Union[str, Fuzzy]] = flavor
|
|
149
|
+
self.hasUse: Optional[bool] = hasUse
|
|
150
|
+
self.imageURL: Optional[Union[str, Fuzzy]] = imageURL
|
|
151
|
+
self.itemKey: Optional[Union[str, Fuzzy]] = itemKey
|
|
152
|
+
self.marketValue: Optional[Union[int, Tuple[int, int]]] = marketValue
|
|
153
|
+
self.netValue: Optional[Union[int, Tuple[int, int]]] = netValue
|
|
154
|
+
self.rarity: Optional[Union[str, Fuzzy]] = rarity
|
|
155
|
+
self.skins: Optional[Dict[str, Any]] = skins
|
|
156
|
+
self.tags: Optional[Dict[str, Any]] = tags
|
|
157
|
+
self.type: Optional[Union[str, Fuzzy]] = type
|
|
158
|
+
self.value: Optional[Union[int, Tuple[int, int]]] = value
|
|
159
|
+
self.limit: Optional[int] = limit
|
|
160
|
+
|
|
161
|
+
def apply(self, data: List[Item]) -> List[Item]:
|
|
162
|
+
results: List[Item] = []
|
|
163
|
+
for item in data:
|
|
164
|
+
if self.id is not None and item.id != self.id:
|
|
165
|
+
continue
|
|
166
|
+
if self.name is not None and not self._matches_field(item.name, self.name):
|
|
167
|
+
continue
|
|
168
|
+
if self.details is not None and not self._matches_field(
|
|
169
|
+
item.details, self.details
|
|
170
|
+
):
|
|
171
|
+
continue
|
|
172
|
+
if self.emoji is not None and not self._matches_field(
|
|
173
|
+
item.emoji, self.emoji
|
|
174
|
+
):
|
|
175
|
+
continue
|
|
176
|
+
if self.flavor is not None and not self._matches_field(
|
|
177
|
+
item.flavor, self.flavor
|
|
178
|
+
):
|
|
179
|
+
continue
|
|
180
|
+
if self.hasUse is not None and item.hasUse != self.hasUse:
|
|
181
|
+
continue
|
|
182
|
+
if self.imageURL is not None and not self._matches_field(
|
|
183
|
+
item.imageURL, self.imageURL
|
|
184
|
+
):
|
|
185
|
+
continue
|
|
186
|
+
if self.itemKey is not None and not self._matches_field(
|
|
187
|
+
item.itemKey, self.itemKey
|
|
188
|
+
):
|
|
189
|
+
continue
|
|
190
|
+
if self.marketValue is not None and not self._matches_numeric(
|
|
191
|
+
item.marketValue, self.marketValue
|
|
192
|
+
):
|
|
193
|
+
continue
|
|
194
|
+
if self.netValue is not None and not self._matches_numeric(
|
|
195
|
+
item.netValue, self.netValue
|
|
196
|
+
):
|
|
197
|
+
continue
|
|
198
|
+
if self.rarity is not None and not self._matches_field(
|
|
199
|
+
item.rarity, self.rarity
|
|
200
|
+
):
|
|
201
|
+
continue
|
|
202
|
+
if self.skins is not None and item.skins != self.skins:
|
|
203
|
+
continue
|
|
204
|
+
if self.tags is not None and item.tags != self.tags:
|
|
205
|
+
continue
|
|
206
|
+
if self.type is not None and not self._matches_field(item.type, self.type):
|
|
207
|
+
continue
|
|
208
|
+
if self.value is not None and not self._matches_numeric(
|
|
209
|
+
item.value, self.value
|
|
210
|
+
):
|
|
211
|
+
continue
|
|
212
|
+
results.append(item)
|
|
213
|
+
if self.limit is not None:
|
|
214
|
+
results = results[: self.limit]
|
|
215
|
+
return results
|
|
216
|
+
|
|
217
|
+
def _matches_field(self, field_value: str, filter_val: Union[str, Fuzzy]) -> bool:
|
|
218
|
+
if not field_value:
|
|
219
|
+
return False
|
|
220
|
+
if isinstance(filter_val, Fuzzy):
|
|
221
|
+
score: float = fuzz.ratio(field_value.lower(), filter_val.value.lower())
|
|
222
|
+
return score >= filter_val.cutoff
|
|
223
|
+
return field_value.lower() == filter_val.lower() # type: ignore
|
|
224
|
+
|
|
225
|
+
def _matches_numeric(
|
|
226
|
+
self, field_value: Union[int, float], filter_val: Union[int, Tuple[int, int]]
|
|
227
|
+
) -> bool:
|
|
228
|
+
if isinstance(filter_val, tuple):
|
|
229
|
+
low, high = filter_val
|
|
230
|
+
return low <= field_value <= high
|
|
231
|
+
return field_value == filter_val
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class ItemsRoute:
|
|
235
|
+
"""
|
|
236
|
+
Represents the /items endpoint, converting raw API data into python objects and
|
|
237
|
+
providing route-specific filtering.
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
def __init__(self, client: "DankMemerClient", cache_ttl: timedelta) -> None:
|
|
241
|
+
self.client: "DankMemerClient" = client
|
|
242
|
+
self.cache_ttl: timedelta = cache_ttl
|
|
243
|
+
self._cache: Optional[Dict[int, Item]] = None
|
|
244
|
+
self._last_update: Optional[datetime] = None
|
|
245
|
+
self._lock: asyncio.Lock = asyncio.Lock()
|
|
246
|
+
|
|
247
|
+
async def _fetch(self) -> Dict[int, Item]:
|
|
248
|
+
raw_data: Dict[str, Any] = await self.client.request("items")
|
|
249
|
+
processed: Dict[int, Item] = {}
|
|
250
|
+
for key, value in raw_data.items():
|
|
251
|
+
try:
|
|
252
|
+
item_id: int = int(key)
|
|
253
|
+
except ValueError:
|
|
254
|
+
continue
|
|
255
|
+
processed[item_id] = Item.from_dict(value)
|
|
256
|
+
self._cache = processed
|
|
257
|
+
self._last_update = datetime.now(timezone.utc)
|
|
258
|
+
return processed
|
|
259
|
+
|
|
260
|
+
async def _get_data(self) -> Dict[int, Item]:
|
|
261
|
+
async with self._lock:
|
|
262
|
+
if (self._cache is None) or (
|
|
263
|
+
datetime.now(timezone.utc) - self._last_update > self.cache_ttl
|
|
264
|
+
):
|
|
265
|
+
return await self._fetch()
|
|
266
|
+
return self._cache
|
|
267
|
+
|
|
268
|
+
async def query(self, item_filter: Optional[ItemsFilter] = None) -> List[Item]:
|
|
269
|
+
"""
|
|
270
|
+
Retrieve the list of Items from the /items endpoint.
|
|
271
|
+
|
|
272
|
+
If no filter is provided, all cached items are returned. Otherwise, the provided
|
|
273
|
+
ItemsFilter is applied and only items matching the filter criteria are returned.
|
|
274
|
+
|
|
275
|
+
:param item_filter: Optional ItemsFilter instance containing filtering criteria.
|
|
276
|
+
:return: A list of Item objects.
|
|
277
|
+
"""
|
|
278
|
+
raw_dict: Dict[int, Item] = await self._get_data()
|
|
279
|
+
items_list: List[Item] = list(raw_dict.values())
|
|
280
|
+
if item_filter is None:
|
|
281
|
+
return items_list
|
|
282
|
+
return item_filter.apply(items_list)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class Fuzzy:
|
|
2
|
+
"""
|
|
3
|
+
Represents a fuzzy match criterion.
|
|
4
|
+
|
|
5
|
+
:param value: The string value to match.
|
|
6
|
+
:param cutoff: The minimum matching ratio (0-100) required for a successful fuzzy match.
|
|
7
|
+
Defaults to 80.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, value: str, cutoff: int = 80) -> None:
|
|
11
|
+
self.value: str = value
|
|
12
|
+
self.cutoff: int = cutoff
|
|
13
|
+
|
|
14
|
+
def __repr__(self) -> str:
|
|
15
|
+
return f"Fuzzy({self.value!r}, cutoff={self.cutoff})"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dankmemer.py
|
|
3
|
+
Version: 0.1.0a0
|
|
4
|
+
Summary: A lightweight async wrapper for the DankAlert API.
|
|
5
|
+
Home-page: https://github.com/RayyanW786/dankmemer.py
|
|
6
|
+
Author: RayyanW786
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: aiohttp>=3.7.4
|
|
16
|
+
Requires-Dist: rapidfuzz>=1.6.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
19
|
+
Requires-Dist: pytest-asyncio>=0.14.0; extra == "dev"
|
|
20
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.rst
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.cfg
|
|
6
|
+
setup.py
|
|
7
|
+
dankmemer/__init__.py
|
|
8
|
+
dankmemer/client.py
|
|
9
|
+
dankmemer/utils.py
|
|
10
|
+
dankmemer.py.egg-info/PKG-INFO
|
|
11
|
+
dankmemer.py.egg-info/SOURCES.txt
|
|
12
|
+
dankmemer.py.egg-info/dependency_links.txt
|
|
13
|
+
dankmemer.py.egg-info/requires.txt
|
|
14
|
+
dankmemer.py.egg-info/top_level.txt
|
|
15
|
+
dankmemer/routes/items.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dankmemer
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = dankmemer.py
|
|
3
|
+
version = attr: dankmemer.__version__
|
|
4
|
+
author = RayyanW786
|
|
5
|
+
description = A lightweight async wrapper for the DankAlert API.
|
|
6
|
+
long_description = file: README.md
|
|
7
|
+
long_description_content_type = text/markdown
|
|
8
|
+
url = https://github.com/RayyanW786/dankmemer.py
|
|
9
|
+
license = MIT
|
|
10
|
+
classifiers =
|
|
11
|
+
Programming Language :: Python :: 3
|
|
12
|
+
Programming Language :: Python :: 3.7
|
|
13
|
+
License :: OSI Approved :: MIT License
|
|
14
|
+
Operating System :: OS Independent
|
|
15
|
+
|
|
16
|
+
[options]
|
|
17
|
+
packages = find:
|
|
18
|
+
python_requires = >=3.7
|
|
19
|
+
install_requires =
|
|
20
|
+
aiohttp>=3.7.4
|
|
21
|
+
rapidfuzz>=1.6.0
|
|
22
|
+
|
|
23
|
+
[options.extras_require]
|
|
24
|
+
dev =
|
|
25
|
+
pytest>=6.0
|
|
26
|
+
pytest-asyncio>=0.14.0
|
|
27
|
+
|
|
28
|
+
[egg_info]
|
|
29
|
+
tag_build =
|
|
30
|
+
tag_date = 0
|
|
31
|
+
|