Skip to content

Commit bd4b2f0

Browse files
authored
Merge pull request #14 from offish/v2.0.8
more methods
2 parents e8ab76f + 874b51d commit bd4b2f0

File tree

9 files changed

+156
-14
lines changed

9 files changed

+156
-14
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
extend-ignore = E203, E704

src/tf2_utils/__init__.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
__title__ = "tf2-utils"
22
__author__ = "offish"
3-
__version__ = "2.0.7"
3+
__version__ = "2.0.8"
44
__license__ = "MIT"
55

6-
from .sku import get_sku, get_sku_properties, sku_to_defindex
6+
from .sku import (
7+
get_sku,
8+
get_sku_properties,
9+
sku_to_defindex,
10+
sku_to_quality,
11+
sku_is_uncraftable,
12+
)
713
from .item import Item
814
from .offer import Offer
9-
from .utils import to_refined, to_scrap, refinedify, account_id_to_steam_id
15+
from .utils import (
16+
to_refined,
17+
to_scrap,
18+
refinedify,
19+
account_id_to_steam_id,
20+
steam_id_to_account_id,
21+
get_account_id_from_trade_url,
22+
get_steam_id_from_trade_url,
23+
get_token_from_trade_url,
24+
)
1025
from .schema import SchemaItemsUtils
1126
from .sockets import BackpackTFSocket, PricesTFSocket
12-
from .prices_tf import PricesTF
27+
from .prices_tf import (
28+
PricesTF,
29+
PricesTFError,
30+
RateLimited,
31+
EmptyResponse,
32+
InternalServerError,
33+
)
1334
from .inventory import Inventory, map_inventory
35+
36+
# flake8: noqa

src/tf2_utils/inventory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ def fetch(self, steam_id: str, app_id: int = 440, context_id: int = 2) -> dict:
5858

5959
try:
6060
return response.json()
61-
except:
62-
return {}
61+
except Exception as e:
62+
return {"error": str(e)}

src/tf2_utils/prices_tf.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from websockets.sync.client import connect
21
import requests
2+
import time
3+
4+
from .utils import to_refined
35

46

57
class PricesTFError(Exception):
@@ -75,6 +77,55 @@ def get_price(self, sku: str) -> dict:
7577
def get_prices(self, page: int, limit: int = 100, order: str = "DESC") -> dict:
7678
return self.__get("/prices", {"page": page, "limit": limit, "order": order})
7779

80+
def format_price(self, data: dict) -> dict:
81+
return {
82+
"buy": {
83+
"keys": data["buyKeys"],
84+
"metal": to_refined(data["buyHalfScrap"] / 2),
85+
},
86+
"sell": {
87+
"keys": data["sellKeys"],
88+
"metal": to_refined(data["sellHalfScrap"] / 2),
89+
},
90+
}
91+
92+
def get_prices_till_page(
93+
self, page_limit: int, print_rate_limit: bool = False
94+
) -> dict:
95+
prices = {}
96+
current_page = 1
97+
# set higher than current page first time
98+
max_page = page_limit if page_limit != -1 else 2
99+
100+
while current_page < max_page:
101+
try:
102+
response = self.prices_tf.get_prices(current_page)
103+
except RateLimited:
104+
timeout = 60
105+
106+
if print_rate_limit:
107+
print(f"rate limited from prices.tf, waiting {timeout} seconds")
108+
109+
time.sleep(timeout)
110+
continue
111+
112+
if "items" not in response:
113+
raise PricesTFError("could not find any items in response")
114+
115+
for item in response["items"]:
116+
prices[item["sku"]] = self.format_price(item)
117+
118+
current_page = response["meta"]["currentPage"] + 1
119+
total_pages = response["meta"]["totalPages"]
120+
121+
if page_limit == -1:
122+
max_page = total_pages
123+
124+
return prices
125+
126+
def get_all_prices(self, print_rate_limit: bool = False) -> dict:
127+
return self.get_prices_till_page(-1, print_rate_limit)
128+
78129
def update_price(self, sku: str) -> tuple[dict, int]:
79130
return self.__post(f"/prices/{sku}/refresh")
80131

src/tf2_utils/providers/steamapis.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def __init__(self, api_key: str) -> None:
55
def get_url_and_params(
66
self, steam_id: str, app_id: int, context_id: int
77
) -> tuple[str, dict]:
8+
url = "https://api.steamapis.com/steam/inventory/{}/{}/{}"
89
return (
9-
f"https://api.steamapis.com/steam/inventory/{steam_id}/{app_id}/{context_id}",
10+
url.format(steam_id, app_id, context_id),
1011
{"api_key": self.api_key},
1112
)

