From 6b598c8a00bde2ec4b8eb2eb08ca8af6c92b7545 Mon Sep 17 00:00:00 2001 From: Alexander Hayden Date: Sat, 5 Oct 2019 22:44:13 -0400 Subject: SELENIUM. Fix downloads being broken --- update.py | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'update.py') diff --git a/update.py b/update.py index 3404175..2727306 100755 --- a/update.py +++ b/update.py @@ -5,6 +5,7 @@ import os import sys import hashlib import shutil +import regex as re import requests @@ -91,21 +92,26 @@ def apply_updates(args): print("Populating version File...") mods = read_file(args.filename) print("Getting new versions of all mods...") + ffx = firefox() with open(args.version_file, 'w') as f: f.write('# Format: \n') f.write("#VERSION " + str(VERSION + 1) + "\n") for mod in mods: print("Fetching {mod[0]}...".format(mod=mod)) - resp = requests.get(mod[1]) + url = find_cdn(ffx, mod[1]) + if url is None: + print('Failed to fetch {mod[0]}!'.format(mod=mod)) + continue + resp = requests.get(url) hsh = hashlib.sha1(resp.content).hexdigest() f.write('{mod[0]} {hsh} {resp.url}\n'.format(mod=mod, hsh=hsh, resp=resp)) + ffx.close() print() print("Done!") print("Updates applied to {args.version_file}".format(args=args)) print("New pack version is " + str(VERSION + 1)) print("[!] No mods were installed. To update your mods folder, run 'update.py install'") - # Find if any updates are available def check_updates(args): print("Checking for updates to version " + str(VERSION) + "...") @@ -115,20 +121,53 @@ def check_updates(args): num_updates = 0 print("Checking updates...") + ffx = firefox() + for mod in latest: print("Checking for updates to {mod[0]}...".format(mod=mod), end="") sys.stdout.flush() # takes care of line-buffered terminals - resp = requests.get(mod[1]) - if resp.url in old_urls: + url = find_cdn(ffx, mod[1]) + if url in old_urls: print(" No updates") else: - print(" Found update: " + resp.url.split('/')[-1]) + print(" Found update: " + url.split('/')[-1]) num_updates += 1 + ffx.close() print("Finished checking for updates. {num} mods can be updated".format(num=num_updates)) if num_updates >= 0: print("Run 'python update.py apply_updates' to create a new version with these updates applied.") +# Use selenium to find curseforge CDN links around cloudflare +def find_cdn(ffx, url): + try: + ffx.get(url + '/download') + page_src = ffx.page_source + dl = re.search('Elerium.PublicProjectDownload.countdown\(".*?"\);', page_src) + if not dl: + return None + dl = re.search('\d+', dl.group(0).split('"')[1]) + dl = dl.group(0) + four = str(int(dl[:4])) + three = str(int(dl[4:])) + + ffx.get(url + '/files') + elements = ffx.find_elements_by_tag_name('tr') + file_name = elements[1].find_element_by_tag_name('a').text + return 'https://media.forgecdn.net/files/{four}/{three}/{jar}'.format(four=four,three=three,jar=file_name) + + except: + return None + +def firefox(): + print("Starting Selenium...") + try: + from selenium.webdriver import Firefox + except: + print("Applying updates requires the `selenium` package") + os.exit(0) + return Firefox() + COMMAND_MAP = { 'install': install, 'apply_updates': apply_updates, -- cgit v1.2.3