|
3 | 3 | import os
|
4 | 4 | import requests
|
5 | 5 | import zipfile
|
| 6 | +from urllib.parse import urlparse |
6 | 7 |
|
7 | 8 | properties = ["Mod_Author", "Mod_Link","Mod_Email","Mod_Name","Mod_Description"]
|
8 | 9 |
|
9 |
| -#mods_json = urllib.request.urlopen("https://nyrds.github.io/NYRDS/mods2.json").read().decode() |
| 10 | +# Try both URLs - first one to respond successfully will be used |
| 11 | +urls_to_try = [ |
| 12 | + "https://ru.nyrds.net/rpd/mods2.json", |
| 13 | + "https://nyrds.net/rpd/mods2.json" |
| 14 | +] |
10 | 15 |
|
11 |
| -mods_json = open("mods2.json").read() |
12 |
| -mods = json.loads(mods_json) |
13 |
| -mods['info'] = {} |
| 16 | +mods = None |
| 17 | + |
| 18 | +# First try to load from local file if it exists |
| 19 | +if os.path.exists("mods2.json"): |
| 20 | + print("Loading mods list from local file") |
| 21 | + try: |
| 22 | + with open("mods2.json", 'r') as f: |
| 23 | + mods = json.load(f) |
| 24 | + print("Successfully loaded mods list from local file") |
| 25 | + except Exception as e: |
| 26 | + print(f"Failed to load from local file: {e}") |
| 27 | + |
| 28 | +# If local file failed or doesn't exist, try downloading |
| 29 | +if mods is None: |
| 30 | + print("Attempting to download mods list from remote servers...") |
| 31 | + for url in urls_to_try: |
| 32 | + try: |
| 33 | + print(f"Trying to download mods list from {url}") |
| 34 | + # Disable proxies to avoid SOCKS issues |
| 35 | + response = requests.get(url, timeout=10, proxies={}) |
| 36 | + response.raise_for_status() # Raise an exception for bad status codes |
| 37 | + mods = json.loads(response.text) |
| 38 | + print(f"Successfully downloaded mods list from {url}") |
| 39 | + |
| 40 | + # Save to local file for future use |
| 41 | + with open("mods2.json", 'w') as f: |
| 42 | + json.dump(mods, f, indent=2, ensure_ascii=False) |
| 43 | + print("Saved mods list to local file") |
| 44 | + break |
| 45 | + except Exception as e: |
| 46 | + print(f"Failed to download from {url}: {e}") |
| 47 | + continue |
| 48 | + |
| 49 | +if mods is None: |
| 50 | + print("Failed to download mods list from all URLs and no local file available") |
| 51 | + exit(1) |
| 52 | + |
| 53 | +if 'info' not in mods: |
| 54 | + mods['info'] = {} |
| 55 | + |
| 56 | +# Create mods directory if it doesn't exist |
| 57 | +if not os.path.exists("mods"): |
| 58 | + os.makedirs("mods") |
14 | 59 |
|
15 | 60 | print("Downloading mods...")
|
16 | 61 | for cat in mods['known_mods']:
|
17 | 62 | for mod in mods['known_mods'][cat]:
|
18 | 63 | print(mod)
|
19 | 64 |
|
20 |
| - response = requests.get(mod['url'], timeout=5) |
| 65 | + try: |
| 66 | + response = requests.get(mod['url'], timeout=15, proxies={}) |
| 67 | + response.raise_for_status() |
| 68 | + |
| 69 | + with open(mod['name'], 'wb') as f: |
| 70 | + f.write(response.content) |
| 71 | + |
| 72 | + with zipfile.ZipFile(mod['name'], 'r') as zip_ref: |
| 73 | + print("Extracting", mod['name']) |
| 74 | + zip_ref.extractall(f"./mods") |
| 75 | + except Exception as e: |
| 76 | + print(f"Failed to download/extract {mod['name']}: {e}") |
| 77 | + |
| 78 | +print("Parsing...") |
| 79 | +if os.path.exists("mods") and os.listdir("mods"): |
| 80 | + for dir in os.listdir("mods"): |
| 81 | + print(dir) |
| 82 | + |
| 83 | + for lang in ["en","ru","fr","es"]: |
| 84 | + try: |
| 85 | + mod_data = {} |
| 86 | + |
| 87 | + with open(f"mods/{dir}/strings_{lang}.json", 'rb') as strings_lang: |
| 88 | + for line in strings_lang: |
| 89 | + str = None |
| 90 | + try: |
| 91 | + str = codecs.decode(line, encoding='utf_8_sig') |
| 92 | + str = str.strip(',\n\r') |
| 93 | + data = json.loads(str) |
| 94 | + for prop in properties: |
| 95 | + if prop == data[0]: |
| 96 | + mod_data[data[0]] = data[1] |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + print(e, str) |
| 100 | + |
| 101 | + if mod_data['Mod_Name'] not in mods['info']: |
| 102 | + mods['info'][mod_data['Mod_Name']] = {} |
| 103 | + |
| 104 | + mods['info'][mod_data['Mod_Name']][lang] = mod_data |
21 | 105 |
|
22 |
| - with open(mod['name'], 'wb') as f: |
23 |
| - f.write(response.content) |
| 106 | + except Exception as e: |
| 107 | + print(e) |
24 | 108 |
|
25 |
| - with zipfile.ZipFile(mod['name'], 'r') as zip_ref: |
26 |
| - print("Extracting", mod['name']) |
27 |
| - zip_ref.extractall(f"./mods") |
| 109 | + with open("mods2.json", 'w') as mods_json: |
| 110 | + mods_json.write(json.dumps(mods,indent=2,ensure_ascii=False)) |
| 111 | +else: |
| 112 | + print("No mods directory or mods directory is empty, skipping parsing") |
28 | 113 |
|
29 | 114 | print("Parsing...")
|
30 | 115 | for dir in os.listdir("mods"):
|
|
0 commit comments