Skip to content

Instantly share code, notes, and snippets.

@bartku
Created April 23, 2012 13:47
Show Gist options
  • Save bartku/2471018 to your computer and use it in GitHub Desktop.
Save bartku/2471018 to your computer and use it in GitHub Desktop.
xvm-stats proxy
#!/usr/bin/env python
from eventlet.green import urllib2
import eventlet
import json
import web
import time
urls = ('/u/(.*)', 'index')
api_ver = {'account_search': 1.1, 'accounts': 1.3, 'clan_search': 1.0, 'clans': 1.1, 'personal': 1.0}
tokens = ['WG-WoT_Assistant-1.2.2', 'WG-WoT_Assistant-1.1.2', 'Intellect_Soft-WoT_Mobile-site', 'Intellect_Soft-WoT_Mobile', 'WG-WoT_Assistant-test']
data = {'dmg': 0, 'def': 0, 'cap': 0, 'des': 0, 'det': 0}
class PlayerData(object):
def __init__(self, id):
self.id = id
self.url = "http://worldoftanks.eu/uc/accounts/" + str(self.id) + "/api/" + str(api_ver['accounts']) + "/?source_token=" + tokens[0]
self.request = urllib2.Request(self.url)
try:
self.response = urllib2.urlopen(self.request)
except urllib2.HTTPError as err:
print('HTTP error: ' + str(err.code))
except urllib2.URLError as err:
print('URL error: ' + err.reason)
def getData(self):
try:
self.predata = json.load(self.response)
self.data = self.predata['data']
except (KeyError, AttributeError):
print('Invalid data')
def getName(self):
try:
return self.data['name']
except AttributeError:
return 0
def getExp(self):
try:
return self.data['ratings']['xp']['value']
except AttributeError:
return 0
def getAvgExp(self):
try:
return self.data['ratings']['battle_avg_xp']['value']
except AttributeError:
return 0
def getDamage(self):
try:
return self.data['ratings']['damage_dealt']['value']
except AttributeError:
return 0
def getAvgDamage(self):
try:
return self.getDamage()/float(self.getBattles())
except ZeroDivisionError:
return 0
def getCapture(self):
try:
return self.data['ratings']['ctf_points']['value']
except AttributeError:
return 0
def getAvgCapture(self):
try:
return self.getCapture()/float(self.getBattles())
except ZeroDivisionError:
return 0
def getDefense(self):
try:
return self.data['ratings']['dropped_ctf_points']['value']
except AttributeError:
return 0
def getAvgDefense(self):
try:
return self.getDefense()/float(self.getBattles())
except ZeroDivisionError:
return 0
def getSpotted(self):
try:
return self.data['ratings']['spotted']['value']
except AttributeError:
return 0
def getAvgSpotted(self):
try:
return self.getSpotted()/float(self.getBattles())
except ZeroDivisionError:
return 0
def getBattles(self):
try:
return self.data['ratings']['battles']['value']
except AttributeError:
return 0
def getWins(self):
try:
return self.data['ratings']['battle_wins']['value']
except AttributeError:
return 0
def getFrags(self):
try:
return self.data['ratings']['frags']['value']
except AttributeError:
return 0
def getAvgFrags(self):
try:
return self.getFrags()/float(self.getBattles())
except ZeroDivisionError:
return 0
def getAvgTankTier(self):
self.avgtier = 0
try:
for i in self.data['vehicles']:
self.avgtier = self.avgtier + i['battle_count'] * i['level']
return float(self.avgtier)/self.getBattles()
except AttributeError:
return 0
except ZeroDivisionError:
return 0
def getEff(self):
try:
return (self.getAvgDamage() * (1.5 / self.getAvgTankTier() + 0.2) +
self.getAvgFrags() * (350 - 20 * self.getAvgTankTier()) +
self.getAvgSpotted() * 200 + 150 * (self.getAvgCapture() + self.getAvgDefense()))
except ZeroDivisionError:
return 0
def fetch(pid):
fetch_player = PlayerData(pid)
fetch_player.getData()
return {'id': int(pid), 'name': fetch_player.getName(), 'eff': int(fetch_player.getEff()), 'battles': fetch_player.getBattles(), 'wins': fetch_player.getWins()}
class StatsGenerator(object):
def printjsonforplayer(self, iid):
self.data = {'info': {'xvm': {'ver': '1.4', 'message': ''}}, 'players': []}
self.player = PlayerData(iid)
self.player.getData()
self.playerdata = {'id': iid, 'name': self.player.getName(), 'eff': int(self.player.getEff()),
'battles': self.player.getBattles(), 'wins': self.player.getWins()}
self.data['players'].append(self.playerdata)
return json.dumps(self.data)
def printjsonforplayers(self, args):
self.start = time.time()
self.data = {'info': {'xvm': {'ver': '1.4', 'message': ''}}, 'players': []}
pool = eventlet.GreenPool()
for i in pool.imap(fetch, args):
self.data['players'].append(i)
print("Elapsed Time: %s" % (time.time() - self.start))
return json.dumps(self.data)
class index:
def GET(self, name):
stats = StatsGenerator()
list = name.split(',')
print('len: ' + str(len(list)))
return stats.printjsonforplayers(list)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment