-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
267 lines (232 loc) · 10.1 KB
/
main.py
File metadata and controls
267 lines (232 loc) · 10.1 KB
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
import tweepy
import time
import datetime
import requests
import json
import pprint
import datetime
import os
import drawResults
import pytz
import traceback
f = open('accounts.json')
accounts = json.load(f)
if os.path.exists("auth.json"):
f = open('auth.json')
auth_json = json.load(f)
SMASHGG_KEY = auth_json["SMASHGG_KEY"]
f = open('update_time.json')
updateTime = json.load(f)["updateTime"]
for account in accounts:
print(account)
try:
BEARER_KEY = auth_json[account]["BEARER"]
CONSUMER_KEY = auth_json[account]["CONSUMER_KEY"]
CONSUMER_SECRET = auth_json[account]["CONSUMER_SECRET"]
ACCESS_KEY = auth_json[account]["ACCESS_KEY"]
ACCESS_SECRET = auth_json[account]["ACCESS_SECRET"]
twitter_API_v2 = tweepy.Client(
bearer_token=BEARER_KEY,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_KEY,
access_token_secret=ACCESS_SECRET
)
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
try:
f = open('events_'+account+'.json')
events_json = json.load(f)
except:
events_json = {}
print("Get character list")
f = requests.get(
"https://api.start.gg/characters?videogameId="+accounts[account]["videogameid"])
characters_json = json.loads(f.text)["entities"]
print("Get tournaments in startgg")
r = requests.post(
"https://www.start.gg/api/-/gql",
headers={
"client-version": "20",
'Content-Type': 'application/json',
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36"
},
json={
'query': '''
query TournamentsByCountry($cCode: String!, $perPage: Int!) {
tournaments(query: {
perPage: $perPage
filter: {
countryCode: $cCode
videogameIds: ['''+accounts[account]["videogameid"]+''']
upcoming: true,
computedUpdatedAt: '''+str(int(updateTime-datetime.timedelta(hours=24).total_seconds()))+'''
}
}) {
nodes {
id
startAt
events {
id
videogame {
id
}
startAt
}
}
}
}
''',
'variables': {
"cCode": accounts[account]["country"],
"perPage": 100
},
}
)
resp = json.loads(r.text)
time.sleep(1)
data = resp["data"]["tournaments"]["nodes"]
proximos_eventos = []
print(f"Found {len(data)} tournaments")
for tournament in data:
r = requests.post(
"https://www.start.gg/api/-/gql",
headers={
"client-version": "20",
'Content-Type': 'application/json',
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36"
},
json={
'query': '''
query Tournament($tournamentId: ID!) {
tournament(id: $tournamentId) {
id
name
url
city
timezone
startAt
endAt
registrationClosesAt
venueName
venueAddress
addrState
events {
id
name
isOnline
state
numEntrants
videogame {
id
}
startAt
phaseGroups {
id
phase {
id
name
}
progressionsOut {
id
}
}
}
streams {
streamName
}
images{
id
url
type
}
}
},
''',
'variables': {
"tournamentId": tournament["id"]
},
}
)
resp = json.loads(r.text)
time.sleep(1)
if resp.get("data", {}).get("tournament", None) == None:
print("Erro? - "+str(resp))
continue
tournament_data = resp["data"]["tournament"]
smash_ultimate_tournaments = 0
for event in tournament_data["events"]:
if event["videogame"]["id"] == int(accounts[account]["videogameid"]):
smash_ultimate_tournaments += 1
for event in tournament_data["events"]:
# Smash Ultimate
if event["videogame"]["id"] != int(accounts[account]["videogameid"]):
continue
if event["startAt"] > time.time():
event["tournament"] = tournament_data["name"]
event["tournament_id"] = tournament_data["id"]
event["city"] = tournament_data["city"]
event["url"] = "https://start.gg"+tournament_data["url"]
event["streams"] = tournament_data["streams"]
event["timezone"] = tournament_data["timezone"]
event["tournament_startAt"] = tournament_data["startAt"]
event["tournament_endAt"] = tournament_data["endAt"]
event["tournament_registrationClosesAt"] = tournament_data["registrationClosesAt"]
event["images"] = tournament_data["images"]
event["tournament_multievent"] = False if smash_ultimate_tournaments <= 1 else True
event["tournament_venueName"] = tournament_data["venueName"]
event["tournament_venueAddress"] = tournament_data["venueAddress"]
event["tournament_addrState"] = tournament_data["addrState"]
proximos_eventos.append(event)
for evento in proximos_eventos:
if str(evento["id"]) not in events_json.keys():
print(account+" - novo evento: " +
evento["name"] + " - " + evento["tournament"])
data_time = datetime.datetime.fromtimestamp(
evento["startAt"], tz=pytz.timezone(accounts[account]["timezone"]))
data = data_time.strftime(accounts[account]["timeFormat"])
data_registration_time = datetime.datetime.fromtimestamp(min(
evento["tournament_registrationClosesAt"], evento["startAt"]), tz=pytz.timezone(accounts[account]["timezone"]))
data_registration = data_registration_time.strftime(
accounts[account]["timeFormat"])
torneio_type = accounts[account]["text-online-tournament"] if evento["isOnline"] == True else accounts[account]["text-offline-tournament"]
torneio_name = evento["tournament"]
if evento["tournament_multievent"]:
torneio_name += " - " + evento["name"]
location = ""
if not evento.get("isOnline") and \
(evento.get("tournament_venueName") or evento.get("tournament_venueAddress")):
location += "📍"
if evento.get("tournament_venueName"):
location += evento.get("tournament_venueName")+", "
if evento.get("tournament_venueAddress"):
splitted = evento.get(
"tournament_venueAddress").split(",")
if len(splitted) == 1:
location += splitted[0]
if len(splitted) == 2:
location += splitted[0]
if len(splitted) == 3:
location += splitted[1]
if len(splitted) == 4:
location += splitted[-3]+", "+splitted[-2]
if len(splitted) == 5:
location += splitted[-3]
location += "\n"
tweet_id = twitter_API_v2.create_tweet(
text=torneio_type + " " +
torneio_name + "\n" +
location +
"📅 "+accounts[account]["text-tournament-start"]+": "+data+" ("+accounts[account]["timezone"]+")"+"\n" +
"✏️ "+accounts[account]["text-tournament-registration-ends"]+": "+data_registration+" ("+accounts[account]["timezone"]+")"+"\n" +
evento["url"]
)
time.sleep(1)
events_json[evento["id"]] = evento
events_json[evento["id"]]["tweet_id"] = tweet_id.data["id"]
except Exception as e:
print(traceback.format_exc())
with open('events_'+account+'.json', 'w') as outfile:
json.dump(events_json, outfile, indent=4)
with open('update_time.json', 'w') as outfile:
json.dump({"updateTime": time.time()}, outfile, indent=4)