Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import webapp2 | ||
import json | ||
import urllib | ||
import time | ||
import xml.etree.ElementTree as ET | ||
|
||
class apiHandler(webapp2.RequestHandler): | ||
def get(self): | ||
if 'gamertag' in self.request.GET: | ||
self.response.headers['Content-Type'] = 'application/json' | ||
docType = '{http://www.w3.org/1999/xhtml}' | ||
gamertag = self.request.GET['gamertag'] | ||
gamertag = urllib.unquote(gamertag) | ||
if 'callback' in self.request.GET: | ||
self.response.write(self.request.GET['callback']+'(') | ||
output = {} | ||
try: | ||
msPageHandle = urllib.urlopen('http://gamercard.xbox.com/en-US/'+urllib.quote(gamertag,'/ ')+'.card') | ||
msPage = msPageHandle.read() | ||
msPageHandle.close | ||
parse = ET.fromstring(msPage) | ||
except: | ||
output['GamertagExists'] = False | ||
self.response.write(json.JSONEncoder().encode(output)) | ||
return | ||
output['Gamertag'] = parse.find('.//*[@id="Gamertag"]').text | ||
output['Gamerscore'] = parse.find('.//*[@id="Gamerscore"]').text | ||
if output['Gamerscore']=='--': | ||
output['GamertagExists'] = False | ||
output['Gamerscore'] = 0 | ||
else: | ||
output['GamertagExists'] = True | ||
output['Gamerscore'] = int(output['Gamerscore']) | ||
mainDivRaw = parse.find('.//'+docType+'div').attrib['class'].split(' ') | ||
output['Subscription'] = mainDivRaw[1] | ||
output['Gender'] = mainDivRaw[2] | ||
output['Pictures'] = {} | ||
output['Pictures']['Tile32px'] = 'http://avatar.xboxlive.com/avatar/'+gamertag+'/avatarpic-s.png' | ||
output['Pictures']['Tile64px'] = 'http://avatar.xboxlive.com/avatar/'+gamertag+'/avatarpic-l.png' | ||
output['Pictures']['FullBody'] = 'http://avatar.xboxlive.com/avatar/'+gamertag+'/avatar-body.png' | ||
reputationRaw = parse.findall('.//*[@class="RepContainer"]/'+docType+'div') | ||
output['Reputation'] = 0 | ||
for star in reputationRaw: | ||
if star.attrib['class']=='Star Full': | ||
output['Reputation'] += 1 | ||
elif star.attrib['class']=='Star ThreeQuarter': | ||
output['Reputation'] += .75 | ||
elif star.attrib['class']=='Star Quarter': | ||
output['Reputation'] += .25 | ||
elif star.attrib['class']=='Star Half': | ||
output['Reputation'] += .5 | ||
lastPlayedRaw = parse.findall('.//*[@id="PlayedGames"]/'+docType+'li/'+docType+'a'); | ||
output['LastPlayed'] = [] | ||
for lastPlayed in lastPlayedRaw: | ||
tmp = {} | ||
attribs = lastPlayed.findall(docType+'span'); | ||
for attrib in attribs: | ||
if attrib.text==None: | ||
continue | ||
elif attrib.text.isdigit(): | ||
tmp[attrib.attrib['class']] = int(attrib.text) | ||
else: | ||
tmp[attrib.attrib['class']] = attrib.text | ||
tmp['Pictures'] = {'Tile32px':lastPlayed.find(docType+'img').attrib['src']} | ||
if 'LastPlayed' in tmp: | ||
tmp['LastPlayed'] = int(time.mktime(time.strptime(tmp['LastPlayed'],'%m/%d/%Y'))) | ||
else: | ||
tmp['LastPlayed'] = 0 | ||
output['LastPlayed'].append(tmp) | ||
self.response.write(json.JSONEncoder().encode(output)) | ||
if 'callback' in self.request.GET: | ||
self.response.write(');') | ||
else: | ||
self.response.headers['Content-Type'] = 'text/plain' | ||
self.response.write('Pass a gamertag in GET like so: /?gamertag=flotwig\nYou can also pass in a JSONP callback with the "callback" parameter\n\nhttps://github.com/flotwig/360api-gae') | ||
|
||
app = webapp2.WSGIApplication([('/', apiHandler)],debug=True) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
application: 360api | ||
version: 1 | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: true | ||
|
||
handlers: | ||
- url: /favicon.ico | ||
static_files: favicon.ico | ||
upload: favicon.ico | ||
- url: / | ||
script: 360api.app |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
360api | ||
======== | ||
This is a port of my earlier Xbox 360 Gamercard API code from PHP to Python 2.7, compatible with Google's app engine. This is my first ever Python project, so please feel free to point out any mistakes so I may improve on them. | ||
|
||
HTTP: http://360api.chary.us/?gamertag=flotwig | ||
HTTPS: https://360api.appspot.com/?gamertag=flotwig |