Skip to content

Commit

Permalink
get_item_nameid method for public mixin. Return yarl.URL from url…
Browse files Browse the repository at this point in the history
… `ItemDescription` properties. v0.5.4
  • Loading branch information
somespecialone committed Jul 19, 2024
1 parent c8e020f commit 5629dbe
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
15 changes: 15 additions & 0 deletions aiosteampy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@
from .exceptions import ApiError, LoginError, ConfirmationError, SessionExpired
from .constants import Game, STEAM_URL, Currency, Language, TradeOfferStatus, MarketListingStatus
from .client import SteamClient, SteamPublicClient
from .models import (
MarketListing,
EconItem,
Confirmation,
MarketListingItem,
MyMarketListing,
BuyOrder,
MarketHistoryListing,
MarketHistoryListingItem,
MarketHistoryEvent,
TradeOfferItem,
TradeOffer,
HistoryTradeOffer,
HistoryTradeOfferItem,
)
14 changes: 10 additions & 4 deletions aiosteampy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Literal, TypeAlias
from datetime import datetime

from yarl import URL

from .constants import (
STEAM_URL,
GameType,
Expand Down Expand Up @@ -103,12 +105,16 @@ def ident_code(self) -> str:
return self.id

@property
def icon_url(self) -> str:
return str(STEAM_URL.STATIC / f"economy/image/{self.icon}/96fx96f")
def icon_url(self) -> URL:
return STEAM_URL.STATIC / f"economy/image/{self.icon}/96fx96f"

@property
def icon_large_url(self) -> URL | None:
return STEAM_URL.STATIC / f"economy/image/{self.icon_large}/330x192" if self.icon_large else None

@property
def icon_large_url(self) -> str | None:
return str(STEAM_URL.STATIC / f"economy/image/{self.icon_large}/330x192") if self.icon_large else None
def market_url(self) -> URL:
return STEAM_URL.MARKET / f"listings/{self.game[0]}/{self.market_hash_name}"

def __eq__(self, other: "ItemDescription"):
return self.id == other.id
Expand Down
31 changes: 30 additions & 1 deletion aiosteampy/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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
from .utils import create_ident_code, find_item_nameid_in_text

if TYPE_CHECKING:
from .client import SteamPublicClient
Expand Down Expand Up @@ -463,3 +463,32 @@ def _parse_items_for_listings(
unowned_context_id=int(a_data["unowned_contextid"]),
**item_descrs_map[create_ident_code(a_data["classid"], app_id)],
)

@overload
async def get_item_nameid(self: "SteamPublicClient", obj: ItemDescription | EconItem) -> int:
...

@overload
async def get_item_nameid(self: "SteamPublicClient", obj: str, app_id: int) -> int:
...

async def get_item_nameid(
self: "SteamPublicClient",
obj: str | ItemDescription | EconItem,
app_id: int = None,
) -> int:
"""
Get `item_name_id` from item Steam Community Market page.
.. seealso:: https://github.com/somespecialone/steam-item-name-ids
"""

if isinstance(obj, ITEM_DESCR_TUPLE):
url = obj.market_url
else: # str
url = STEAM_URL.MARKET / f"listings/{app_id}/{obj}"

res = await self.session.get(url)
text = await res.text()

return find_item_nameid_in_text(text)
13 changes: 10 additions & 3 deletions aiosteampy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from http.cookies import SimpleCookie, Morsel
from math import floor
from secrets import token_hex
from re import search as re_search
from re import search as re_search, compile as re_compile
from json import loads as j_loads

from aiohttp import ClientSession, ClientResponse
Expand Down Expand Up @@ -43,6 +43,7 @@
"receive_to_buyer_pays",
"generate_session_id",
"decode_jwt",
"find_item_nameid_in_text",
)


Expand Down Expand Up @@ -419,5 +420,11 @@ def decode_jwt(token: str) -> JWTToken:
return j_loads(b64decode(parts[1] + "==", altchars="-_"))


def patch_session_with_proxy(session: ClientSession, proxy: str):
pass
_ITEM_NAMEID_RE = re_compile(r"Market_LoadOrderSpread\(\s?(?P<nameid>\d+)\s?\)")


def find_item_nameid_in_text(text: str) -> int | None:
"""Find and return`item_nameid` in HTML text response from Steam Community Market page"""

res = _ITEM_NAMEID_RE.search(text)
return int(res["nameid"]) if res is not None else res
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiosteampy"
version = "0.5.3"
version = "0.5.4"
description = "Trade and interact with steam market, webapi, guard"
license = "MIT"
authors = ["Dmytro Tkachenko <[email protected]>"]
Expand Down

0 comments on commit 5629dbe

Please sign in to comment.