This repository has been archived by the owner on Jul 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_wow_addons.py
305 lines (227 loc) · 10.9 KB
/
update_wow_addons.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
from configparser import ConfigParser
from multiprocessing import Lock, Pool, TimeoutError as mpTimeoutError, Value
from os import cpu_count, getenv
from pathlib import Path
from platform import system as pf_system
from random import randrange
from sys import exit
from time import time
from zipfile import ZipFile
import cloudscraper
from colorama import Fore, Style, deinit, init
from tqdm import tqdm
from bs4 import BeautifulSoup as bs
class Updater:
def __init__(self, testing=False):
self.testing = testing # if true, game dir changes and random addons are updated
self.base_url = 'https://www.curseforge.com'
self.timeout = 20 # seconds
self.worker_timed_out = False
self.allowed_release_types = 'RB' # [R = release, B = beta, A = alpha]
self.cache_dirs = {
'Windows': Path(str(getenv('temp'))),
'Linux': Path.home() / '.cache',
'Darwin': Path.home() / '.cache'
}
self.cache_dir = self.cache_dirs[pf_system()] / 'wow-addon-updates'
if not self.cache_dir.is_dir():
try:
self.cache_dir.mkdir()
except PermissionError as e:
raise RuntimeError(f'{Fore.RED}Do not have permissions to access {self.cache_dir}, error:\n {e}')
self.config_file = Path(__file__).resolve().parent / 'update_wow_addons.config'
with open(self.config_file, 'r') as f:
self.config = ConfigParser(allow_no_value=True, interpolation=None)
self.config.read_file(f)
if self.testing:
self.game_dir = Path(__file__).resolve().parent / 'testing'
print(f'{Fore.YELLOW}### Running in testing mode. Changing game directory to '
f'\'{self.game_dir}\' and updating random addons.\n{Fore.RESET}')
else:
self.game_dir = Path(self.config['settings']['game directory'])
if not self.game_dir.is_dir():
raise RuntimeError(f'{Fore.RED}\'{self.game_dir}\' is not a valid game directory.')
self.client = self.config['settings']['client'].lower()
# codes to filter the latest files page for specific game version
self.filters = {
'classic': '1738749986%3A67408',
'retail': '1738749986%3A517'
}
clients = ['classic', 'retail']
self.addons = []
self.addons_len = 0
self.size = 0.0
if self.client in clients:
self._collect_addons(self.client)
else:
if ',' in self.client:
client_list = list(set(self.client.split(',')))
elif self.client in ['both', 'all']:
client_list = clients
else:
raise RuntimeError(f'{Fore.RED}Invalid game version specified. \'{self.client}\''
f' is not accepted. Must be either classic, retail or both.')
for client in client_list:
client = client.strip()
if client in clients and len(client) > 0:
self._addon_dir(client) # early check if client is installed
self._collect_addons(client)
self.addons_len = len(self.addons)
if self.addons_len == 0:
raise RuntimeError(f'{Fore.RED}No addons found in [{self.client}] section of the configuration file.')
self.cfs = cloudscraper.create_scraper()
self._main()
def _collect_addons(self, client):
for name, last_update in self.config.items(client):
if self.testing:
last_update = 0. if randrange(1, 100) <= 25 else time()
elif not last_update:
last_update = 0.
self.addons.append(Addon(name=name, client=client, last_update=float(last_update)))
def _find_update(self, addon):
url = f'{self.base_url}/wow/addons/{addon.name}/files/all?filter-game-version={self.filters[addon.client]}'
r = self.cfs.get(url)
check_response_status(r)
soup = bs(r.text, 'html.parser')
rows = soup.find_all('tr')
with print_lock:
self._print_looking_for_update(i=idx.value)
idx.value += 1
for row in rows[1:]:
cols = row.find_all('td')
release_type = cols[0].text.strip()
if release_type in self.allowed_release_types.upper():
last_update_curse = int(cols[3].find('abbr').get('data-epoch'))
if last_update_curse > addon.last_update:
addon.file_url = cols[1].find('a')['href']
addon.latest_file = last_update_curse
return addon
def _update_addon(self, addon):
addon_start = time()
out_path = self.cache_dir / f'{addon.client}_{addon.name}.zip'
r = self.cfs.get(f'{self.base_url}{addon.file_url}')
check_response_status(r)
soup = bs(r.text, 'html.parser')
a_tag_buttons = soup.find_all('a', {'class': 'button button--hollow'})
for a_tag in a_tag_buttons:
url = a_tag.get('href')
if url.startswith(f'/wow/addons/{addon.name}/download/'):
zip_file = self.cfs.get(f'{self.base_url}{url}/file')
check_response_status(zip_file)
with open(out_path, 'wb') as f:
f.write(zip_file.content)
break
ZipFile(out_path).extractall(self._addon_dir(addon.client))
zip_size = out_path.stat().st_size / 1000000
return addon, zip_size, addon_start
def _main(self):
num_workers = cpu_count() * 2
init()
start = time()
shared_idx = Value('i', 0)
global_print_lock = Lock()
# check for latest versions
with Pool(processes=min(num_workers, self.addons_len),
initializer=init_globals,
initargs=(shared_idx, global_print_lock)) as p:
it = p.imap_unordered(self._find_update, self.addons)
arr = []
while True:
try:
arr.append(it.next(timeout=self.timeout))
except StopIteration:
break
except mpTimeoutError:
self.worker_timed_out = True
continue
# first filter out NoneTypes, then return only the outdated addons
outdated = list(filter(lambda x: x and x.outdated, arr))
eol = '\n\n' if len(outdated) == 0 else f' ({round(time() - start, ndigits=2)}s)\n\n'
self._print_looking_for_update(eol=eol)
outdated_len = len(outdated)
if outdated_len == 0:
print(f'{Fore.CYAN}=>{Fore.RESET} All addons are up-to-date! '
f'We\'re done here! ({round(time() - start, ndigits=2)}s)')
exit(0)
else:
cols = {
'classic': Fore.RED,
'retail': Fore.LIGHTGREEN_EX
}
# sort addons by client first, then by name
addons_sorted = [[a.name, a.client] for a in sorted(outdated, key=lambda x: (x.client, x.name))]
colored_names = ' '.join([f'{cols[c]}{n[:2]}{Fore.RESET}{n[2:]}' for n, c in addons_sorted])
pad = len(sorted(addons_sorted, key=lambda x: len(x[0]), reverse=True)[0][0])
print(f'{Style.BRIGHT}{Fore.CYAN}=>{Fore.RESET} Updating {Fore.YELLOW}'
f'{outdated_len if outdated_len > 1 else ""}{Fore.RESET}'
f'{" addons" if outdated_len > 1 else "addon"}:{Style.RESET_ALL} '
f'{colored_names}', Style.RESET_ALL, '\n')
# update out-of-date addons
with Pool(processes=min(num_workers, outdated_len)) as p:
it = p.imap_unordered(self._update_addon, outdated)
pb = tqdm(total=outdated_len,
desc=f' {pad * " "} ',
bar_format='{n_fmt}/{total_fmt} |{bar}|{desc}')
pb.set_lock(global_print_lock)
while True:
try:
addon, size, timestamp = it.next(timeout=self.timeout)
desc = f' {addon.name + (pad - len(addon.name)) * " "}{Fore.RESET} '
pb.set_description_str(desc=desc)
pb.update()
self.size += size
self.config.set(f'{addon.client}', addon.name, str(timestamp))
except StopIteration:
pb.close()
break
except mpTimeoutError:
raise RuntimeError(f'{Fore.RED}Something went wrong while installing one or more addons. '
f'Rerun the script to make sure everything is up-to-date.{Fore.RESET}')
if not self.testing:
with open(self.config_file, 'w') as f:
self.config.write(f)
msg = f'\nsummary: {round(time() - start, ndigits=2)}s, {round(self.size, ndigits=2)}MB'
if self.worker_timed_out:
msg += '\nSome worker/s timed out. Run the program again to be certain everything is up-to-date!'
print(msg)
deinit()
def _addon_dir(self, client):
addon_dir = self.game_dir / f'_{client}_' / 'Interface' / 'AddOns'
if not addon_dir.is_dir():
if self.testing:
print(f'{Fore.YELLOW}### Creating addon directory for testing at \'{addon_dir}\'.{Fore.RESET}')
addon_dir.mkdir(parents=True)
else:
raise RuntimeError(f'{Fore.RED}{client.capitalize()} addon folder not found at \'{addon_dir}\'.')
return addon_dir
def _print_looking_for_update(self, i=0, eol=' '):
anims = {
'dots': [' ', '. ', '.. ', '...'],
'braille': ['⠶', '⠦', '⠖', '⠲', '⠴']
}
anim = anims['braille']
symbol = anim[int(i / 2) % len(anim)]
print(f'\r{Style.BRIGHT}{Fore.BLUE}{symbol}{Fore.RESET}'
f' Checking for latest versions of {Fore.YELLOW}{self.addons_len}'
f'{Fore.RESET} {"addons" if self.addons_len > 1 else "addon"}.{Style.RESET_ALL}', end=eol)
def check_response_status(response):
if not response.ok:
response.raise_for_status()
def init_globals(shared_idx, lock):
global idx, print_lock
idx = shared_idx
print_lock = lock
class Addon:
def __init__(self, name=None, client=None, last_update=None, file_url=None, latest_file=None):
self.name = name
self.client = client
self.file_url = file_url
self.last_update = last_update
self.latest_file = latest_file
def __repr__(self):
return f'<{self.name}:{self.client}>'
def outdated(self):
if self.last_update is not None and self.latest_file is not None:
return self.last_update < self.latest_file
if __name__ == '__main__':
Updater(testing=False)