Skip to content

Commit 9a77329

Browse files
initial template for calling tcg pricing
1 parent f7a7dd1 commit 9a77329

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

card_data/tcg_pricing.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import requests
2+
import polars as pl
3+
4+
pl.Config(tbl_rows=-1)
5+
6+
SET_PRODUCT_MATCHING = {
7+
"sv01": "22873"
8+
}
9+
10+
def is_card(item):
11+
"""Check if item has a 'Number' field in extendedData"""
12+
for data_field in item["extendedData"]:
13+
if data_field["name"] == "Number":
14+
return True
15+
return False
16+
17+
def get_card_number(card):
18+
"""Get the card number from extendedData"""
19+
for data_field in card["extendedData"]:
20+
if data_field["name"] == "Number":
21+
return data_field["value"]
22+
return None
23+
24+
def pull_product_information():
25+
# Get products
26+
url = f"https://tcgcsv.com/tcgplayer/3/{SET_PRODUCT_MATCHING['sv01']}/products"
27+
r = requests.get(url)
28+
29+
if r.status_code != 200:
30+
return
31+
32+
data = r.json()
33+
34+
# Get prices ONCE
35+
url_prices = f"https://tcgcsv.com/tcgplayer/3/22873/prices"
36+
r_prices = requests.get(url_prices)
37+
price_data = r_prices.json()
38+
39+
# Create price lookup dictionary
40+
price_dict = {price["productId"]: price["marketPrice"]
41+
for price in price_data["results"]}
42+
43+
# Build lists
44+
product_id_list = []
45+
name_list = []
46+
card_number_list = []
47+
price_list = []
48+
49+
for card in data["results"]: # Remove islice to get all cards
50+
if not is_card(card):
51+
continue
52+
53+
number = get_card_number(card)
54+
card_number_list.append(number)
55+
56+
# Clean name
57+
name = card["name"].partition("-")[0].strip() if "-" in card["name"] else card["name"]
58+
name_list.append(name)
59+
60+
product_id = card["productId"]
61+
product_id_list.append(product_id)
62+
63+
# Look up price from dictionary (fast!)
64+
market_price = price_dict.get(product_id)
65+
price_list.append(market_price)
66+
67+
df = pl.DataFrame({
68+
"product_id": product_id_list,
69+
"name": name_list,
70+
"card_number": card_number_list,
71+
"price": price_list,
72+
})
73+
74+
print(df.sort("card_number"))
75+
76+
pull_product_information()

0 commit comments

Comments
 (0)