Last active
August 19, 2020 19:07
-
-
Save JJTech0130/a39b0530bfab91e1732d1a022eca50d1 to your computer and use it in GitHub Desktop.
Usage: ./fixmeta.py --input $path_to_input_json --output $path_to_output_json
This file contains hidden or 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
#!/usr/bin/python3 | |
import os, hashlib, urllib.request, optparse, json | |
def get_remote_info(url): | |
remote = urllib.request.urlopen(url) | |
#Hash types | |
hash1 = hashlib.sha1() | |
hash256 = hashlib.sha256() | |
#Download data into buffer | |
data = remote.read() | |
#Get size | |
filesize = data.__len__() | |
#Get hashes | |
hash1.update(data) | |
hash256.update(data) | |
#Return dictionary | |
info = { | |
'sha1' : hash1.hexdigest(), | |
'sha256' : hash256.hexdigest(), | |
'size' : filesize | |
} | |
return info | |
def fixjson(input_file,output_file): | |
#Load JSON meta file | |
with open(input_file) as f: | |
data = json.load(f) | |
#Iterate through libraries | |
for library in data['libraries']: | |
try: | |
#Replace URL | |
library['downloads']['artifact']['url'] = library['downloads']['artifact']['url'].replace('libraries.minecraft.net', 'jjtech0130.github.io/natives') | |
#Get hash and size of new URL | |
remote_info = get_remote_info(library['downloads']['artifact']['url']) | |
#Insert new hash and size | |
library['downloads']['artifact']['sha1'] = remote_info['sha1'] | |
library['downloads']['artifact']['size'] = remote_info['size'] | |
except KeyError: | |
pass | |
try: | |
#We don't need OSX and Windows natives | |
del library['downloads']['classifiers']['natives-osx'] | |
del library['downloads']['classifiers']['natives-windows'] | |
#Replace URL | |
library['downloads']['classifiers']['natives-linux']['url'] = library['downloads']['classifiers']['natives-linux']['url'].replace('libraries.minecraft.net', 'jjtech0130.github.io/natives') | |
#Get hash and size of new URL | |
remote_info = get_remote_info(library['downloads']['classifiers']['natives-linux']['url']) | |
#Insert new hash and size | |
library['downloads']['classifiers']['natives-linux']['sha1'] = remote_info['sha1'] | |
library['downloads']['classifiers']['natives-linux']['size'] = remote_info['size'] | |
#Delete references to the OSX and Windows natives | |
del library['natives']['osx'] | |
del library['natives']['windows'] | |
except KeyError: | |
pass | |
#Output modified JSON | |
with open(output_file, 'w') as f: | |
json.dump(data, f, indent=4) | |
if __name__ == '__main__': | |
opt = optparse.OptionParser() | |
opt.add_option('--input', '-i') | |
opt.add_option('--output', '-o') | |
options, args = opt.parse_args() | |
fixjson(options.input, options.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment