|
| 1 | +import urllib.request |
| 2 | +import requests |
| 3 | +import re |
| 4 | +import platform |
| 5 | +import zipfile |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import traceback |
| 9 | +import shutil |
| 10 | + |
| 11 | +from packaging import version |
| 12 | +from bs4 import BeautifulSoup # beautifulsoup4 |
| 13 | +from tqdm import tqdm |
| 14 | + |
| 15 | +LUA_EXTENSION_URL = "https://raw.githubusercontent.com/michael-mueller-git/Python-Funscript-Editor/main/contrib/Installer/assets/main.lua" |
| 16 | +FUNSCRIPT_GENERATOR_RELEASE_URL = "https://github.com/michael-mueller-git/Python-Funscript-Editor/releases" |
| 17 | +OFS_EXTENSION_DIR = os.path.expandvars(r'%APPDATA%\OFS\OFS_data\extensions') |
| 18 | + |
| 19 | + |
| 20 | +class DownloadProgressBar(tqdm): |
| 21 | + def update_to(self, b=1, bsize=1, tsize=None): |
| 22 | + if tsize is not None: |
| 23 | + self.total = tsize |
| 24 | + self.update(b * bsize - self.n) |
| 25 | + |
| 26 | + |
| 27 | +def download_url(url, output_path): |
| 28 | + print("Download latest release of Python-Funscript-Editor") |
| 29 | + with DownloadProgressBar(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t: |
| 30 | + urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to) |
| 31 | + |
| 32 | + |
| 33 | +def error(msg): |
| 34 | + print("ERROR: " + msg) |
| 35 | + sys.exit() |
| 36 | + |
| 37 | + |
| 38 | +def is_ofs_installed(): |
| 39 | + if not os.path.exists(OFS_EXTENSION_DIR): |
| 40 | + error("OFS is not installed. Please download and install OFS. Befor running this installer open OFS once!") |
| 41 | + |
| 42 | + |
| 43 | +def get_download_urls(): |
| 44 | + try: |
| 45 | + html_text = requests.get(FUNSCRIPT_GENERATOR_RELEASE_URL).text |
| 46 | + download_urls = { version.parse(re.search(r'v[^/]*', x).group().lower().replace("v", "")) : "https://github.com" + x \ |
| 47 | + for x in [link.get('href') for link in BeautifulSoup(html_text, 'html.parser').find_all('a') \ |
| 48 | + if link.get('href').endswith(".zip") and "/releases/" in link.get('href')] |
| 49 | + } |
| 50 | + latest = max(download_urls) |
| 51 | + return download_urls, latest |
| 52 | + except: |
| 53 | + error("Download URL not found (" + FUNSCRIPT_GENERATOR_RELEASE_URL + ")") |
| 54 | + |
| 55 | + |
| 56 | +def is_latest_version_installed(version_file, version): |
| 57 | + if os.path.exists(version_file): |
| 58 | + with open(version_file, 'r') as f: |
| 59 | + if str(f.read()).lower() == "v"+str(version): |
| 60 | + print("You have already the latest version installed") |
| 61 | + sys.exit() |
| 62 | + |
| 63 | + |
| 64 | +def update(download_urls, latest): |
| 65 | + extension_dir = os.path.join(OFS_EXTENSION_DIR, "Funscript Generator Windows") |
| 66 | + zip_file = os.path.join(extension_dir, "funscript-editor-v" + str(latest) + ".zip") |
| 67 | + dest_dir = os.path.join(os.path.dirname(zip_file), "funscript-editor") |
| 68 | + version_file = os.path.join(os.path.dirname(zip_file), "funscript-editor", "funscript_editor", "VERSION.txt") |
| 69 | + |
| 70 | + is_latest_version_installed(version_file, latest) |
| 71 | + |
| 72 | + trial = 0 |
| 73 | + while True: |
| 74 | + os.makedirs(os.path.dirname(zip_file), exist_ok = True) |
| 75 | + if not os.path.exists(zip_file): |
| 76 | + download_url(download_urls[latest], zip_file) |
| 77 | + |
| 78 | + try: |
| 79 | + os.makedirs(dest_dir + "_update", exist_ok = True) |
| 80 | + with zipfile.ZipFile(zip_file) as zf: |
| 81 | + for member in tqdm(zf.infolist(), desc='Extracting '): |
| 82 | + zf.extract(member, dest_dir + "_update") |
| 83 | + break |
| 84 | + except: |
| 85 | + trial += 1 |
| 86 | + if trial < 2: |
| 87 | + print("Local Version is corrupt redownloading") |
| 88 | + os.remove(zip_file) |
| 89 | + continue |
| 90 | + else: |
| 91 | + error("Installation failed") |
| 92 | + |
| 93 | + if os.path.exists(dest_dir): |
| 94 | + try: shutil.rmtree(dest_dir) |
| 95 | + except: error('Error while deleting old Version (Is OFS currenty running?)') |
| 96 | + |
| 97 | + shutil.move(dest_dir + "_update", dest_dir) |
| 98 | + |
| 99 | + with open(os.path.join(extension_dir, "main.lua"), "wb") as f: |
| 100 | + f.write(requests.get(LUA_EXTENSION_URL).content) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + try: |
| 105 | + if platform.system() != "Windows": |
| 106 | + error("This installer only work on Windows") |
| 107 | + |
| 108 | + is_ofs_installed() |
| 109 | + download_urls, latest = get_download_urls() |
| 110 | + update(download_urls, latest) |
| 111 | + |
| 112 | + print("Installation completed") |
| 113 | + |
| 114 | + except SystemExit as e: |
| 115 | + input() |
| 116 | + |
| 117 | + except: |
| 118 | + traceback.print_exc() |
| 119 | + input() |
0 commit comments