-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonutils.py
More file actions
54 lines (42 loc) · 1.64 KB
/
jsonutils.py
File metadata and controls
54 lines (42 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
import requests
from utils import getFromCurseforgeAPI
def getPackName(jsonInput):
jsonLoaded = json.loads(jsonInput)
modpackName = jsonLoaded["name"]
try:
modpackVersion = jsonLoaded["version"]
except KeyError:
modpackVersion = "0"
return modpackName + "_" + modpackVersion
def getMCVersion(jsonInput):
jsonLoaded = json.loads(jsonInput)
mcVersion = jsonLoaded["minecraft"]["version"]
return mcVersion
def getModloaderName(jsonInput):
jsonLoaded = json.loads(jsonInput)
modloaderVersion = jsonLoaded["minecraft"]["modLoaders"][0]["id"]
return modloaderVersion
def downloadMods(jsonInput, location, path):
jsonLoaded = json.loads(jsonInput)
modsList = jsonLoaded["files"]
downloadedMods = 1
totalMods = len(modsList)
f = open(path + "README.txt", 'a')
for file in modsList:
projectID = file["projectID"]
fileID = file["fileID"]
url = f"https://api.curseforge.com/v1/mods/{projectID}/files/{fileID}"
fileName = downloadFile(url, location, downloadedMods, totalMods)
print(f"{downloadedMods}/{totalMods} | ✅ {fileName}")
f.write(f"\n{downloadedMods}/{totalMods} | {fileName}")
downloadedMods += 1
f.close()
def downloadFile(url, location, downloadedMods, totalMods):
jsonLoaded = json.loads(getFromCurseforgeAPI(url))
downloadURL = jsonLoaded["data"]["downloadUrl"]
fileName = jsonLoaded["data"]["fileName"]
print(f"{downloadedMods}/{totalMods} | 🔽 {fileName}")
r = requests.get(downloadURL, allow_redirects=True)
open(location + fileName, 'wb').write(r.content)
return fileName