src/tf2_utils/sku.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@ def get_sku_properties(item: Item | dict) -> dict:
4646
return sku_properties
4747

4848

49+
def get_property(sku: str, index: int) -> str:
50+
return sku.split(";")[index]
51+
52+
4953
def sku_to_defindex(sku: str) -> int:
50-
return int(sku.split(";")[:-1][0])
54+
return int(get_property(sku, 0))
55+
56+
57+
def sku_to_quality(sku: str) -> int:
58+
return int(get_property(sku, 1))
59+
60+
61+
def sku_is_uncraftable(sku: str) -> bool:
62+
return sku.find(";uncraftable") != -1
5163

5264

5365
def get_sku(item: Item | dict) -> str:

src/tf2_utils/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,24 @@ def refinedify(value: float) -> float:
2828
return math.floor((round(value * 9, 0) * 100) / 9) / 100
2929

3030

31+
def get_account_id_from_trade_url(trade_url: str) -> str:
32+
partner_index = trade_url.index("?partner=") + 9
33+
token_index = trade_url.index("&token=")
34+
return trade_url[partner_index:token_index]
35+
36+
37+
def get_steam_id_from_trade_url(trade_url: str) -> str:
38+
return account_id_to_steam_id(get_account_id_from_trade_url(trade_url))
39+
40+
41+
def get_token_from_trade_url(trade_url: str) -> str:
42+
token_index = trade_url.index("&token=") + 7
43+
return trade_url[token_index:]
44+
45+
3146
def account_id_to_steam_id(account_id: int | str) -> str:
3247
return str(76561197960265728 + int(account_id))
48+
49+
50+
def steam_id_to_account_id(steam_id: str | int) -> str:
51+
return str(int(steam_id) - 76561197960265728)

tests/test_sku.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from src.tf2_utils.utils import read_json_file
2-
from src.tf2_utils import get_sku, get_sku_properties
2+
from src.tf2_utils import (
3+
get_sku,
4+
get_sku_properties,
5+
sku_to_defindex,
6+
sku_to_quality,
7+
sku_is_uncraftable,
8+
)
39

410
from unittest import TestCase
511

@@ -57,3 +63,10 @@ def test_uncraftable_hat(self):
5763

5864
# https://marketplace.tf/items/tf2/734;6;uncraftable
5965
self.assertEqual("734;6;uncraftable", sku)
66+
67+
def test_properties(self):
68+
sku = "734;6;uncraftable"
69+
70+
self.assertEqual(734, sku_to_defindex(sku))
71+
self.assertEqual(6, sku_to_quality(sku))
72+
self.assertEqual(True, sku_is_uncraftable(sku))

tests/test_utils.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
from src.tf2_utils import account_id_to_steam_id, to_scrap, to_refined, refinedify
1+
from src.tf2_utils import (
2+
account_id_to_steam_id,
3+
steam_id_to_account_id,
4+
to_scrap,
5+
to_refined,
6+
refinedify,
7+
get_account_id_from_trade_url,
8+
get_steam_id_from_trade_url,
9+
get_token_from_trade_url,
10+
)
211

312
from unittest import TestCase
413

14+
STEAM_ID = "76561198253325712"
15+
ACCOUNT_ID = "293059984"
16+
517

618
class TestUtils(TestCase):
719
def test_steam_id(self):
8-
steam_id = account_id_to_steam_id("293059984")
9-
10-
self.assertEqual("76561198253325712", steam_id)
20+
self.assertEqual(STEAM_ID, account_id_to_steam_id(ACCOUNT_ID))
21+
self.assertEqual(ACCOUNT_ID, steam_id_to_account_id(STEAM_ID))
22+
self.assertEqual(STEAM_ID, account_id_to_steam_id(int(ACCOUNT_ID)))
23+
self.assertEqual(ACCOUNT_ID, steam_id_to_account_id(int(STEAM_ID)))
1124

1225
def test_to_refined(self):
1326
scrap = 43
@@ -32,3 +45,10 @@ def test_refinedify_down(self):
3245
refined = refinedify(wrong_value)
3346

3447
self.assertEqual(12.44, refined)
48+
49+
def test_trade_url(self):
50+
trade_url = "https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR" # noqa
51+
52+
self.assertEqual(ACCOUNT_ID, get_account_id_from_trade_url(trade_url))
53+
self.assertEqual(STEAM_ID, get_steam_id_from_trade_url(trade_url))
54+
self.assertEqual("0-l_idZR", get_token_from_trade_url(trade_url))

0 commit comments

Comments
 (0)