-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_bot.py
332 lines (261 loc) · 10.3 KB
/
update_bot.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from bs4 import BeautifulSoup
from subprocess import call
import requests
import json
import telepot
import os
import schedule
import time
import logging
# import pdb
# Set up logging
logging.basicConfig(filename='updater.log',level=logging.INFO)
logger = logging.getLogger(__name__)
gist_id = str(os.environ['VERSION_GIST'])
version_file = 'known_versions.json'
def get_version_file():
with open(version_file,'w') as verfile:
call(['gist', '-r', gist_id], stdout=verfile)
def get_known_versions():
global known_versions
with open(version_file,'r') as verfile:
known_versions = json.load(verfile)
return known_versions
def update_known_versions(package, version):
with open(os.devnull, 'w') as FNULL:
call(['gist', '-u', gist_id, version_file], stdout=FNULL)
print(('Updated gist file with version "' + version + '" for ' + package))
def do_update():
get_version_file()
get_known_versions()
updates = [
# G870A(),
qBittorrent(),
libtorrent(),
KeePassXC(),
Atom(),
AsciidoctorPDF(),
AsciiBinder(),
PiHole()
]
for update in updates:
# print(update.known_versions)
# print(update.known)
# print(update.version)
# print(update.url)
if update.version not in update.known_versions:
notify(update.package,update.version,update.url,update.known)
update.append_known(update.package,update.version)
else:
print((update.package + ' version "' + update.version + '" is already known.'))
def liquidize(input):
data = BeautifulSoup(input.text,'html.parser')
return data
def prepare_msg(package, version, url):
text = 'A new update for %s is released:\n%s\nDownload here: %s'
message = text % (package, str(version), str(url))
return message
def send_bot_msg(message):
bot = telepot.Bot(os.environ['BOT_TOKEN'])
user_id = os.environ['USER_ID']
bot.sendMessage(user_id, message)
def notify(package, version, url, known):
message = prepare_msg(package, version, url)
send_bot_msg(message)
# print(message)
class Update(object):
def __init__(self):
self.package = package
self.version = version
self.known_versions = get_known_versions()[package]
self.known = self.get_current()
def get_current(self, package):
self.known = str(known_versions[package][-1])
return self.known
def write_log(self, package):
logging.debug('Performing update for ' + self.package)
# def get_github_release(self, package, srcstring):
def strain_version(self, verstring, verpos, endpos=''):
start = verstring.find(verpos, 0)
if endpos == '':
end = len(verstring)
else:
end = verstring.find(endpos, 0)
sievelen = len(verpos)
self.version = str(verstring[start+sievelen:end])
return self.version
def append_known(self, package, version):
self.known_versions.append(str(version))
known_versions[package] = self.known_versions
print(('Added ' + package + ' version ' + version + ' to list.'))
with open('known_versions.json','w') as verfile:
verlist = json.dumps(known_versions, indent=4, sort_keys=True)
verfile.write(verlist)
update_known_versions(package, version)
def get_data(self, srcstring, headers=''):
data = requests.get(srcstring, headers)
if data.status_code == 200:
return data
else:
return None
def print_version(self, version):
print((self.version))
class G870A(Update):
def __init__(self):
self.package = 'G870A'
self.srcstring = 'https://services.att.com/kmservices/v2/contents/KM1126238?app-id=esupport'
self.xml = self.get_data(self.srcstring, {'Accept': 'application/json'}).json()['resultBody']['contentTypeProperties']['currentsoftdetails']
self.version = self.get_version()
self.previous_version = self.get_previous()
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
pos = self.xml.find("Baseband version:",0)
verpos = self.xml.find("G870A",pos+17,pos+57)
version = self.xml[verpos:verpos+13]
return version
def get_previous(self):
prev = self.xml.rfind("Previous versions required:",0)
prev_ver = self.xml.find("G870A",prev+27)
previous_version = self.xml[prev_ver:prev_ver+13]
return previous_version
def build_url(self):
url = 'https://xdmd.sl.attcompute.com/agents/42998/1488/SS-' + self.get_previous() + '-to-' + self.get_version()[7:] + '-UP'
return url
class qBittorrent(Update):
def __init__(self):
self.package = 'qBittorrent'
self.srcstring = 'https://api.github.com/repos/qbittorrent/qBittorrent/tags'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
verstring = str(self.data[0]['name'])
version = 'v' + self.strain_version(verstring, 'release-')
return version
def build_url(self):
url = 'https://github.com/qbittorrent/qBittorrent/archive/release-' + self.get_version()[1:] + '.tar.gz'
return url
class libtorrent(Update):
def __init__(self):
self.package = 'libtorrent'
self.srcstring = 'https://api.github.com/repos/arvidn/libtorrent/releases'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
verstring = str(self.data[0]['name'])
version = 'v' + self.strain_version(verstring, 'libtorrent-')
return version
def build_url(self):
url = 'https://github.com/arvidn/libtorrent/archive/libtorrent-' + self.get_version()[1:] + '.tar.gz'
return url
class KeePassXC(Update):
def __init__(self):
self.package = 'KeePassXC'
self.srcstring = 'https://api.github.com/repos/keepassxreboot/keepassxc/releases'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
version = str(self.data[0]['tag_name'])
return version
def build_url(self):
url = 'https://github.com/keepassxreboot/keepassxc/releases/tag/' + self.version
return url
class Atom(Update):
def __init__(self):
self.package = 'Atom'
self.srcstring = 'https://api.github.com/repos/atom/atom/releases/latest'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
version = str(self.data['name'])
return version
def build_url(self):
url = 'https://github.com/atom/atom/releases/tag/' + 'v' + self.get_version()
return url
class AsciidoctorPDF(Update):
def __init__(self):
self.package = 'Asciidoctor-PDF'
self.srcstring = 'https://api.github.com/repos/asciidoctor/asciidoctor-pdf/releases'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
version = str(self.data[0]['name'])
return version
def build_url(self):
url = str(self.data[0]['html_url'])
return url
class AsciiBinder(Update):
def __init__(self):
self.package = 'AsciiBinder'
self.srcstring = 'https://api.github.com/repos/redhataccess/ascii_binder/tags'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
version = str(self.data[0]['name'])
return version
def build_url(self):
url = str(self.data[0]['tarball_url'])
return url
class PiHole(Update):
def __init__(self):
self.package = 'Pi-Hole'
self.srcstring = 'https://api.github.com/repos/pi-hole/pi-hole/releases'
self.data = json.loads(self.get_data(self.srcstring).text)
self.known = self.get_current(self.package)
self.known_versions = get_known_versions()[self.package]
self.version = self.get_version()
self.url = self.build_url()
self.write_log(self.package)
def get_version(self):
version = str(self.data[0]['tag_name'])
return version
def build_url(self):
url = str(self.data[0]['html_url'])
return url
def main():
send_bot_msg("All's well, started up fine.")
do_update()
schedule.every(2).hours.do(do_update)
try:
while True:
schedule.run_pending()
time.sleep(30)
except requests.exceptions.ConnectionError:
logging.error('ConnectionError occurred', exc_info=True)
send_bot_msg('I could not retrieve something. Will try again later.')
pass
except KeyboardInterrupt:
print("Terminated!")
except (NameError, RuntimeError, TypeError):
print("There was a problem with the code, quitting.")
except:
logger.error('Something has broken.', exc_info=True)
send_bot_msg('Update bot has crashed!')
if __name__ == "__main__":
main()