-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
122 lines (94 loc) · 3.57 KB
/
generate.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Modpack manifest.json Generator
# by jadc
import requests
import json
import sys
def generate():
# Prepare headers with unique user-agent
headers = {'User-Agent': 'modmanifestgen'}
# Reads manifest template
with open('manifest-template.json') as m:
manifest = json.load(m)
# Check if version is configured in manifest
if manifest['minecraft']['version'] == '':
print('Error. There is no Minecraft version specified in the manifest.json')
exit()
# Determines latest forge modloader version depending on mc version
modLoaderData = requests.get('https://addons-ecs.forgesvc.net/api/v2/minecraft/modloader', headers=headers)
for key in modLoaderData.json():
if key['latest'] == True:
if key['gameVersion'] == manifest['minecraft']['version']:
modLoaders = [{"id": key['name'], "primary": True}]
manifest['minecraft']["modLoaders"] = modLoaders
# Reads file from drag and drop or first argument, otherwise emods.txt
if len(sys.argv) > 1:
i = open(sys.argv[1], 'r')
else:
try:
i = open('mods.txt')
except FileNotFoundError:
print(
'Error. Default mods list (mods.txt) not found; either create it or use another file in the argument/drag '
'and drop.')
exit()
# Prepares output file
mods = {
'files': []
}
# Mod log function
line = 0
def log(s):
global line
print('LINE #' + str(line).zfill(4) + ': ' + s)
# Project ID from Curseforge link
def getProjectIDFromLink(link):
req = requests.get(link, headers=headers)
# For every mod id
for line in i:
line += 1
# Remove whitespace
line = line.strip()
# Skip blank lines
if not line:
continue
# Skip comments
if line.startswith('#'):
print(projectID)
continue
# Get project id from curseforge link
projectID = getProjectIDFromLink(line)
# API GET Request
api = requests.get('https://addons-ecs.forgesvc.net/api/v2/addon/' + projectID, headers=headers)
# Skip non-existent mods
if api.status_code == 404:
log('Failed to add mod. Not found.')
continue
data = api.json()
# Gets mod name
modname = data['name']
# Gets file id for latest build in desired mc version
for key in data['gameVersionLatestFiles']:
buildExists = False
# If mod has desired mc version
if key['gameVersion'] == manifest['minecraft']['version']:
# Prepares json for mod
mod = {
'alias': modname + ' (' + key['projectFileName'] + ')',
'projectID': int(projectID),
'fileID': key['projectFileId'],
'required': True
}
mods['files'].append(mod)
log('Added [' + modname + '] (' + key['projectFileName'] + ')')
buildExists = True
break
if not buildExists:
log('Failed to add [' + modname + '], no build for MC ' + manifest['minecraft']['version'])
# Add files object to manifest
manifest['files'] = mods['files']
# Output manifest json to file (with beautify)
with open('manifest.json', 'w') as o:
json.dump(manifest, o, sort_keys=True, indent=4)
print('Done. Press ENTER to close this window.')
# Pause to allow viewing of logs
input()