upd: WoT plugin - added new library for using Wargaming PublicAPI; deleted deprecated command wotoffers
This commit is contained in:
1
lib/wgpapi/__init__.py
Normal file
1
lib/wgpapi/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from session import *
|
||||||
77
lib/wgpapi/session.py
Normal file
77
lib/wgpapi/session.py
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
"""
|
||||||
|
Python library for using Wargaming PublicAPI
|
||||||
|
Copyright (C) 2014 VitaliyS <hetleven@yandex.ua>
|
||||||
|
|
||||||
|
based on: https://github.com/OpenWGPAPI
|
||||||
|
|
||||||
|
This code is covered by the standard Python License.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
import urllib
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
class Server:
|
||||||
|
RU = 'worldoftanks.ru'
|
||||||
|
EU = 'worldoftanks.eu'
|
||||||
|
COM = 'worldoftanks.com'
|
||||||
|
SEA = 'worldoftanks.asia'
|
||||||
|
KR = 'worldoftanks.kr'
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Page(object):
|
||||||
|
|
||||||
|
def __init__(self, url, num_retries=5, delay_seconds=5):
|
||||||
|
|
||||||
|
self.url = url
|
||||||
|
self.row_text = ''
|
||||||
|
self.response_code = 0
|
||||||
|
self.response_info = ()
|
||||||
|
self.num_retries = num_retries
|
||||||
|
self.delay_seconds = delay_seconds
|
||||||
|
|
||||||
|
def fetch(self):
|
||||||
|
|
||||||
|
last_exception = None
|
||||||
|
count = 0
|
||||||
|
is_ok = False
|
||||||
|
|
||||||
|
while (not is_ok) and (count < self.num_retries):
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = urllib2.urlopen(self.url, None, 30)
|
||||||
|
is_ok = True
|
||||||
|
except Exception as e:
|
||||||
|
last_exception = e
|
||||||
|
count += 1
|
||||||
|
time.sleep(self.delay_seconds)
|
||||||
|
|
||||||
|
if not is_ok:
|
||||||
|
raise last_exception
|
||||||
|
|
||||||
|
if response.getcode() > 200:
|
||||||
|
raise Error("urlopen code: %d url: %s" % (response.getcode(), self.url))
|
||||||
|
|
||||||
|
self.response_code = response.getcode()
|
||||||
|
self.response_info = response.info()
|
||||||
|
self.row_text = response.read()
|
||||||
|
return self.row_text
|
||||||
|
|
||||||
|
class Session(object):
|
||||||
|
|
||||||
|
def __init__(self, api_host, api_key, num_retries=5, delay_seconds=5):
|
||||||
|
self.api_host = api_host
|
||||||
|
self.api_key = api_key
|
||||||
|
self.num_retries = num_retries
|
||||||
|
self.delay_seconds = delay_seconds
|
||||||
|
|
||||||
|
def fetch(self, url, params):
|
||||||
|
page = Page("http://api.%s/%s/?application_id=%s&%s" % (self.api_host, url, self.api_key, params), self.num_retries, self.delay_seconds)
|
||||||
|
resp = json.loads(page.fetch())
|
||||||
|
if resp['status'] == 'ok':
|
||||||
|
return resp['data']
|
||||||
|
|
||||||
|
raise Error(repr(resp))
|
||||||
@@ -21,19 +21,19 @@
|
|||||||
# #
|
# #
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
|
|
||||||
API_ADDR = 'http://api.worldoftanks.%s' % {'RU': 'ru', 'EU': 'eu', 'COM': 'com', 'SEA': 'asia', 'KR': 'kr'}[GT('wot_region')]
|
import wgpapi
|
||||||
APP_ID = GT('wot_appid')
|
|
||||||
|
wot_api = wgpapi.Session(getattr(wgpapi.Server, GT('wot_region')), GT('wot_appid'), GT('wot_retries'), GT('wot_delay'))
|
||||||
|
|
||||||
clantags = re.compile('(\(.*?\))|(\[.*?\])')
|
clantags = re.compile('(\(.*?\))|(\[.*?\])')
|
||||||
|
|
||||||
def get_tanks_data():
|
def get_tanks_data():
|
||||||
data = urllib.urlopen('%s/wot/encyclopedia/tanks/?application_id=%s&fields=level,name_i18n,name' % (API_ADDR, APP_ID))
|
d = wot_api.fetch('wot/encyclopedia/tanks', 'fields=level,name_i18n,name')
|
||||||
d = json.load(data)
|
|
||||||
res = {}
|
res = {}
|
||||||
for i in d['data']:
|
for i in d:
|
||||||
n18 = d['data'][i]['name_i18n'].rsplit(':', 1)[-1].replace('_', ' ')
|
n18 = d[i]['name_i18n'].rsplit(':', 1)[-1].replace('_', ' ')
|
||||||
n = d['data'][i]['name'].rsplit(':', 1)[-1].replace('_', ' ')
|
n = d[i]['name'].rsplit(':', 1)[-1].replace('_', ' ')
|
||||||
res[i] = {'name_i18n': n18, 'name': n, 'level': d['data'][i]['level']}
|
res[i] = {'name_i18n': n18, 'name': n, 'level': d[i]['level']}
|
||||||
return res
|
return res
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -53,34 +53,28 @@ def wot(type, jid, nick, text):
|
|||||||
name, tank = text[0], ''
|
name, tank = text[0], ''
|
||||||
else:
|
else:
|
||||||
name, tank = text[0], text[1].lower()
|
name, tank = text[0], text[1].lower()
|
||||||
try:
|
|
||||||
data = load_page('%s/2.0/account/list/?application_id=%s&search=%s&fields=account_id&limit=1' % (API_ADDR, APP_ID, name))
|
try:
|
||||||
v = json.loads(data)
|
|
||||||
player_id = str(v['data'][0]['account_id'])
|
v = wot_api.fetch('wot/account/list', 'search=%s&fields=account_id&limit=1' % name)
|
||||||
|
player_id = str(v[0]['account_id'])
|
||||||
|
|
||||||
data = load_page('%s/2.0/tanks/stats/?application_id=%s&account_id=%s' % (API_ADDR, APP_ID, player_id))
|
vdata = wot_api.fetch('wot/tanks/stats', 'account_id=%s' % player_id)
|
||||||
vdata = json.loads(data)
|
|
||||||
|
|
||||||
data = load_page('%s/2.0/account/tanks/?application_id=%s&account_id=%s&fields=mark_of_mastery,tank_id' % (API_ADDR, APP_ID, player_id))
|
pdata = wot_api.fetch('wot/account/info', 'account_id=%s&fields=nickname,statistics,global_rating' % player_id)
|
||||||
|
stat = pdata[player_id]['statistics']
|
||||||
|
|
||||||
data = load_page('%s/2.0/account/info/?application_id=%s&account_id=%s&fields=nickname,statistics,global_rating' % (API_ADDR, APP_ID, player_id))
|
claninfo = wot_api.fetch('wot/clan/membersinfo', 'member_id=%s' % player_id)
|
||||||
pdata = json.loads(data)
|
|
||||||
stat = pdata['data'][player_id]['statistics']
|
|
||||||
|
|
||||||
|
if claninfo[player_id]:
|
||||||
data = load_page('%s/wot/clan/membersinfo/?application_id=%s&member_id=%s' % (API_ADDR, APP_ID, player_id))
|
clan_id = str(claninfo[player_id]['clan_id'])
|
||||||
claninfo = json.loads(data)
|
cdata = wot_api.fetch('wot/clan/info', 'clan_id=%s&fields=abbreviation' % clan_id)
|
||||||
|
cname = cdata[clan_id]['abbreviation']
|
||||||
if claninfo['data'][player_id]:
|
|
||||||
clan_id = str(claninfo['data'][player_id]['clan_id'])
|
|
||||||
data = load_page('%s/2.0/clan/info//?application_id=%s&clan_id=%s&fields=abbreviation' % (API_ADDR, APP_ID, clan_id))
|
|
||||||
cdata = json.loads(data)
|
|
||||||
cname = cdata['data'][clan_id]['abbreviation']
|
|
||||||
except:
|
except:
|
||||||
pdata = {'status': ''}
|
pdata = None
|
||||||
|
|
||||||
if pdata['status'] == 'ok' and pdata['data'][player_id]:
|
if pdata and pdata[player_id]:
|
||||||
wotname = pdata['data'][player_id]['nickname'] + ('[%s]' % cname if claninfo['data'][player_id] else '')
|
wotname = pdata[player_id]['nickname'] + ('[%s]' % cname if claninfo[player_id] else '')
|
||||||
|
|
||||||
if tank:
|
if tank:
|
||||||
if len(tank) == 1:
|
if len(tank) == 1:
|
||||||
@@ -90,7 +84,7 @@ def wot(type, jid, nick, text):
|
|||||||
msg = '%s:' % wotname
|
msg = '%s:' % wotname
|
||||||
tids = [tid for tid in tanks_data if tank in tanks_data[tid]['name'].lower() or tank in tanks_data[tid]['name_i18n'].lower()]
|
tids = [tid for tid in tanks_data if tank in tanks_data[tid]['name'].lower() or tank in tanks_data[tid]['name_i18n'].lower()]
|
||||||
|
|
||||||
for t in vdata['data'][player_id]:
|
for t in vdata[player_id]:
|
||||||
if str(t['tank_id']) in tids:
|
if str(t['tank_id']) in tids:
|
||||||
tank_win = t['all']['wins']
|
tank_win = t['all']['wins']
|
||||||
tank_battle = t['all']['battles']
|
tank_battle = t['all']['battles']
|
||||||
@@ -144,7 +138,7 @@ def wot(type, jid, nick, text):
|
|||||||
msg += L('\nAv. captured points: %s','%s/%s'%(jid,nick)) % round(CAP, 2)
|
msg += L('\nAv. captured points: %s','%s/%s'%(jid,nick)) % round(CAP, 2)
|
||||||
DEF = stat['all']['dropped_capture_points'] / float(battles)
|
DEF = stat['all']['dropped_capture_points'] / float(battles)
|
||||||
msg += L('\nAv. defense points: %s','%s/%s'%(jid,nick)) % round(DEF, 2)
|
msg += L('\nAv. defense points: %s','%s/%s'%(jid,nick)) % round(DEF, 2)
|
||||||
tanks = [vh for vh in vdata['data'][player_id] if vh['all']['battles']]
|
tanks = [vh for vh in vdata[player_id] if vh['all']['battles']]
|
||||||
s = sum([t['all']['battles'] * tanks_data[str(t['tank_id'])]['level'] for t in tanks])
|
s = sum([t['all']['battles'] * tanks_data[str(t['tank_id'])]['level'] for t in tanks])
|
||||||
TIER = s / float(battles)
|
TIER = s / float(battles)
|
||||||
WINRATE = wins / float(battles)
|
WINRATE = wins / float(battles)
|
||||||
@@ -193,7 +187,7 @@ def wot(type, jid, nick, text):
|
|||||||
elif wn6 >= 1925:
|
elif wn6 >= 1925:
|
||||||
msg += L(' - unicum','%s/%s'%(jid,nick))
|
msg += L(' - unicum','%s/%s'%(jid,nick))
|
||||||
|
|
||||||
msg += L('\nWG rating: %s','%s/%s'%(jid,nick)) % pdata['data'][player_id]['global_rating']
|
msg += L('\nWG rating: %s','%s/%s'%(jid,nick)) % pdata[player_id]['global_rating']
|
||||||
|
|
||||||
stat_rnd = lambda x: stat['all'][x] - stat['clan'][x] - stat['company'][x]
|
stat_rnd = lambda x: stat['all'][x] - stat['clan'][x] - stat['company'][x]
|
||||||
|
|
||||||
@@ -240,18 +234,16 @@ def wotclan(type, jid, nick, text):
|
|||||||
text = text.strip().upper()
|
text = text.strip().upper()
|
||||||
if text:
|
if text:
|
||||||
try:
|
try:
|
||||||
data = load_page('%s/2.0/clan/list/?application_id=%s&search=%s' % (API_ADDR, APP_ID, text))
|
data = wot_api.fetch('wot/clan/list/', 'search=%s' % text)
|
||||||
data = json.loads(data)
|
claninfo = [i for i in data if i['abbreviation'] == text]
|
||||||
claninfo = [i for i in data['data'] if i['abbreviation'] == text]
|
|
||||||
if claninfo:
|
if claninfo:
|
||||||
claninfo = claninfo[0]
|
claninfo = claninfo[0]
|
||||||
clid = claninfo['clan_id']
|
clid = claninfo['clan_id']
|
||||||
owner = claninfo['owner_name']
|
owner = claninfo['owner_name']
|
||||||
created_at = claninfo['created_at']
|
created_at = claninfo['created_at']
|
||||||
abbrev = claninfo['abbreviation']
|
abbrev = claninfo['abbreviation']
|
||||||
data = load_page('%s/2.0/clan/info/?application_id=%s&clan_id=%s' % (API_ADDR, APP_ID, clid))
|
data = wot_api.fetch('wot/clan/info/', 'clan_id=%s' % clid)
|
||||||
data = json.loads(data)
|
claninfo2 = data[str(clid)]
|
||||||
claninfo2 = data['data'][str(clid)]
|
|
||||||
msg = L('Name: %s [%s]','%s/%s'%(jid,nick)) % (claninfo2['name'], abbrev)
|
msg = L('Name: %s [%s]','%s/%s'%(jid,nick)) % (claninfo2['name'], abbrev)
|
||||||
msg += L('\nOwner: %s','%s/%s'%(jid,nick)) % owner
|
msg += L('\nOwner: %s','%s/%s'%(jid,nick)) % owner
|
||||||
msg += L('\nCreated at: %s','%s/%s'%(jid,nick)) % time.ctime(created_at)
|
msg += L('\nCreated at: %s','%s/%s'%(jid,nick)) % time.ctime(created_at)
|
||||||
@@ -266,25 +258,7 @@ def wotclan(type, jid, nick, text):
|
|||||||
msg = L('What?','%s/%s'%(jid,nick))
|
msg = L('What?','%s/%s'%(jid,nick))
|
||||||
send_msg(type,jid,nick,msg)
|
send_msg(type,jid,nick,msg)
|
||||||
|
|
||||||
def wotoffers(type, jid, nick, text):
|
|
||||||
text = text.strip().split(' ', 1)
|
|
||||||
try:
|
|
||||||
url = 'http://jexp2.wotapi.ru/wotnews/get-news.php'
|
|
||||||
d = '?'
|
|
||||||
for opt in text:
|
|
||||||
if opt in ['active', 'all']:
|
|
||||||
url += '%sactivity=%s' % (d, opt)
|
|
||||||
d = '&'
|
|
||||||
if opt in ['real', 'prem', 'info']:
|
|
||||||
url += '%sdetailed=%s' % (d, opt)
|
|
||||||
d = '&'
|
|
||||||
msg = load_page(url).decode('utf-8')
|
|
||||||
except:
|
|
||||||
msg = L('Impossible to get info','%s/%s'%(jid,nick))
|
|
||||||
send_msg(type,jid,nick,msg)
|
|
||||||
|
|
||||||
global execute
|
global execute
|
||||||
|
|
||||||
execute = [(3, 'wot', wot, 2, 'World of Tanks - info about user. Usage: wot [nick [tank]]'),
|
execute = [(3, 'wot', wot, 2, 'World of Tanks - info about user. Usage: wot [nick [tank]]'),
|
||||||
(3, 'wotclan', wotclan, 2, 'World of Tanks - info about clan. Usage: wotclan clan'),
|
(3, 'wotclan', wotclan, 2, 'World of Tanks - info about clan. Usage: wotclan clan')]
|
||||||
(3, 'wotoffers', wotoffers, 2, 'World of Tanks - info about offers. Usage: wotoffers [active|all] [real|prem|info]')]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user