-
Notifications
You must be signed in to change notification settings - Fork 2
/
public.py
464 lines (409 loc) · 15.8 KB
/
public.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from typing import Callable, TypeAlias, overload, TYPE_CHECKING
from aiohttp import ClientResponseError
from yarl import URL
from .models import (
ItemDescriptionEntry,
ItemTag,
ItemDescription,
EconItem,
ItemAction,
MarketListing,
MarketListingItem,
ITEM_DESCR_TUPLE,
)
from .constants import STEAM_URL, Game, Currency, GameType, Language, T_KWARGS
from .typed import ItemOrdersHistogram, ItemOrdersActivity, PriceOverview
from .exceptions import ApiError
from .utils import create_ident_code
if TYPE_CHECKING:
from .client import SteamPublicClient
__all__ = ("SteamPublicMixin", "INVENTORY_URL")
INV_PAGE_SIZE = 2000 # steam new limit rule
INVENTORY_URL = STEAM_URL.COMMUNITY / "inventory"
PREDICATE: TypeAlias = Callable[[EconItem], bool]
PRIVATE_USER_EXC_MSG = "User inventory is private."
ITEM_MARKET_LISTINGS_DATA: TypeAlias = tuple[list[MarketListing], int]
class SteamPublicMixin:
"""Contains methods that do not require authentication."""
__slots__ = ()
# init method with attr in client
async def get_user_inventory(
self: "SteamPublicClient",
steam_id: int,
game: GameType,
*,
predicate: PREDICATE = None,
page_size=INV_PAGE_SIZE,
**kwargs: T_KWARGS,
) -> list[EconItem]:
"""
Fetches inventory of user.
:param steam_id: steamid64 of user
:param game: just Steam Game
:param page_size: max items on page. Current Steam limit is 2000
:param predicate: callable with single arg `EconItem`, must return bool
:return: list of `EconItem`
:raises ApiError: if response data `success` is False or user inventory is private
"""
inv_url = INVENTORY_URL / f"{steam_id}/"
params = {"l": self.language, "count": page_size, **kwargs}
headers = {"Referer": str(inv_url)}
url = inv_url / f"{game[0]}/{game[1]}"
item_descrs_map = {}
items = []
more_items = True
last_assetid = None
while more_items:
params_pag = {**params, "start_assetid": last_assetid} if last_assetid else params
data = await self._fetch_inventory(url, params_pag, headers)
more_items = data.get("more_items", False)
if more_items:
last_assetid = data.get("last_assetid")
items.extend(self._parse_items(data, steam_id, item_descrs_map))
return [i for i in items if predicate(i)] if predicate else items
async def _fetch_inventory(
self: "SteamPublicClient",
url: URL,
params: dict,
headers: dict,
) -> dict[str, list[dict] | int]:
try:
r = await self.session.get(url, params=params, headers=headers)
except ClientResponseError as e:
raise ApiError(PRIVATE_USER_EXC_MSG, str(url)) if e.status == 403 else e
rj: dict[str, list[dict] | int] = await r.json()
if not rj.get("success"):
raise ApiError(f"Can't fetch inventory.", rj)
return rj
@staticmethod
def _find_game_for_asset(description_data: dict[str, int], assets: list[dict[str, int | str]]) -> GameType:
try:
return Game(description_data["appid"])
except ValueError:
for asset in assets:
if asset["classid"] == description_data["classid"]:
return asset["appid"], int(asset["contextid"])
@classmethod
def _parse_items(
cls,
data: dict[str, list[dict]],
steam_id: int,
item_descrs_map: dict[str, dict],
) -> list[EconItem]:
for d_data in data["descriptions"]:
key = d_data["classid"]
if key not in item_descrs_map:
item_descrs_map[key] = cls._create_item_description_kwargs(d_data, data["assets"])
return [
EconItem(
asset_id=int(asset_data["assetid"]),
owner_id=steam_id,
amount=int(asset_data["amount"]),
**item_descrs_map[asset_data["classid"]],
)
for asset_data in data["assets"]
]
@classmethod
def _create_item_actions(cls, actions: list[dict]) -> tuple[ItemAction, ...]:
return tuple(ItemAction(a_data["link"], a_data["name"]) for a_data in actions)
@classmethod
def _create_item_tags(cls, tags: list[dict]) -> tuple[ItemTag, ...]:
return tuple(
ItemTag(
category=t_data["category"],
internal_name=t_data["internal_name"],
localized_category_name=t_data["localized_category_name"],
localized_tag_name=t_data["localized_tag_name"],
color=t_data.get("color"),
)
for t_data in tags
)
@classmethod
def _create_item_description_entries(cls, descriptions: list[dict]) -> tuple[ItemDescriptionEntry, ...]:
return tuple(
ItemDescriptionEntry(
value=de_data["value"],
color=de_data.get("color"),
)
for de_data in descriptions
if de_data["value"] != " " # ha, surprise!
)
@classmethod
def _create_item_description_kwargs(cls, data: dict, assets: list[dict[str, int | str]]) -> dict:
return dict(
class_id=int(data["classid"]),
instance_id=int(data["instanceid"]),
game=cls._find_game_for_asset(data, assets),
name=data["name"],
market_name=data["market_name"],
market_hash_name=data["market_hash_name"],
name_color=data["name_color"] or None,
background_color=data.get("name_color") or None,
type=data["type"] or None,
icon=data["icon_url"],
icon_large=data.get("icon_url_large"),
commodity=bool(data["commodity"]),
tradable=bool(data["tradable"]),
marketable=bool(data["marketable"]),
market_tradable_restriction=data.get("market_tradable_restriction"),
market_buy_country_restriction=data.get("market_buy_country_restriction"),
market_fee_app=data.get("market_fee_app"),
market_marketable_restriction=data.get("market_marketable_restriction"),
actions=cls._create_item_actions(data.get("actions", ())),
market_actions=cls._create_item_actions(data.get("market_actions", ())),
owner_actions=cls._create_item_actions(data.get("owner_actions", ())),
tags=cls._create_item_tags(data.get("tags", ())),
descriptions=cls._create_item_description_entries(data.get("descriptions", ())),
owner_descriptions=cls._create_item_description_entries(data.get("owner_descriptions", ())),
fraud_warnings=tuple(*data.get("fraudwarnings", ())),
)
async def fetch_item_orders_histogram(
self: "SteamPublicClient",
item_nameid: int,
*,
lang: Language = None,
country: str = None,
currency: Currency = None,
**kwargs: T_KWARGS,
) -> ItemOrdersHistogram:
"""
Do what described in method name.
.. seealso::
* https://github.com/Revadike/InternalSteamWebAPI/wiki/Get-Market-Item-Orders-Histogram
* https://github.com/somespecialone/steam-item-name-ids
.. warning:: This request is rate limited by Steam.
:param item_nameid: special id of item class. Can be found only on listings page.
:param lang:
:param country:
:param currency:
:return: `ItemOrdersHistogram` dict
:raises ApiError:
"""
params = {
"norender": 1,
"language": lang or self.language,
"country": country or self.country,
"currency": currency or self.currency,
"item_nameid": item_nameid,
**kwargs,
}
r = await self.session.get(STEAM_URL.MARKET / "itemordershistogram", params=params)
rj: ItemOrdersHistogram = await r.json()
if not rj.get("success"):
raise ApiError(f"Can't fetch item orders histogram for {item_nameid}.", rj)
return rj
async def fetch_item_orders_activity(
self: "SteamPublicClient",
item_name_id: int,
*,
lang: Language = None,
country: str = None,
currency: Currency = None,
**kwargs: T_KWARGS,
) -> ItemOrdersActivity:
"""
Do what described in method name.
.. seealso::
* https://github.com/Revadike/InternalSteamWebAPI/wiki/Get-Market-Item-Orders-Activity
* https://github.com/somespecialone/steam-item-name-ids
:param item_name_id: special id of item class. Can be found only on listings page.
:param lang:
:param country:
:param currency:
:return: `ItemOrdersActivity` dict
:raises ApiError:
"""
params = {
"norender": 1,
"language": lang or self.language,
"country": country or self.country,
"currency": currency or self.currency,
"item_nameid": item_name_id,
**kwargs,
}
r = await self.session.get(STEAM_URL.MARKET / "itemordersactivity", params=params)
rj: ItemOrdersActivity = await r.json()
if not rj.get("success"):
raise ApiError(f"Can't fetch item orders activity for {item_name_id}.", rj)
return rj
@overload
async def fetch_price_overview(
self,
obj: EconItem | ItemDescription,
*,
country: str = ...,
currency: Currency = ...,
) -> PriceOverview:
...
@overload
async def fetch_price_overview(
self,
obj: str,
app_id: int,
*,
country: str = ...,
currency: Currency = ...,
) -> PriceOverview:
...
async def fetch_price_overview(
self: "SteamPublicClient",
obj: str | EconItem | ItemDescription,
app_id: int = None,
*,
country: str = None,
currency: Currency = None,
**kwargs: T_KWARGS,
) -> PriceOverview:
"""
Fetch price data.
.. warning:: This request is rate limited by Steam.
:param obj:
:param app_id:
:param country:
:param currency:
:return: `PriceOverview` dict
:raises ApiError:
"""
if isinstance(obj, ITEM_DESCR_TUPLE):
name = obj.market_hash_name
app_id = obj.game.app_id
else: # str
name = obj
params = {
"country": country or self.country,
"currency": currency or self.currency,
"market_hash_name": name,
"appid": app_id,
**kwargs,
}
r = await self.session.get(STEAM_URL.MARKET / "priceoverview", params=params)
rj: PriceOverview = await r.json()
if not rj.get("success"):
raise ApiError(f"Can't fetch price overview for `{name}`.", rj)
return rj
@overload
async def get_item_listings(
self,
obj: EconItem | ItemDescription,
*,
country: str = ...,
currency: Currency = ...,
query: str = ...,
start: int = ...,
count: int = ...,
) -> ITEM_MARKET_LISTINGS_DATA:
...
@overload
async def get_item_listings(
self,
obj: str,
app_id: int,
*,
country: str = ...,
currency: Currency = ...,
query: str = ...,
start: int = ...,
count: int = ...,
) -> ITEM_MARKET_LISTINGS_DATA:
...
async def get_item_listings(
self: "SteamPublicClient",
obj: str | EconItem | ItemDescription,
app_id: int = None,
*,
country: str = None,
currency: Currency = None,
lang: str = None,
query="",
start: int = 0,
count: int = 10,
**kwargs: T_KWARGS,
) -> ITEM_MARKET_LISTINGS_DATA:
"""
Fetch item listings from market.
You can paginate by yourself passing `start` arg.
.. warning:: This request is rate limited by Steam.
:param obj: market hash name or `EconItem` or `ItemDescription`
:param app_id:
:param country:
:param currency:
:param lang:
:param count: page size, Steam limit this size to 10 for now
:param start: offset position
:param query: raw search query
:return: list of `MarketListing`, total listings count
:raises ApiError:
"""
if isinstance(obj, ITEM_DESCR_TUPLE):
name = obj.market_hash_name
app_id = obj.game[0]
else: # str
name = obj
base_url = STEAM_URL.MARKET / f"listings/{app_id}/{name}"
params = {
"filter": query,
"country": country or self.country,
"currency": currency or self.currency,
"start": start,
"count": count,
"language": lang or self.language,
**kwargs,
}
r = await self.session.get(base_url / "render/", params=params, headers={"Referer": str(base_url)})
rj: dict[str, int | dict[str, dict]] = await r.json()
if not rj.get("success"):
raise ApiError(f"Can't fetch market listings for `{name}`.", rj)
if not rj["total_count"] or not rj["assets"]:
return [], 0
item_descrs_map = {}
econ_items_map = {}
self._update_item_descrs_map_for_public(rj["assets"], item_descrs_map)
self._parse_items_for_listings(rj["assets"], item_descrs_map, econ_items_map)
return [
MarketListing(
id=int(l_data["listingid"]),
item=econ_items_map[
create_ident_code(
l_data["asset"]["id"],
l_data["asset"]["appid"],
l_data["asset"]["contextid"],
)
],
currency=Currency(int(l_data["currencyid"]) - 2000),
price=int(l_data["price"]),
fee=int(l_data["fee"]),
converted_currency=Currency(int(l_data["converted_currencyid"]) - 2000),
converted_fee=int(l_data["converted_fee"]),
converted_price=int(l_data["converted_price"]),
)
for l_data in rj["listinginfo"].values()
], rj["total_count"]
@classmethod
def _update_item_descrs_map_for_public(
cls,
assets: dict[str, dict[str, dict[str, dict]]],
item_descrs_map: dict[str, dict],
):
for app_id, app_data in assets.items():
for context_id, context_data in app_data.items():
for asset_id, a_data in context_data.items():
key = create_ident_code(a_data["classid"], app_id)
item_descrs_map[key] = cls._create_item_description_kwargs(a_data, [a_data])
@staticmethod
def _parse_items_for_listings(
data: dict[str, dict[str, dict[str, dict]]],
item_descrs_map: dict[str, dict],
econ_items_map: dict[str, MarketListingItem],
):
for app_id, app_data in data.items():
for context_id, context_data in app_data.items():
for a_data in context_data.values():
key = create_ident_code(a_data["id"], app_id, context_id)
if key not in econ_items_map:
econ_items_map[key] = MarketListingItem(
asset_id=int(a_data["id"]),
market_id=0, # market listing post init
unowned_id=int(a_data["unowned_id"]),
unowned_context_id=int(a_data["unowned_contextid"]),
**item_descrs_map[create_ident_code(a_data["classid"], app_id)],
)