Last active
August 29, 2015 13:59
-
-
Save KsaRedFx/10960825 to your computer and use it in GitHub Desktop.
white-listconvert.py
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 os | |
import json | |
import urllib2 | |
from itertools import izip, izip_longest | |
from urllib import quote, quote_plus as _quote_plus | |
mode=None | |
def grouper(iterable, n, fillvalue=None): | |
args = [iter(iterable)] * n | |
return izip_longest(*args, fillvalue=fillvalue) | |
def getUUID(names, requested='N/A', total='N/A', mode=None): | |
if mode is "Mojang": | |
profurl = 'http://api.goender.net/api/profiles/minecraft/mojang' | |
else: | |
profurl = 'http://api.goender.net/api/profiles/minecraft' | |
post_data = names | |
usagent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, ' \ | |
'like Gecko) Chrome/22.0.1229.79 Safari/537.4' | |
try: | |
req = urllib2.Request(profurl, json.dumps(post_data)) | |
req.add_header('User-agent', usagent) | |
req.add_header('Content-Type', 'application/json') | |
opener = urllib2.build_opener() | |
response = opener.open(req) | |
except Exception as e: | |
return "Errored: %s" % e | |
profiles = json.load(response) | |
if type(profiles) == dict and "Error" in profiles.keys(): | |
return "Something went wrong: %s \n %s" % (profiles, post_data) | |
else: | |
report = "Fetched %s UUIDs, Requested %s UUIDs, Recieved So Far %s UUIDs, Total %s UUIDs" % (len(profiles), len(post_data), str(requested) ,str(total)) | |
if mode is "Mojang": | |
whitelist = [] | |
for items in profiles: | |
current = {} | |
uuid = items['id'] | |
name = items['name'] | |
uuid = "%s-%s-%s-%s-%s" % (uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:]) | |
current['name'] = name | |
current['uuid'] = uuid | |
whitelist.append(current) | |
print report | |
return whitelist | |
else: | |
return report | |
###### Names in white-list.txt | |
files = "white-list.txt" | |
newfile = "%s.json" % files.split(".")[0].replace('-', '') | |
allnames = open(files).readlines() | |
os.rename(files, '%s.backup' % files) | |
proccessed = [] | |
complete = [] | |
mode = 'Mojang' | |
for name in allnames: | |
name = name.rstrip('\n') | |
proccessed.append(name) | |
allnames = proccessed | |
################################################################################ | |
req = 0 | |
total_per_go = 1000 | |
groups = grouper(allnames, total_per_go) | |
for names in groups: | |
names = filter(lambda a: a != None, names) | |
req = req + total_per_go | |
results = getUUID(names, req, len(allnames), mode) | |
if mode is "Mojang": | |
complete.append(results) | |
else: | |
print results | |
if mode is "Mojang": | |
f = open(newfile, 'w+') | |
f.write(json.dumps(complete[0], sort_keys=True, indent=4)) | |
f.truncate() | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment