Last active
January 3, 2016 06:58
-
-
Save Imprtat/8425901 to your computer and use it in GitHub Desktop.
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 python3.3 | |
# -*- coding: utf-8 -*- | |
import urllib.request as request | |
import urllib.parse as parse | |
import xlsxwriter | |
from xlsxwriter.utility import xl_rowcol_to_cell as cell_name | |
import json, pickle, datetime, sys | |
API_URL = 'api.worldoftanks.ru' | |
GAME_WOT = 'wot' | |
CLAN_ID = sys.argv[1] #18890 | |
vehicles = {} | |
defaultArgs = { | |
'application_id': 'demo' | |
} | |
def apiRequest(game, url, args = {}, protocol = 'https'): | |
url = '%s://%s/%s/%s/' % (protocol, API_URL, game, url) | |
data = defaultArgs.copy() | |
data.update(args) | |
urlArgs = parse.urlencode(data).encode('utf-8') | |
print('apiRequest: url: %s' % url) | |
print('apiRequest data: %s' % urlArgs) | |
#TODO: handle http errors | |
resp = request.urlopen(url, urlArgs) | |
if resp.code == 200: | |
respStr = resp.readall().decode('utf-8') | |
return resp, json.loads(respStr) | |
else: | |
return resp, None | |
def getSimpleInfo(game, url, data = {}, fields = ''): | |
if fields: | |
data['fields'] = fields | |
_, info = apiRequest(game, url, data) | |
# print(info) | |
if info and info['status'] == 'ok': | |
return info['data'] | |
else: | |
return None | |
def getClanMembers(game, clanId, fields = 'members.account_name'): | |
data = { | |
'clan_id': clanId | |
} | |
return getSimpleInfo(game, 'clan/info', data, fields) | |
def getVehicles(game, accounts, fields): | |
data = { | |
'account_id': accounts | |
} | |
return getSimpleInfo(game, 'account/tanks', data, fields) | |
def getVehicleInfo(game, fields = 'tank_id,level,type,name,name_i18n'): | |
return getSimpleInfo(game, 'encyclopedia/tanks', fields = fields) | |
def loadVehicleInfo(game): | |
filename = './vehicle.%s' % game | |
try: | |
f = open(filename, 'rb') | |
if f.readable(): | |
vehicles[game] = pickle.load(f) | |
f.close() | |
except IOError as e: | |
data = getVehicleInfo(game) | |
if data: | |
vehicles[game] = data | |
f = open(filename, 'wb') | |
pickle.dump(vehicles[game], f) | |
f.close() | |
def getVehicleLevel(game, vehId): | |
vehicle = vehicles[game].get(str(vehId)) | |
if vehicle: | |
return vehicle['level'] | |
return 0 | |
def getVehicleName(game, vehId): | |
vehicle = vehicles[game].get(str(vehId)) | |
if vehicle: | |
return vehicle['name_i18n'] | |
return 0 | |
def getKeysForBulk(data, limit = 5): | |
keys = [] | |
tmpKeys = [] | |
for i in data: | |
if len(tmpKeys) < limit: | |
tmpKeys.append(i) | |
else: | |
tmpKeys.append(i) | |
keys.append(','.join(tmpKeys)) | |
tmpKeys.clear() | |
if len(tmpKeys): | |
keys.append(','.join(tmpKeys)) | |
return keys | |
def getVehiclesByLevel(game, level): | |
vehs = [] | |
for v in vehicles[game]: | |
if getVehicleLevel(game, v) == level: | |
vehs.append(v) | |
return vehs | |
def getUserVehicleStat(stat, vehId): | |
for veh in stat: | |
if veh['tank_id'] == int(vehId): | |
return veh | |
return None | |
def main(): | |
loadVehicleInfo(GAME_WOT) | |
topVehs = getVehiclesByLevel(GAME_WOT, 10) | |
clanMembers = getClanMembers(GAME_WOT, CLAN_ID) | |
if not clanMembers: | |
return | |
vehicleStat = {} | |
members = clanMembers[CLAN_ID]['members'] | |
for clanAccounts in getKeysForBulk(members, 7): | |
data = getVehicles(GAME_WOT, clanAccounts, 'tank_id,statistics.battles,statistics.wins') | |
vehicleStat.update(data) | |
now = datetime.date.today() - datetime.timedelta(1) | |
xFile = xlsxwriter.Workbook('clans/%s.xlsx' % CLAN_ID) | |
sheetName = '%d-%02d-%02d' % (now.year, now.month, now.day) | |
sheet = None | |
for s in xFile.worksheets(): | |
if s.name == sheetName: | |
sheet = s | |
if sheet == None: | |
sheet = xFile.add_worksheet(sheetName) | |
column = 1 | |
for vehId in topVehs: | |
sheet.write(0, column, getVehicleName(GAME_WOT, vehId)) | |
column += 1 | |
sheet.write(0, column, 'Avg') | |
row = 1 | |
for accountId in vehicleStat: | |
sheet.write(row, 0, members[accountId]['account_name']) | |
info = vehicleStat[accountId] | |
column = 1 | |
totalBattles = 0 | |
sumWins = 0 | |
for vehId in topVehs: | |
vehStat = getUserVehicleStat(info, vehId) | |
if vehStat: | |
stats = vehStat['statistics'] | |
winrate = round(100 * stats['wins'] / stats['battles'], 2) | |
totalBattles += stats['battles'] | |
sumWins += stats['wins'] | |
sheet.write(row, column, winrate) | |
column += 1 | |
# sheet.write(row, column, '=AVERAGE(%s:%s)' % (cell_name(row, 1), cell_name(row, column - 1))) | |
sheet.write(row, column, round(100 * sumWins / totalBattles, 2) if totalBattles > 0 else 0) | |
row += 1 | |
xFile.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment