This repository was archived by the owner on Jan 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·264 lines (206 loc) · 6.33 KB
/
bot.py
File metadata and controls
executable file
·264 lines (206 loc) · 6.33 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
#!/usr/bin/env python3
"""
Bot
"""
import urllib.request
import random
import json
import traceback
from pathlib import Path
from time import sleep
WEBHOOK_URL = json.loads(Path("conf.json").read_text())["webhook_url"]
PING_ERRORS = "<@818564860484780083>"
PING_NEW_VERSION = "<@&1342761186353090590>"
CHECK_IT = False
class Response:
def __init__(self, url, status, body, headers):
self.url = url
self.status = status
self.body = body
self.headers = headers
class Date():
def __init__(self, date):
self.string = date
self.date_set = set()
for sub in date.split():
self.date_set.add(sub.strip(" \t\r\n\v\f,.<>;'/:?{}[]-=_+\\|()*&^%$#@!`~"))
def __eq__(self, other):
if (len(self.date_set) == len(other.date_set)):
for v in self.date_set:
if v not in other.date_set:
return False
return True
else:
return False
def __repr__(self):
return self.string
def http_request(url, method='GET', data=None, headers={}):
resp = urllib.request.urlopen(urllib.request.Request(url, data, method=method, headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0"
} | headers))
data = resp.read()
r = Response(resp.url, resp.status, data, resp.headers)
resp.close()
return r
def post(url, data):
data = json.dumps(data).encode("utf-8")
return http_request(url, 'POST', data, {"Content-Type": "application/json"})
def send_message(content):
print("[Message]\n" + content + "\n[/Message]")
try:
post(WEBHOOK_URL, {
"content": content,
})
except:
pass
def get_app_updated_date_play(appid):
if CHECK_IT: return random.choice(["17 Feb 2025", "Feb 17, 2025", "Mar 6, 2025", None])
try:
r = http_request(f"https://play.google.com/store/apps/details?id={appid}")
r = r.body.decode("utf-8")
i = r.index("Updated on")
if (i < 0):
print("Failed to parse response: String 'Updated on' not found")
return None
d = r[i:i+100]
d = d.partition('>')[2].partition('>')[2].partition('<')[0]
return d
except:
traceback.print_exc()
return None
def get_app_version_ios(appurl):
if CHECK_IT: return random.choice(["1.5.5", "1.0.4", "1.0.3", None])
try:
r = http_request(appurl)
r = r.body.decode("utf-8")
i = r.index('whats-new__latest__version">Version ')
if (i < 0):
print("Failed to parse App Store response: Could not find version string start")
return None
d = r[i:i+100]
d = d.partition('>')[2].partition('<')[0][8:]
return d
except:
traceback.print_exc()
return None
class UpdateTracker:
def __init__(self, display_name, identifier):
self.display_name = display_name
self.identifier = identifier
self.last = None
self.consec_errors = 0
self.check()
class PlayTracker(UpdateTracker):
def check(self):
date = get_app_updated_date_play(self.identifier)
if not date:
self.consec_errors += 1
if self.consec_errors == 4:
return f"{PING_ERRORS} Failed to fetch google play update date for {self.display_name} four times in a row!"
else:
return None
else:
self.consec_errors = 0
date = Date(date)
if not self.last:
print(f"Setting {self.display_name} (Google Play) date for first time")
self.last = date
else:
prev = self.last
self.last = date
if prev != date:
print(f"{prev} != {date}")
return f"{PING_NEW_VERSION}\n## {self.display_name} updated on Google Play!\nOld date: {prev}\nNew date: {date}"
else:
print(f"{prev} == {date}")
def table(self):
return f"{self.display_name} (Android): {self.last}"
class AppleTracker(UpdateTracker):
def check(self):
ver = get_app_version_ios(self.identifier)
if not ver:
self.consec_errors += 1
if self.consec_errors == 4:
return f"{PING_ERRORS} Failed to fetch apple app store version for {self.display_name} four times in a row!"
else:
return None
else:
self.consec_errors = 0
if not self.last:
print(f"Setting {self.display_name} (App Store) version for first time")
self.last = ver
else:
prev = self.last
self.last = ver
if prev != ver:
print(f"{prev} != {ver}")
return f"{PING_NEW_VERSION}\n## {self.display_name} updated on App Store!\nOld version: {prev}\nNew version: {ver}"
else:
print(f"{prev} == {ver}")
def table(self):
return f"{self.display_name} (iOS): {self.last}"
def rand_wait(base, variance):
sleep(base + (2 * variance * (random.random() - 0.5)))
LAST_DATE = None
LAST_IOS_VER = None
CONSEC_ERRORS = 0
def generate_report():
global LAST_DATE
global LAST_IOS_VER
global CONSEC_ERRORS
date = get_app_updated_date_play("com.mediocre.smashhit")
ios_ver = get_app_version_ios("https://apps.apple.com/us/app/smash-hit/id603527166")
if not date or not ios_ver:
CONSEC_ERRORS += 1
if CONSEC_ERRORS == 4:
send_message(f"{PING_ERRORS} Failed to fetch data four times in a row! Check that the bot is still working.")
return
else:
CONSEC_ERRORS = 0
date = Date(date)
if not LAST_DATE:
print("Setting date for first time")
LAST_DATE = date
LAST_IOS_VER = ios_ver
send_message(f"## Starting up!\nRecorded current Play Store update date as: {date}\nRecorded current app store version as: {ios_ver}")
else:
if LAST_DATE != date:
print(f"{repr(LAST_DATE)} != {repr(date)}")
send_message(f"{PING_NEW_VERSION}\n## Smash Hit updated on Google Play!\nOld update date: {LAST_DATE}\nNew update date: {date}")
LAST_DATE = date
else:
print(f"{repr(LAST_DATE)} == {repr(date)}")
if LAST_IOS_VER != ios_ver:
send_message(f"{PING_NEW_VERSION}\n## Smash Hit updated on App Store!\nOld version: {LAST_IOS_VER}\nNew version: {ios_ver}")
LAST_IOS_VER = ios_ver
def main():
while True:
generate_report()
rand_wait(60 * 30, 60 * 4)
def do_checking():
trackers = [
PlayTracker("Smash Hit", "com.mediocre.smashhit"),
AppleTracker("Smash Hit", "https://apps.apple.com/us/app/smash-hit/id603527166"),
AppleTracker("Smash Hit+", "https://apps.apple.com/us/app/smash-hit/id6503247519"),
]
# Startup message
msg = "## Starting up!\nCurrent versions recorded as:\n"
for t in trackers:
msg += t.table() + "\n"
send_message(msg.rstrip())
while True:
if CHECK_IT:
sleep(10)
else:
rand_wait(60 * 30, 60 * 5)
for t in trackers:
msg = t.check()
if msg:
send_message(msg)
def main_new():
try:
do_checking()
except KeyboardInterrupt:
print("Bye!")
if __name__ == "__main__":
main_new()