-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (70 loc) · 2.57 KB
/
main.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
import datetime
from hashlib import md5
from os import getenv
from time import time_ns
from requests import get
from dotenv import load_dotenv
load_dotenv()
PUBLIC_API_KEY = getenv("MARVEL_PUBLIC_API_KEY")
PRIVATE_API_KEY = getenv("MARVEL_PRIVATE_API_KEY")
SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters"
DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}"
PAGE_SIZE = 20
while True:
timestamp = str(time_ns())
key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest()
default_params = {
'apikey': PUBLIC_API_KEY,
'ts': timestamp,
'hash': key_hash,
}
name = input("What Marvel character? ")
keep_searching = True
ID = None
offset = 0
while keep_searching:
print("Trying to find that character . . .")
search_params = {
**default_params,
'nameStartsWith': name,
'limit': PAGE_SIZE,
'offset': offset
}
res = get(SEARCH_URL, search_params).json()
# refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape
data = res['data']
total = data['total']
offset = data['offset']
characters = data['results']
if total == 0:
print("Couldn't find any characters by this name...")
keep_searching = False
elif total == 1:
ID = characters[0]['id']
keep_searching = False
else:
IDs = []
i = 0
print(f"Please select a character)")
for character in characters:
i += 1
print(f"- {i}: {character['name']}")
IDs.append(character['id'])
choice = input(f"Which ID? (1-{len(characters)}), or 'n' for next page ")
if choice == "n":
if offset + PAGE_SIZE >= total:
print("There are no more pages...")
keep_searching = False
else:
offset += PAGE_SIZE
else:
index = int(choice) - 1
ID = IDs[index]
keep_searching = False
found_a_character = True
if ID: # if any previous branch resulted in a result
print("Retrieving details . . .")
res = get(DETAILS_URL.format(id=ID), params=default_params).json()
character = res['data']['results'][0] # should only be one returned
print(f"Name: {character['name']}")
print(f"Bio: {character['description']}")