Skip to content

Commit 33321ec

Browse files
Update mods_data.py to use same mod sources as parallel download implementation. Modified script to try both https://ru.nyrds.net/rpd/mods2.json and https://nyrds.net/rpd/mods2.json URLs, using whichever responds first. Added better error handling, local file caching, and improved directory handling. Created initial mods2.json template file.
1 parent e8f7508 commit 33321ec

File tree

2 files changed

+101
-10
lines changed

2 files changed

+101
-10
lines changed

tools/mods/mods2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"known_mods": {
3+
"common": []
4+
},
5+
"info": {}
6+
}

tools/mods/mods_data.py

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,113 @@
33
import os
44
import requests
55
import zipfile
6+
from urllib.parse import urlparse
67

78
properties = ["Mod_Author", "Mod_Link","Mod_Email","Mod_Name","Mod_Description"]
89

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+
]
1015

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")
1459

1560
print("Downloading mods...")
1661
for cat in mods['known_mods']:
1762
for mod in mods['known_mods'][cat]:
1863
print(mod)
1964

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
21105

22-
with open(mod['name'], 'wb') as f:
23-
f.write(response.content)
106+
except Exception as e:
107+
print(e)
24108

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")
28113

29114
print("Parsing...")
30115
for dir in os.listdir("mods"):

0 commit comments

Comments
 (0)