Last active
August 29, 2015 13:56
-
-
Save pwyliu/8857833 to your computer and use it in GitHub Desktop.
Download Spelunky daily run results
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
#!/usr/bin/env python | |
import sys | |
import datetime | |
import requests | |
import defusedxml.ElementTree as ETree | |
def get_url(url): | |
try: | |
resp = requests.get(url) | |
resp.raise_for_status() | |
except requests.RequestException as ex: | |
print "ERROR: {}".format(ex) | |
sys.exit(1) | |
return resp | |
def text_to_tree(xml): | |
try: | |
return ETree.fromstring(xml) | |
except ETree.ParseError: | |
print "ERROR: Invalid XML" | |
sys.exit(1) | |
def get_score(global_url, steamid, date=None): | |
lb_url = None | |
resp = get_url(global_url) | |
xml = text_to_tree(resp.text) | |
if date is None: | |
date = datetime.datetime.utcnow().strftime('%m/%d/%Y') | |
title = '{} DAILY'.format(str(date)) | |
for elem in xml.iter('leaderboard'): | |
if title in elem.find('name').text: | |
lb_url = elem.find('url').text | |
break | |
if lb_url is not None: | |
lb_url += '&steamid={}'.format(steamid) | |
resp = get_url(lb_url) | |
xml = text_to_tree(resp.text) | |
for elem in xml.iter('entry'): | |
if steamid in elem.find('steamid').text: | |
return ({ | |
'steamid': steamid, | |
'score': elem.find('score').text, | |
'rank': elem.find('rank').text, | |
'ugcid': elem.find('ugcid').text, | |
'details': elem.find('details').text | |
}, lb_url) | |
return None, lb_url | |
else: | |
print "ERROR: Leaderboard not found" | |
sys.exit(1) | |
if __name__ == '__main__': | |
STEAMID64 = '76561197997426215' # Player id | |
COMMUNITY = '239350' # Spelunky community name | |
URL = ('http://steamcommunity.com/stats/{}/' | |
'leaderboards/?xml=1').format(COMMUNITY) | |
score, search_url = get_score(URL, STEAMID64) | |
if score is not None: | |
print score | |
sys.exit(0) | |
else: | |
print "No score found on {}".format(search_url) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment