Last active
May 25, 2019 05:22
-
-
Save sfxworks/329a6dd09506c86d11f5d047ff799e69 to your computer and use it in GitHub Desktop.
Convert a curse modpack into an ATLauncher XML modpack file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import json | |
import urllib.request | |
import shutil | |
import zipfile | |
import os.path | |
#Curse Base URL | |
baseurl = "https://minecraft.curseforge.com/projects/" | |
dl_base_url = "http://files.sfxworks.net/lapitos/" | |
internal_base_url = "/var/www/html/lapitos/" | |
#<XML> Mod Header ------------------- | |
XMLversion = ET.Element("version") | |
mods = ET.SubElement(XMLversion, "mods") | |
#Unzip somwhere... | |
#Load manifest | |
with open("manifest.json") as f: | |
manifest = json.load(f) | |
#Retrieve File | |
for file in manifest['files']: | |
#Parse Vars | |
invalidJson = False | |
projectID = str(file['projectID']) | |
fileID = str(file['fileID']) | |
curseFile = baseurl + projectID + "/files/" + fileID + '/download' | |
file_name = projectID + '-' + fileID | |
#Mod defaults | |
version = "0" | |
name = projectID + "-" + fileID | |
website = baseurl + projectID + "/files/" + fileID | |
description = "This mod has invalid json in their mcmod.info file. You will have to gather these values manually." | |
print("Getting " + file_name) | |
if os.path.isfile(internal_base_url + file_name) is not True: | |
with urllib.request.urlopen(curseFile) as response, open(internal_base_url + file_name, 'wb') as out_file: | |
shutil.copyfileobj(response, out_file) | |
archive = zipfile.ZipFile(internal_base_url + file_name) | |
print("Extracting mcmod.info") | |
for archive_file in archive.namelist(): | |
if archive_file.startswith('mcmod.info'): | |
archive.extract(archive_file, 'tmp') | |
break | |
with open('tmp/mcmod.info') as f: | |
try: | |
print("Parsing mcmod.info") | |
#Get mcmod.info | |
mcmodinfo = json.load(f) | |
#Get object position read if modlistversion is 2 | |
if 'modListVersion' in mcmodinfo: | |
if mcmodinfo['modListVersion'] == 2: | |
mcmodinfo = mcmodinfo['modList'] | |
#Overwrite defaults for values to extract | |
version = str(mcmodinfo[0]['version']) | |
name = mcmodinfo[0]['name'] | |
website = baseurl + projectID | |
if 'url' in mcmodinfo[0]: | |
if mcmodinfo[0]['url'] is not "": | |
website = mcmodinfo[0]['url'] | |
if 'description' in mcmodinfo[0]: | |
description = mcmodinfo[0]['description'] | |
#Handle bad json | |
except (ValueError, KeyError): | |
print ("----MOD HAS INVALID JSON----") | |
url = dl_base_url + file_name | |
print("Writing " + name + " in XML") | |
ET.SubElement(mods, "mod", name=name, version=version, file=file_name, url=url, description=description, website=website, download="direct", type="mods") | |
print ("Writing XML") | |
tree = ET.ElementTree(XMLversion) | |
tree.write("test.xml") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment