-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplatoon.py
272 lines (186 loc) · 7.45 KB
/
splatoon.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import datetime
import json
import requests
import iksm
import tools
BASE_URL = 'http://localhost:8080'
with open('data/splatoon2/gamemodes.json') as file_:
GAMEMODES = json.loads(file_.read())
with open('data/splatoon2/rulesets.json') as file_:
RULESETS = json.loads(file_.read())
COLORS = [0xfa5a00, 0x2851f6, 0xc800dc, 0xf93195, 0x00c8b4, 0xa0cc0a]
class Stage:
name = None
sid = None
def __init__(self, data):
self.name = data['name']
self.sid = data['id']
def __str__(self):
return self.name
class Weapon:
wid = None
name = None
image = None
thumbnail = None
def __init__(self, entry):
self.wid = entry['id']
self.name = entry['name']
self.image = entry['image']
self.thumbnail = entry['thumbnail']
def __str__(self):
return self.name
def __repr__(self):
return self.__str__()
def get_salmon_weapons(weapons):
result = []
for weapon in weapons:
key = [x for x in weapon.keys() if x not in ['image', 'id', 'name', 'thumbnail']][0]
result.append(Weapon(weapon[key]))
return result
class Match:
def __init__(self, data):
self.result = data['my_team_result']['key']
self.stage = Stage(data['stage'])
self.ruleset = data['rule']['name']
self.gamemode = data['game_mode']['name']
self.symbol = 'W' if self.result == 'victory' else 'L'
self.weapon = data['player_result']['player']['weapon']['name']
def __str__(self):
return f'{self.result.capitalize()}: {self.gamemode} {self.ruleset} on {str(self.stage)}'
def __repr__(self):
return self.__str__()
class GenericScheduleItem:
start = None
end = None
start_stamp = None
def time_range(self):
time_format = '%b %-d %-I:%M%p'
return f'{self.start.strftime(time_format)} – {self.end.strftime(time_format)}'
def start_string(self):
time_format = '%-I:%M%p'
return f'{self.start.strftime(time_format)} {tools.get_today_tomorrow(self.start)}'
def duration_remaining(self):
now = datetime.datetime.now()
if now < self.start:
return (self.start - now, 'until start')
elif now < self.end:
return (self.end - now, 'remaining')
else:
return (now - self.end, 'ago')
def __init__(self, entry):
self.start = datetime.datetime.fromtimestamp(int(entry['start_time']))
self.end = datetime.datetime.fromtimestamp(int(entry['end_time']))
self.start_stamp = entry['start_time']
class ScheduleItem(GenericScheduleItem):
ruleset = None
gamemode = None
stages = []
def __init__(self, entry):
self.ruleset = (entry['rule']['name'], entry['rule']['key'])
self.gamemode = (entry['game_mode']['name'], entry['game_mode']['key'])
self.stages = [Stage(x) for x in [entry['stage_a'], entry['stage_b']]]
self.start = datetime.datetime.fromtimestamp(int(entry['start_time']))
self.end = datetime.datetime.fromtimestamp(int(entry['end_time']))
def __str__(self):
gamemode = self.gamemode[0]
ruleset = self.ruleset[0]
stages = ' & '.join(map(str, self.stages))
time_range = self.time_range()
return f'{gamemode} {ruleset} on {stages} at {time_range}'
def __repr__(self):
return self.__str__()
class SalmonScheduleItem(GenericScheduleItem):
weapons = None
stage = None
def __init__(self, entry):
self.stage = entry['stage']['name']
self.start = datetime.datetime.fromtimestamp(int(entry['start_time']))
self.end = datetime.datetime.fromtimestamp(int(entry['end_time']))
self.weapons = get_salmon_weapons(entry['weapons'])
def __str__(self):
time_range = self.time_range()
weapon_string = tools.english_list(map(str, self.weapons))
return f'Salmon Run on {self.stage} with the {weapon_string} at {time_range}'
def __repr__(self):
return self.__str__()
def combine_gamemodes(schedule):
all_entries = []
for gamemode in schedule.values():
all_entries += gamemode
return all_entries
def search_schedule(focus, *args, schedule_=None):
schedule = get_schedule() if schedule_ is None else schedule_
entries = schedule if schedule_ is not None else [ScheduleItem(x) for x in combine_gamemodes(schedule)]
filtered = list(sorted(filter(lambda x: focus(x) in args, entries), key=lambda x: x.start))
return filtered
def get_current_stages():
schedule = get_schedule()
current_stages = []
for gamemode in GAMEMODES:
current_entries = schedule[gamemode['id']]
first = [ScheduleItem(x) for x in current_entries][0]
current_stages.append(first)
return current_stages
def stages_notification(blocks, include_gamemode=True, include_ruleset=False,
include_stage=False, include_time=True, return_sentence=True):
with open('data/splatoon2/gamemodes.json') as file_:
gamemodes = json.loads(file_.read())
result = []
for gamemode in gamemodes:
current_blocks = [x for x in blocks if gamemode['id'] == x.gamemode[1]]
if len(current_blocks) == 0:
continue
mode_result = []
if include_gamemode:
mode_result.append(gamemode['name'])
for block in current_blocks:
current_string = []
if include_ruleset:
current_string.append(block.ruleset[0])
if include_stage:
current_string.append(f"on {' and '.join(map(str, block.stages))}")
if include_time:
current_string.append(f'at {block.start_string()}')
mode_result.append(' '.join(current_string))
result.append(': '.join(mode_result))
return '. '.join(result) if return_sentence else result
def get_schedule_objects():
return {key: value for key, value in [(mode, [ScheduleItem(x) for x in entries]) for mode, entries in get_schedule().items()]}
def call_splatoon_api(path, user):
url = f'https://app.splatoon2.nintendo.net{path}'
cookie = user['cookie']
req = requests.get(url, cookies={'iksm_session': cookie})
if 'code' in req.json().keys():
# cookie expired, get a new one
nickname, new_cookie = iksm.get_cookie(user['session_token'])
iksm.update_users({user['uid']: {'cookie': new_cookie}})
req = requests.get(url, cookies={'iksm_session': new_cookie})
return req.json()
def get_schedule():
path = '/api/schedules'
user = iksm.get_user('580157651548241940')
return call_splatoon_api(path, user)
def get_salmon_schedule():
path = '/api/coop_schedules'
user = iksm.get_user('580157651548241940')
return call_splatoon_api(path, user)
def get_matches(results):
return [Match(x) for x in results['results']]
def get_weapons(matches):
return [x.weapon for x in matches]
def get_results(user):
return call_splatoon_api('/api/results', user)
def get_salmon_results(user):
return call_splatoon_api('/api/coop_results', user)
def get_records(user):
return call_splatoon_api('/api/records', user)
def get_ranks(records):
modes = [
('Splat Zones', 'udemae_zones'),
('Tower Control', 'udemae_tower'),
('Rainmaker', 'udemae_rainmaker'),
('Clam Blitz', 'udemae_clam')
]
return {display: records['records']['player'][key]['name'] for display, key in modes}
if __name__ == '__main__':
pass