-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPlayerMaxTrophiesFromAPI.py
120 lines (113 loc) · 4.78 KB
/
getPlayerMaxTrophiesFromAPI.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
import asyncio
import clashroyale
import sqlite3
import globals
globals.init()
async def get_players(cr, clan_groups):
return await asyncio.gather(*[
cr.get_player(*group)
for group in clan_groups
])
async def get_players_max_trophies(max_players_to_update):
# Get Client Objects
officialClient = clashroyale.official_api.Client(globals.officialAPIToken, is_async=True, timeout=999)
conn = sqlite3.connect(globals.databasename)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS players(
player_tag TEXT,
update_date TEXT,
max_trophies TEXT,
UNIQUE(player_tag));''')
conn.commit()
# Loop through the players, iMaxTags at a time
iMaxTags = 1
iMaxRate = 75
if max_players_to_update > 0:
lPlayersTags = [i[0] for i in cursor.execute(
'''SELECT player_tag FROM players WHERE (max_trophies < 100 OR max_trophies IS NULL) AND length(player_tag) > 5 ORDER BY update_date ASC LIMIT %s''' % max_players_to_update)]
else:
lPlayersTags = [i[0] for i in cursor.execute(
'''SELECT player_tag FROM players WHERE (max_trophies < 100 OR max_trophies IS NULL) AND length(player_tag) > 5 ORDER BY update_date ASC''')
if len(i[0]) > 5]
print(lPlayersTags)
if len(lPlayersTags) == 0:
lPlayersTags = [i[0] for i in cursor.execute(
'''SELECT player_tag FROM players WHERE (max_trophies < 100 OR max_trophies IS NULL) AND length(player_tag) > 5 ORDER BY update_date ASC LIMIT %s''' % max_players_to_update)]
for iPlayerGroup in range(0, len(lPlayersTags), iMaxTags * iMaxRate):
print('Player group', iPlayerGroup, '-', iPlayerGroup + iMaxTags * iMaxRate, 'out of', len(lPlayersTags))
lPlayerGroup = [lPlayersTags[x:(x + iMaxTags)] for x in
range(iPlayerGroup, min(len(lPlayersTags), iPlayerGroup + iMaxTags * iMaxRate), iMaxTags)]
while True:
try:
lAllPlayers = await get_players(officialClient, lPlayerGroup)
await asyncio.sleep(2)
except asyncio.exceptions.TimeoutError:
print('Asyncio Timeout Error!')
await asyncio.sleep(2)
continue
except clashroyale.errors.NotResponding:
print('Clash Royale API Timed Out')
await asyncio.sleep(2)
continue
except clashroyale.errors.RatelimitError or clashroyale.errors.RatelimitErrorDetected:
print('API Rate Limit Error')
await asyncio.sleep(2)
continue
except clashroyale.errors.NetworkError:
print('Network Error')
await asyncio.sleep(5)
continue
except clashroyale.errors.ServerError:
print('Server Error')
await asyncio.sleep(5)
continue
# except:
print('Unknown Error')
await asyncio.sleep(5)
continue
except asyncio.exceptions.TimeoutError:
print('Asyncio Timeout Error!')
await asyncio.sleep(2)
continue
except clashroyale.errors.NotResponding:
print('Clash Royale API Timed Out')
await asyncio.sleep(2)
continue
except clashroyale.errors.RatelimitError or clashroyale.errors.RatelimitErrorDetected:
print('API Rate Limit Error')
await asyncio.sleep(2)
continue
except clashroyale.errors.NetworkError:
print('Network Error')
await asyncio.sleep(5)
continue
except clashroyale.errors.ServerError:
print('Server Error')
await asyncio.sleep(5)
continue
# except:
print('Unknown Error')
await asyncio.sleep(5)
continue
except clashroyale.errors.NotFoundError:
print('Not Found Error')
await asyncio.sleep(10)
break
# loop through battles and add to array
for lPlayers in lAllPlayers:
cursor.execute("""UPDATE players
SET max_trophies=%s
WHERE player_tag=%s""" % (
lPlayers['bestTrophies'], '"' + lPlayers['tag'] + '"')) # .replace('#', '')
try:
conn.commit()
except:
pass
break
await asyncio.sleep(10)
conn.commit()
cursor.close()
conn.close()
await officialClient.close()
# asyncio.run(get_players_max_trophies(0))
asyncio.run(get_players_max_trophies(500000))