Skip to content

Commit

Permalink
Merge branch 'python'
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Feb 24, 2016
2 parents 5cd6772 + dfc5ff2 commit 9b6d18f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
77 changes: 77 additions & 0 deletions 360api/360api.py
@@ -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)
12 changes: 12 additions & 0 deletions 360api/app.yaml
@@ -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 added 360api/favicon.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions README.md
@@ -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

0 comments on commit 9b6d18f

Please sign in to comment.