Skip to content

Instantly share code, notes, and snippets.

@saucecode
Last active June 27, 2019 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saucecode/021f180eb450e1146cbdbdd58eccefad to your computer and use it in GitHub Desktop.
Save saucecode/021f180eb450e1146cbdbdd58eccefad to your computer and use it in GitHub Desktop.
import base64, zlib, sys, os, json
inflator = zlib.decompressobj()
bins = sorted([i for i in os.listdir('.') if i.endswith('.in.bin')], key=lambda j:int(j[:-7]))
pretty_print = True
# go through all the *.in.bin files, decompress them sequentially
# and write them to *.in.json files. Optionally pretty print
# them (switch the variable above)
for bin in bins:
# load and decompress the file
with open(bin, 'rb') as f:
readbytes = inflator.decompress(bytearray(base64.b64decode(f.read())))
# output file name
json_name = bin[:-3] + 'json'
# use `json.dump` with 4-space indenting
if pretty_print:
with open(json_name, 'w') as f:
try:
json.dump(json.loads(readbytes.decode()), f, indent=4)
except:
print(bin, 'is not a json file')
# just dump the decompressed contents
else:
with open(json_name, 'wb') as f:
f.write(readbytes)
# go through all the *.out.bin files, and pretty print them
if pretty_print:
bins = [i for i in os.listdir('.') if i.endswith('.out.json')]
for bin in bins:
with open(bin, 'r') as f:
read = f.read()
try:
read = json.loads(read)
except:
continue
with open(bin, 'w') as f:
json.dump(read, f, indent=4)
# write everything into final.json
jsons = sorted([i for i in os.listdir('.') if i.endswith('.json') and not 'final' in i], key=lambda s:int(s.split('.')[0]))
output = []
for index, fname in enumerate(jsons):
with open(fname, 'r') as f:
try:
output.append([index, fname, json.load(f)])
except:
print(fname, 'is not a json file')
with open('final.json','w') as f:
json.dump(output, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment