-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
131 lines (83 loc) · 3.63 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from requests_cache import CachedSession
from hsr_client.datamodels.searchItem import *
from hsr_client.datamodels.character import Character
from hsr_client.routes import *
from typing import Union, List, LiteralString
from hsr_client.utils import generate_t, base36encode
from hsr_client.constants import *
from hsr_client.errors import *
from json import dump
class SRSClient:
'''
StarRailStation Website Client
: initializes the client
'''
def __init__(self) -> None:
self.__session = CachedSession(cache_name='srs.cache', backend='sqlite', expire_after=3600)
self.__session.headers.update(
{'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68',
'referer': 'https://starrailstation.com/'}
)
def generate_hash_route(self, language: Language, route: Routes, goto: bool = False, item_id : Union[int, str]=''):
'''
:generates hashed route for fetching data
--
params
--
- language: Languages Enum
Languages.EN, Languages.RU etc
- route: a Routes object
- goto: if you want to search in a specific route [True]
defaults to False
- item_id : id of the item you want to search in a route
'''
if not isinstance(language, Language):
raise InvalidLanguage
url = route.generate_main_lang_path(language)
if goto:
if route.path is not None:
url = f"{route.generate_goto_lang_path(language)}{item_id}.json"
hashed_path = base36encode(generate_t(url))
return f"{MAIN_ROUTE}{hashed_path}"
def fetch(self, language: Language , route: Routes, goto: bool = False, item_id : Union[int, str] = '') -> List[dict] | dict | None:
'''
:fetches data from the api route
--
params
--
- language: Languages Enum
Languages.EN, Languages.RU etc
- route: a Routes object
- goto: if you want to search in a specific route [True]
defaults to False
- item_id : id of the item you want to search in a route
'''
if not isinstance(language, Language):
raise InvalidLanguage
response = self.__session.get(self.generate_hash_route(language, route, goto, item_id))
if response.status_code < 300:
data = response.json()
if 'entries' in data:
return data['entries']
else:
return data
def get_all_items(self, type: Optional[Item], language: Language = Language.EN) -> list[SearchItem]:
'''
:fetches all items from api route
--
params
--
- language: Languages Enum
Languages.EN, Languages.RU etc
- type : a type object
Item.MATERIALS, Item.PLAYERCARDS, Item.CHARACTERS etc
'''
if not isinstance(language, Language):
raise InvalidLanguage
response = self.fetch(language, SEARCH, False)
if response is not None:
all_items = [SearchItem(**{ **d, **{'id': d['url'].split("/")[1]}}) for d in response]
if type is not None:
return list(filter(lambda x: x.type == type, all_items))
return all_items
raise Exception('Not enough arguments provided, or nothing is returned from api call!')