Created
January 22, 2019 00:56
-
-
Save jacob1/14aa9ef023d49173e138aa1edb6fd62a to your computer and use it in GitHub Desktop.
backup tpt saves, just give it a powder.pref
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 urllib, urllib2 | |
import re | |
import sys | |
import json | |
def FatalError(message): | |
print(message) | |
exit(1) | |
def GetPage(url, cookies = None, headers = None, removeTags = False): | |
if cookies: | |
req = urllib2.Request(url, urllib.urlencode(headers) if headers else None, {'Cookie':cookies}) | |
else: | |
req = urllib2.Request(url) | |
page = urllib2.urlopen(req, timeout=10).read() | |
if removeTags: | |
return re.sub("<.*?>", "", page) | |
return page | |
def GetSessionInfo(): | |
try: | |
powderpref = open("powder.pref", "r") | |
prefs = json.loads(powderpref.read()) | |
powderpref.close() | |
return "UserRemember=Yes; UserID={0}; UserKey={1}".format(prefs["User"]["ID"],prefs["User"]["SessionID"]) | |
except: | |
return None | |
def GetUsername(): | |
try: | |
powderpref = open("powder.pref", "r") | |
prefs = json.loads(powderpref.read()) | |
powderpref.close() | |
return prefs["User"]["Username"] | |
except: | |
return None | |
def GetMaxPage(page): | |
pagelinks = [] | |
pagelinks.extend(re.findall("/User/Saves.html\?ID=[0-9]*&PageNum=([0-9]*)", page)) | |
if pagelinks: | |
return int(max(pagelinks)) | |
else: | |
return 0 | |
if __name__ == "__main__": | |
username = None | |
start = 0 | |
end = -1 | |
for arg in sys.argv[1:]: | |
(argname, argvalue) = arg.split("=") | |
if argname == "--username": | |
username = argvalue | |
elif argname == "--pages": | |
(start,end) = map(int, argvalue.split(",")) | |
else: | |
FatalError("Valid arguments: --username=<name>, --pages=<start>,<end>") | |
if not username: | |
username = GetUsername() | |
if not username: | |
FatalError("Could not get username from powder.pref, use the --username argument") | |
print(username) | |
if start < 0 or end < 0: | |
link = "http://powdertoy.co.uk/User/Saves.html?Name=%s" % username | |
page = GetPage(link, GetSessionInfo()) | |
if page: | |
end = GetMaxPage(page) | |
else: | |
FatalError("Could not get number of pages to fetch saves from, use the --pages argument") | |
print(start,end) | |
matches = [] | |
for i in range(start, end): | |
link = "http://powdertoy.co.uk/User/Saves.html?Name=%s&PageNum=%s" % (username, i) | |
page = GetPage(link, GetSessionInfo()) | |
matches.extend(re.findall("http://static.powdertoy.co.uk/([0-9]*)_small.png", page)) | |
print(matches) | |
for i in matches: | |
save = GetPage("http://static.powdertoy.co.uk/%s.cps" % i) | |
f = open(i+".cps", "w") | |
f.write(save) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment