forked from TrendingTechnology/VimmsDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
513 lines (446 loc) · 18.1 KB
/
script.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""The Vimms-DL Tool"""
from prettytable import PrettyTable
import zipfile
import re
import sys
import os
from typing import List
import py7zr
from requests.models import Response
import requests
from bs4 import BeautifulSoup
from src.helpers import models
from src import helpers
def get_rom_download_url(url: str) -> str:
"""Gets the Download ID for the a specific ROM from the ROMs page url"""
download_id: str = ''
try:
page: Response = requests.get('https://vimm.net/' + url)
soup: BeautifulSoup = BeautifulSoup(page.content, 'html.parser')
result = soup.find(id='download_form')
result = result.find(attrs={'name': 'mediaId'})
download_id: str = result['value']
except:
e = sys.exc_info()[0]
print('Failed on getting ROM ID')
print(e)
return download_id
def get_sub_section_letter_from_str(subsection: str) -> str:
"""Returns the subsection letter to get the downloaded ROM to the\
correct alphanumeric directory"""
number: str = '§ion=number'
if number in subsection.lower():
return 'number'
else:
return subsection[-1]
def get_section_of_roms(section: str) -> List[models.ROM]:
"""Gets a section of ROM home page URIs from a system category"""
roms: List[models.ROM] = []
try:
page: Response = requests.get('https://vimm.net/vault/' + section)
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find('table', {'class': 'rounded centered cellpadding1 hovertable striped'})
rows = table.select('tr')
for row in rows:
title_link = row.select_one('td[style="width:auto"] > a[href*="/vault/"]')
if title_link:
rom_title = title_link.text
rom_uri = title_link['href']
rom = models.ROM(rom_title, rom_uri)
roms.append(rom)
except:
e = sys.exc_info()[0]
return roms
def get_every_system_roms() -> List[models.BulkSystemROMS]:
every_rom: List[models.BulkSystemROMS] = []
for i in range(0, 17):
system_roms: models.BulkSystemROMS = get_all_system_roms(
helpers.selection_to_uri(helpers.get_selection_from_num(i)))
every_rom.append(system_roms)
return every_rom
def get_all_system_roms(system: str) -> models.BulkSystemROMS:
"""Used in bulk mode to get the home page URI for every rom on a system"""
print('Getting a list of roms for the ' + system)
section_roms: List[models.SectionofROMs] = []
section_urls: List[str] = [
f'?p=list&system={system}§ion=number', f'{system}/a',
f'{system}/b', f'{system}/c', f'{system}/d', f'{system}/e',
f'{system}/f', f'{system}/g', f'{system}/h', f'{system}/i',
f'{system}/j', f'{system}/k', f'{system}/l', f'{system}/m',
f'{system}/n', f'{system}/o', f'{system}/p', f'{system}/q',
f'{system}/r', f'{system}/s', f'{system}/t', f'{system}/u',
f'{system}/v', f'{system}/w', f'{system}/x', f'{system}/y',
f'{system}/z'
]
for x in section_urls:
roms: List[models.ROM] = get_section_of_roms(x)
section: models.SectionofROMs = models.SectionofROMs(x, roms)
section_roms.append(section)
system_roms: models.BulkSystemROMS = models.BulkSystemROMS(
section_roms, system)
return system_roms
def download_file(page_url: str, download_url: str, path: str) -> str:
"""Downloads one rom from the uri, downloadid\
downloads to the path director"""
x: int = 0
filename: str = ''
while True:
headers: dict[str, str] = {
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image' +
'/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding':
'gzip, deflate, br',
'Connection':
'keep-alive',
'User-Agent':
helpers.get_random_ua(),
'Referer':
f'https://vimm.net/vault{page_url}'
}
file: Response = requests.get(
f'https://download2.vimm.net/download/?mediaId={download_url}',
headers=headers,
allow_redirects=True)
if file.status_code == 200:
filename = file.headers['Content-Disposition']
filenames: List[str] = re.findall(r'"([^"]*)"', filename)
filename = filenames[0]
full_path = os.path.join(path, filename)
open(full_path, 'wb').write(file.content)
print('Downloaded ' + filename + '!')
break
if x == 4:
print(f'5 Requests made to {download_url} and failed')
break
if file.status_code != 200:
x += 1
continue
return filename
def get_search_selection(config: models.Config) -> models.Config:
"""Gets search criteria for search mode"""
search_selection: models.SearchSelection = models.SearchSelection()
print('\nPlease select what system you want to search')
print('Press Enter to do a general site wide search')
helpers.print_console_list()
while True:
user_input: str = sys.stdin.readline()
try:
if user_input == '\n':
search_selection.System = 'general'
config.Query.SearchSelections = search_selection
break
if not (int(user_input) > 17 or int(user_input) < 0):
search_selection.System = \
helpers.get_selection_from_num(int(user_input))
config.Query.SearchSelections = search_selection
break
else:
print('Not a selection')
print('Please select a value from the list')
except ValueError:
print('Please select a value from the list')
continue
print('Input what rom you want to search for')
search_selection.Query = sys.stdin.readline()
return config
def get_system_search_section(
search_selection: models.SearchSelection) -> List[models.ROM]:
"""Gets a section of roms using system search from the search selection"""
roms: List[models.ROM] = []
try:
page = requests.get(helpers.get_search_url(search_selection))
soup: BeautifulSoup = BeautifulSoup(page.content, 'html.parser')
table = soup.find('table', {'class': 'rounded centered cellpadding1 hovertable striped'})
rows = table.select('tr')
for row in rows:
title_link = row.select_one('td[style="width:auto"] > a[href*="/vault/"]')
if title_link:
rom_title = title_link.text
rom_uri = title_link['href']
rom = models.ROM(rom_title, rom_uri)
roms.append(rom)
except BaseException:
e = sys.exc_info()[0]
print('Failed on system search section')
print(e)
return roms
def get_general_search_section(
search_selection: models.SearchSelection) -> List[models.ROM]:
"""Gets a section of roms when using general search from the search selection"""
roms: List[models.ROM] = []
try:
page = requests.get(helpers.get_search_url(search_selection))
soup: BeautifulSoup = BeautifulSoup(page.content, 'html.parser')
table = soup.find('table', {'class': 'rounded centered cellpadding1 hovertable striped'})
rows = table.find_all('tr')
for row in rows[1:]: # skip the header row
cols = row.find_all('td')
if len(cols) >= 4:
rom_platform = cols[0].text.strip()
title_link = cols[1].find('a', href=True)
rom_title= title_link.text.strip()
rom_uri = title_link['href']
rom = models.ROM(rom_title, rom_uri, rom_platform)
roms.append(rom)
except BaseException:
e = sys.exc_info()[0]
print('Failed getting general search section')
print(e)
return roms
def get_program_mode() -> models.Config:
"""Gets input from user to go into either (Bulk/Search) mode"""
config: models.Config = models.Config()
print(
'\nWould you like to bulk download roms for systems or search for specific roms? (B/s)'
)
print("For bulk mode use 'b' and search mode use 's'")
print('Default is \'b\'')
while True:
user_input: str = sys.stdin.readline()
if user_input == '\n':
config.BulkMode = True
break
if user_input.lower() == 'b\n':
config.BulkMode = True
break
if user_input.lower() == 's\n':
config.SearchMode = True
break
else:
print('Not a selection')
print('Please Select B/s')
continue
return config
def get_bulk_selections(config: models.Config) -> models.Config:
"""Gets input in bulk mode if the user wants to only download specific consoles"""
print("Press Enter to download all of Vimm's roms or select from the" +
" following of what systems you would like to download")
print('Enter \'d\' when finished if choosing specific consoles\n')
helpers.print_console_list()
while True:
user_input: str = sys.stdin.readline()
if user_input == '\n' and len(config.Selections) == 0:
config.All = True
break
if user_input == 'd\n':
break
try:
if not (int(user_input) > 17 or int(user_input) < 0):
config.Selections.append(int(user_input))
else:
print('Not a selection')
print('Please select a value from the list')
except ValueError:
print('Please select a value from the list')
continue
return config
def get_extraction_status(config: models.Config) -> models.Config:
"""Used in Bulk and Search mode to check if user wants to \
extract and delete downloaded ROM archives"""
print(
'Would you like to automatically extract and delete archives after download? (Y/n)'
)
print('Default is \'y\'')
while True:
user_input: str = sys.stdin.readline()
if user_input == '\n':
config.Extract = True
break
if user_input.lower() == 'y\n':
config.Extract = True
break
if user_input.lower() == 'n\n':
config.Extract = False
break
if (user_input.lower() != 'n\n') and (user_input.lower() != 'y\n'):
print('Not a selection')
print('Please Select Y/n')
continue
return config
def print_general_search(roms: List[models.ROM]):
table = PrettyTable()
table.field_names = ["Selection Number", "System", "ROM"]
count = 0
print(
"\nSelect which roms you would like to download and then enter 'd'\n")
for x in roms:
table.add_row([count, x.Console, x.Name])
count += 1
table.align = "l"
table.right_padding_width = 0
print(table)
def print_system_search(roms: List[models.ROM]):
"""Prints the results from a system search"""
table = PrettyTable()
table.field_names = ["Selection Number", "ROM"]
count: int = 0
print(
'\nSelect which roms you would like to download and then enter \'d\'')
for x in roms:
table.add_row([count, x.Name])
count += 1
table.align = "l"
table.right_padding_width = 0
print(table)
def print_search_results(roms: List[models.ROM]) -> None:
"""Prints the returned search results from the users query"""
if roms[0].Console != '':
print_general_search(roms)
else:
print_system_search(roms)
def get_search_result_input(roms: List[models.ROM]) -> List[int]:
"""Used to get input in search mode for what ROMs the user wants to download"""
download_sel_roms: List[int] = []
print(
'\nSelect which roms you would like to download and then enter \'d\'')
while True:
user_input = sys.stdin.readline()
if user_input == '\n':
print('Please select a rom or press \'q\' to quit program')
continue
if user_input == 'q\n':
exit()
if user_input == 'd\n':
break
try:
if not (int(user_input) > len(roms) - 1 or int(user_input) < 0):
download_sel_roms.append(int(user_input))
else:
print('Not a selection')
print('Please select a value from the list')
except ValueError:
print('Please select a value from the list')
continue
return download_sel_roms
def download_search_results(downloads: List[int], roms: List[models.ROM],
config: models.Config) -> None:
"""Downloads the users specified roms in search mode"""
for x in downloads:
download_name = download_file(roms[x].URI,
get_rom_download_url(roms[x].URI), '.')
if config.Extract:
extract_and_delete_search_results('.',download_name)
def extract_file(path: str, name: str) -> None:
"""Extracts the downloaded archives"""
full_path: str = os.path.join(path, name)
base_filename: List[str] = re.findall(r'(.+?)(\.[^.]*$|$)', name)
file_name: str = str(base_filename[0][0])
file_type = re.findall(r'(zip|7z)', full_path)
if str(file_type[0]).lower() == 'zip':
with (zipfile.ZipFile(full_path, 'r')) as z:
dir_path = create_directory_for_rom(file_name, path)
z.extractall(os.path.join(dir_path))
if str(file_type[0]).lower() == '7z':
with py7zr.SevenZipFile(full_path, mode='r') as z:
dir_path = create_directory_for_rom(file_name, path)
z.extractall(dir_path)
def delete_file(path: str, name: str) -> None:
"""Deletes the archives"""
os.remove(os.path.join(path, name))
def check_if_need_to_re_search() -> bool:
"""Gets user input to research if query didn't return wanted results"""
search: bool = False
print('Do you want to search again?(y/N)')
while True:
user_input = sys.stdin.readline()
if user_input == '\n':
break
if user_input.lower() == 'y\n':
search = True
break
if user_input.lower() == 'n\n':
break
if (user_input.lower() != 'n\n') and user_input.lower() != 'y\n':
print('Not a selection')
print('Please Select y/N')
continue
return search
def run_search(config: models.Config) -> List[models.ROM]:
"""Runs the correct search method to get a list of the search results"""
if helpers.is_general_search(config.Query.SearchSelections):
roms: List[models.ROM] = get_general_search_section(
config.Query.SearchSelections)
return roms
roms: List[models.ROM] = get_system_search_section(
config.Query.SearchSelections)
return roms
def create_directory_for_rom(name: str, path: str) -> str:
"""Used to create the directory for the ROMs archived files to\
be extracted to"""
new_path: str = os.path.join(path, name)
os.mkdir(new_path)
return new_path
def run_search_loop(config: models.Config) -> None:
"""Main loop for the search program"""
while True:
config = get_search_selection(config)
roms: List[models.ROM] = run_search(config)
print_search_results(roms)
restart: bool = check_if_need_to_re_search()
if restart:
continue
downloads: List[int] = get_search_result_input(roms)
download_search_results(downloads, roms, config)
print('Done!')
restart = check_if_need_to_re_search()
if restart:
continue
else:
exit()
# TODO FIX THREADING
def extract_and_delete_search_results(path: str, download: str) -> None:
"""Used to extract and delete the archives in search mode"""
extract_file(path, download)
print('Finished extracting ' + download + '!')
delete_file(path, download)
def get_user_sel_bulk_roms(
config: models.Config) -> List[models.BulkSystemROMS]:
selected_bulk: List[models.BulkSystemROMS] = []
for i in config.Selections:
system_roms: models.BulkSystemROMS =\
get_all_system_roms(helpers.selection_to_uri(helpers.get_selection_from_num(i)))
selected_bulk.append(system_roms)
return selected_bulk
def download_bulk_roms(config: models.Config,
roms: List[models.BulkSystemROMS]):
for system in roms:
print('Starting to download all roms for the ' + system.System + '!')
for section in system.Sections:
for rom in section.ROMS:
download_name = download_file(rom.URI,
get_rom_download_url(rom.URI),
section.Path)
if config.Extract:
extract_and_delete_search_results(section.Path,download_name)
def run_selected_program(config: models.Config) -> None:
"""Runs selected program"""
if config.BulkMode:
config = get_bulk_selections(config)
if config.All:
all_roms: List[models.BulkSystemROMS] = get_every_system_roms()
all_roms: List[
models.BulkSystemROMS] = helpers.generate_path_to_bulk_roms(
all_roms)
helpers.create_directory_structure(config, os.getcwd())
download_bulk_roms(config, all_roms)
exit()
else:
user_selected_bulk: List[
models.BulkSystemROMS] = get_user_sel_bulk_roms(config)
user_selected_bulk: List[
models.BulkSystemROMS] = helpers.generate_path_to_bulk_roms(
user_selected_bulk)
helpers.create_directory_structure(config, os.getcwd())
download_bulk_roms(config, user_selected_bulk)
exit()
if config.SearchMode:
run_search_loop(config)
def main() -> None:
"""Programs main method"""
helpers.print_welcome()
config: models.Config = get_program_mode()
config: models.Config = get_extraction_status(config)
run_selected_program(config)
if __name__ == '__main__':
main()