Skip to content

Instantly share code, notes, and snippets.

@C-ffeeStain
Created April 11, 2021 14:36
Show Gist options
  • Save C-ffeeStain/a010301c2e6c22e2dbcf15086389a69d to your computer and use it in GitHub Desktop.
Save C-ffeeStain/a010301c2e6c22e2dbcf15086389a69d to your computer and use it in GitHub Desktop.
import requests,os,winreg,shutil
from pprint import pprint
from pathlib import Path
if not os.path.exists('.files'):
os.mkdir('.files')
count = requests.get('https://spelunky.fyi/api/mods', headers = {'Authorization': 'Token e0d3a9d49771c1d98273e9e1b209d44d50146c4f'}).json()['count']
def check_registry_for_spel2():
if winreg is None:
return None
programs = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall')
index = 0
while True:
try:
keyname = winreg.EnumKey(programs, index)
index += 1
except OSError:
return None
try:
subkey = winreg.OpenKey(programs, keyname)
name, _ = winreg.QueryValueEx(subkey, "DisplayName")
if name == "Spelunky 2":
return Path(winreg.QueryValueEx(subkey, "InstallLocation")[0])
except OSError:
continue
return None
def install_mod_by_name(name):
offset = int(round(count, -2))
print('Searching {} mods...'.format(count))
for i in range(int(count / 50)):
offset -= 50
r = requests.get('https://spelunky.fyi/api/mods/?offset=' + str(offset), headers = {'Authorization': 'Token e0d3a9d49771c1d98273e9e1b209d44d50146c4f'})
mods = [mod for mod in reversed(r.json()['results'])]
for mod in mods:
if mod['name'].lower().startswith(name.lower()):
print('Found mod, downloading latest file... ')
download_url = requests.get(mod['self_url'], allow_redirects=True, headers = {'Authorization': 'Token e0d3a9d49771c1d98273e9e1b209d44d50146c4f'}).json()['mod_files'][0]['download_url']
r = requests.get(download_url, allow_redirects=True)
filename = r.url.split('/')[-1]
if filename.endswith('zip'): action = 'wb'
else: action = 'w'
with open('.files/' + filename, action) as f:
f.write(r.content)
if action == 'wb':
print('Downloaded file is a zip file, extracting...')
s2_dir = check_registry_for_spel2()
if not os.path.exists(s2_dir / ('Mods/Packs/' + filename.split('.')[0])): os.mkdir('Mods/Packs/' + filename.split('.')[0])
shutil.unpack_archive('.files/' + filename, Path(s2_dir) / ('Mods/Packs/' + filename.split('.')[0])) if s2_dir is not None else print('no s2 dir')
shutil.rmtree('.files')
return
install_mod_by_name('shoppie ghosts')
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment