Skip to content

Commit 711fbb6

Browse files
committed
Refactor set info requests
1 parent 5911c9e commit 711fbb6

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

src/scry/cli.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from scry.db_queries import get_unique_cards
55
from scry.loading import Loading
6-
from scry.request import find_current_release, check_date_past
6+
from scry.request import find_current_release, check_date_past, get_set_info
77
from . import (
88
get_random_card,
99
insert_cards,
@@ -140,43 +140,44 @@ def handle_search(args, db_connection):
140140

141141

142142
def handle_set(args, db_connection):
143-
# Search for a set of cards
143+
# Searches for a specific set of cards
144+
144145
connection = db_connection
146+
147+
# loading animation
148+
loading = Loading().start()
149+
150+
# use setcode, or find setcode of "latest" set
145151
query_setcode = ""
146152
if args.set_query.lower() == "latest":
147-
# Find the moce recent release and query stats for it
153+
# Find the most recent release and query stats for it
148154
current_set = find_current_release(set_codes())
149-
current_set_code = current_set.get("set_code")
150-
query_setcode = str(current_set_code)
151-
current_set_name = current_set.get("name")
152-
print(f"Stats for {current_set_name} ({current_set_code}):")
155+
query_setcode = str(current_set)
153156
else:
154157
query_setcode = str(args.set_query)
155-
print(
156-
f"Stats for \033[1m{lookup_set_info("name", args.set_query.upper()).upper()}\033[0m"
157-
)
158158

159-
query = f"set:{query_setcode}" # unique:prints includes variations within set, otherwise only unique cards
159+
# request general set info from /set:code
160+
set_info = get_set_info(query_setcode)
160161

161-
card_list = get_card_list(query) or []
162-
stamp = get_timestamp()
162+
set_name = set_info.get("name")
163+
set_date = set_info.get("released_at")
164+
set_count = set_info.get("card_count")
163165

166+
# request list of cards from set
167+
# optionally append `unique:prints` for variations within set
168+
setlist_query = f"set:{query_setcode}"
169+
card_list = get_card_list(setlist_query) or []
170+
stamp = get_timestamp()
164171
insert_cards(card_list, stamp, connection)
165172

166-
# loading animation
167-
loading = Loading().start()
168-
set_release_details = []
169-
set_release_details.append(lookup_set_info("release_date", query_setcode.upper()))
170-
set_release_details.append(lookup_set_info("card_count", query_setcode.upper()))
171-
set_release_details.append(get_unique_cards(connection, stamp))
172173
loading.end()
173174

174-
print(
175-
f"""
176-
\x1b[3m Released: {set_release_details[0]}
177-
{set_release_details[1]} cards in set
178-
(Showing {set_release_details[2]} unique cards only)\n \033[0m"""
179-
)
175+
print(f"Stats for \033[1m{set_name} ({query_setcode.upper()})\033[0m")
176+
print(f"\x1b[3m Released: {set_date}")
177+
print(f" {set_count} cards in set")
178+
print(f" Showing {get_unique_cards(connection,stamp)} unique cards\n \033[0m")
179+
180+
# finally show stats for the set
180181
print_stats(connection, stamp)
181182

182183

@@ -232,9 +233,11 @@ def format_set_info(set_details) -> str:
232233

233234

234235
def lookup_set_info(info: str, set_code: str) -> str:
236+
# TODO: don't call set codes every time!!
235237
setlist = set_codes()
236238
for s in setlist:
237239
if set_code == s["set_code"]:
240+
print(s[info])
238241
return s[info]
239242
return "Set info not found. Check the setcode is correct. You can request name, card_count, release_date"
240243

src/scry/request.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,30 @@ def set_codes(include_all_sets=False) -> list:
208208
except requests.exceptions.RequestException as err:
209209
print("Setlist ERROR: ", err)
210210
return []
211+
212+
213+
def get_set_info(set_code: str) -> dict:
214+
endpoint = "/sets/"
215+
216+
sleep(0.1) # just in case we ever call this in a loop
217+
218+
try:
219+
req_url = url + endpoint + set_code
220+
res = requests.get(req_url, headers=headers, timeout=TIMEOUT)
221+
res.raise_for_status()
222+
223+
response = res.json()
224+
225+
show_warnings(response)
226+
227+
if res.status_code == 200:
228+
return response
229+
else:
230+
print(
231+
"Something may have gone wrong getting info about the set. Status Code: ",
232+
res.status_code,
233+
)
234+
return {}
235+
except requests.exceptions.RequestException as err:
236+
print("Setlist ERROR: ", err)
237+
return {}

0 commit comments

Comments
 (0)