|
1 | | -import json |
| 1 | +import jstyleson |
2 | 2 | import glob |
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import urllib.request |
| 6 | +from io import StringIO |
6 | 7 |
|
7 | 8 | from ignore_json import ignore |
8 | 9 |
|
9 | 10 | for filename in glob.glob(os.path.join('.', '*.json')): |
10 | 11 | if filename not in ignore: |
11 | 12 | print(f"Opening: {filename}") |
12 | | - filecontent = open(filename, "r").read() |
13 | | - modlist = json.loads(filecontent) |
| 13 | + |
| 14 | + with open(filename, "r") as file: |
| 15 | + filecontent = file.read() |
| 16 | + |
| 17 | + try: |
| 18 | + jstyleson.load(StringIO(filecontent)) |
| 19 | + modlist = jstyleson.loads(filecontent) |
| 20 | + except Exception as err: |
| 21 | + print(f"❌ Error reading JSON file {filename}: {err}") |
| 22 | + sys.exit(os.EX_SOFTWARE) |
| 23 | + |
14 | 24 | for mod, data in modlist.items(): |
15 | 25 | url = data["download"].replace(" ", "%20") |
16 | 26 | print(f"Download {mod}: {url}") |
| 27 | + |
17 | 28 | try: |
18 | 29 | response = urllib.request.urlopen(url) |
19 | | - except: |
20 | | - print("Error: download failed!") |
| 30 | + print(f"✅ Download successful") |
| 31 | + except Exception as err: |
| 32 | + print(f"❌ Download failed: {err}") |
21 | 33 | sys.exit(os.EX_SOFTWARE) |
22 | | - filesize = round(len(response.read()) / 1024 / 1024, 3) |
| 34 | + |
| 35 | + try: |
| 36 | + filecontent = response.read().decode("utf-8") |
| 37 | + jstyleson.load(StringIO(filecontent)) |
| 38 | + print(f"✅ JSON valid") |
| 39 | + except Exception as err: |
| 40 | + print(f"❌ JSON invalid: {err}") |
| 41 | + sys.exit(os.EX_SOFTWARE) |
| 42 | + |
| 43 | + filesize = round(len(filecontent.encode('utf-8')) / 1024 / 1024, 3) |
23 | 44 | print(f"Size: {filesize}") |
24 | 45 | data["downloadSize"] = filesize |
25 | 46 |
|
26 | 47 | resultcontent = json.dumps(modlist, indent='\t', separators=(',', ' : ')) + "\n" |
27 | 48 |
|
28 | 49 | if filecontent != resultcontent: |
29 | | - open(filename, "w").write(resultcontent) |
| 50 | + with open(filename, "w") as file: |
| 51 | + file.write(resultcontent) |
30 | 52 |
|
31 | 53 | sys.exit(os.EX_OK) |
0 commit comments