-
Notifications
You must be signed in to change notification settings - Fork 0
/
genshin_dev_data.py
75 lines (59 loc) · 2.29 KB
/
genshin_dev_data.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
"""
class used to get data from https://api.genshin.dev
"""
from typing import Dict
import requests
class InvalidCharacterException(Exception):
pass
class GenshinDevData:
GENSHIN_DEV_URL = 'https://api.genshin.dev'
def send_request(session: requests.Session, method: str, endpoint: str) -> Dict[str, any]:
resp = session.request(method, GenshinDevData.GENSHIN_DEV_URL + endpoint, timeout=20)
if resp.status_code != 200:
raise requests.RequestException(f'Bad response code {resp.status_code}. Response: {resp.text}')
return resp.json()
"""
returns: dict with keys:
[
'talent-book',
'boss-material',
'common-ascension-material',
'crown'
]
Values are lists of dictionaries guaranteed to have KVPS:
{
'name': <material-name>,
'rarity': <material-rarity>,
}
"""
def get_character_talent_materials(character: str):
ret = dict()
session = requests.session()
# get book
books = GenshinDevData.send_request(session, 'get', '/materials/talent-book')
for book, info in books.items():
if 'characters' in info and character in info['characters']:
ret['talent-book'] = info['items']
break
# get boss material
boss_materials = GenshinDevData.send_request(session, 'get', '/materials/talent-boss')
for material, info in boss_materials.items():
if 'characters' in info and character in info['characters']:
ret['boss-material'] = [{
'name': info['name'],
'rarity': 5,
}]
break
# get common ascension material
common_materials = GenshinDevData.send_request(session, 'get', '/materials/common-ascension')
for material, info in common_materials.items():
if 'characters' in info and character in info['characters']:
ret['common-ascension-material'] = info['items']
break
if not ('talent-book' in ret and 'boss-material' in ret and 'common-ascension-material' in ret):
raise InvalidCharacterException
ret['crown'] = [{
'name': 'Crown of Insight',
'rarity': 5,
}]
return ret