Advertisement
Guest User

VSmodAutoUpdater_rev2

a guest
Jan 24th, 2024
122
0
259 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | Fixit | 0 0
  1. import requests
  2. import json
  3. import os
  4. import wget
  5. import zipfile
  6.  
  7.  
  8. def separator():
  9.     print(f"{'_' * 50}")
  10.  
  11.  
  12. home_url = "https://mods.vintagestory.at"
  13. mod_api = "http://mods.vintagestory.at/api/mod"
  14.  
  15. f = open(".\\modsdir.txt", "r")
  16.  
  17. if f.read() == "":
  18.     f.close()
  19.     f = open(".\\modsdir.txt", "w")
  20.     mods_dir = input("What is your mod directory?")
  21.     f.write(mods_dir)
  22.     f.close()
  23.  
  24. f.close()
  25.  
  26. f = open(".\\modsdir.txt", "r")
  27. modpath = f.read()
  28. f.close()
  29.  
  30. path_out = ".\\output"
  31. if not os.path.isdir(path_out):
  32.     os.mkdir(path_out)
  33.     print("Creating output directory")
  34.  
  35. modlist = os.listdir(modpath)
  36.  
  37. mod_info_list = []
  38.  
  39. print("Mods in your folder: \n")
  40. for mod in modlist:
  41.     # Get modinfo.json data inside local mod-zipfile
  42.     if mod.endswith(".zip"):
  43.         get_zip = zipfile.ZipFile(os.path.join(modpath, mod), mode="r")
  44.         with get_zip.open("modinfo.json", mode="r") as mod_info:
  45.             mod_info_content = mod_info.read().decode("utf-8")
  46.             print(mod_info_content)
  47.             try:
  48.                 mod_info_json = json.loads(mod_info_content)
  49.                 if 'name' in mod_info_json:
  50.                     mod_info_list.append(mod_info_json)
  51.                     print(mod_info_json["name"])
  52.                 else:
  53.                     print("Schlüssel 'name' wurde in mod_info_json nicht gefunden")
  54.             except json.decoder.JSONDecodeError as e:
  55.                 print(f"Error parsing JSON for {mod}: {e}")
  56.  
  57. separator()
  58. print("\nChecking for updates...\n")
  59.  
  60. for mod in mod_info_list:
  61.     if 'modid' in mod:  # Check if 'modid' key exists
  62.         api_req = mod_api + "/" + str(mod["modid"])
  63.         # Get new mod info data from database to check version
  64.         mod_res = requests.get(api_req, headers={"Accept": "application/json"})
  65.  
  66.         res_modinfo = mod_res.json()["mod"]
  67.  
  68.         releases = res_modinfo["releases"][0]
  69.  
  70.         if releases["modversion"] != mod["version"]:
  71.             separator()
  72.             print("\n" + mod["name"] + " hat eine neuere Version!")
  73.             print("Herunterladen von:")
  74.  
  75.             file_id = releases["fileid"]
  76.             downlink = home_url + "/download?fileid=" + str(file_id)
  77.             print(downlink)
  78.             response = wget.download(downlink, out=path_out)
  79.             print("\nFertig! Überprüfen Sie den Ausgabeordner\n")
  80.             separator()
  81.         else:
  82.             print(mod["name"] + " ist die neueste Version!")
  83.     else:  # Key 'modid' does not exist
  84.         print(f"modid key does not exist for mod: {mod['name']}")
  85.  
  86. separator()
  87. input("\n\nDer Download ist abgeschlossen, drücken Sie die Eingabetaste, um zu beenden!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement