diff options
author | VitaliyS <hetleven@yandex.ua> | 2014-12-27 03:18:46 +0300 |
---|---|---|
committer | VitaliyS <hetleven@yandex.ua> | 2014-12-27 03:18:46 +0300 |
commit | 15c0f74823baea91409b557aa08249cc6e82551e (patch) | |
tree | 8dedf81b7d9c9b2dd44629c729e80fefd27600c8 | |
parent | 6434acf5ad4bd69779557bcb8c2a07b73f69b76f (diff) |
upd: WoT plugin - added new library for using Wargaming PublicAPI; deleted deprecated command wotoffers
-rw-r--r-- | lib/wgpapi/__init__.py | 1 | ||||
-rw-r--r-- | lib/wgpapi/session.py | 77 | ||||
-rw-r--r-- | plugins/wot.py | 90 |
3 files changed, 110 insertions, 58 deletions
diff --git a/lib/wgpapi/__init__.py b/lib/wgpapi/__init__.py new file mode 100644 index 0000000..61c162c --- /dev/null +++ b/lib/wgpapi/__init__.py @@ -0,0 +1 @@ +from session import *
diff --git a/lib/wgpapi/session.py b/lib/wgpapi/session.py new file mode 100644 index 0000000..4c2e13b --- /dev/null +++ b/lib/wgpapi/session.py @@ -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)) diff --git a/plugins/wot.py b/plugins/wot.py index e7fd8e6..5e2a3b7 100644 --- a/plugins/wot.py +++ b/plugins/wot.py @@ -21,19 +21,19 @@ # #
# --------------------------------------------------------------------------- #
-API_ADDR = 'http://api.worldoftanks.%s' % {'RU': 'ru', 'EU': 'eu', 'COM': 'com', 'SEA': 'asia', 'KR': 'kr'}[GT('wot_region')]
-APP_ID = GT('wot_appid')
+import wgpapi
+
+wot_api = wgpapi.Session(getattr(wgpapi.Server, GT('wot_region')), GT('wot_appid'), GT('wot_retries'), GT('wot_delay'))
clantags = re.compile('(\(.*?\))|(\[.*?\])')
def get_tanks_data():
- data = urllib.urlopen('%s/wot/encyclopedia/tanks/?application_id=%s&fields=level,name_i18n,name' % (API_ADDR, APP_ID))
- d = json.load(data)
+ d = wot_api.fetch('wot/encyclopedia/tanks', 'fields=level,name_i18n,name')
res = {}
- for i in d['data']:
- n18 = d['data'][i]['name_i18n'].rsplit(':', 1)[-1].replace('_', ' ')
- n = d['data'][i]['name'].rsplit(':', 1)[-1].replace('_', ' ')
- res[i] = {'name_i18n': n18, 'name': n, 'level': d['data'][i]['level']}
+ for i in d:
+ n18 = d[i]['name_i18n'].rsplit(':', 1)[-1].replace('_', ' ')
+ n = d[i]['name'].rsplit(':', 1)[-1].replace('_', ' ')
+ res[i] = {'name_i18n': n18, 'name': n, 'level': d[i]['level']}
return res
try:
@@ -53,34 +53,28 @@ def wot(type, jid, nick, text): name, tank = text[0], ''
else:
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))
- v = json.loads(data)
- player_id = str(v['data'][0]['account_id'])
-
- data = load_page('%s/2.0/tanks/stats/?application_id=%s&account_id=%s' % (API_ADDR, APP_ID, player_id))
- vdata = json.loads(data)
+
+ try:
+
+ 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/account/tanks/?application_id=%s&account_id=%s&fields=mark_of_mastery,tank_id' % (API_ADDR, APP_ID, player_id))
+ vdata = wot_api.fetch('wot/tanks/stats', 'account_id=%s' % player_id)
- 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))
- pdata = json.loads(data)
- stat = pdata['data'][player_id]['statistics']
+ pdata = wot_api.fetch('wot/account/info', 'account_id=%s&fields=nickname,statistics,global_rating' % player_id)
+ stat = pdata[player_id]['statistics']
+ claninfo = wot_api.fetch('wot/clan/membersinfo', 'member_id=%s' % player_id)
- data = load_page('%s/wot/clan/membersinfo/?application_id=%s&member_id=%s' % (API_ADDR, APP_ID, player_id))
- claninfo = json.loads(data)
-
- 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']
+ if claninfo[player_id]:
+ clan_id = str(claninfo[player_id]['clan_id'])
+ cdata = wot_api.fetch('wot/clan/info', 'clan_id=%s&fields=abbreviation' % clan_id)
+ cname = cdata[clan_id]['abbreviation']
except:
- pdata = {'status': ''}
+ pdata = None
- if pdata['status'] == 'ok' and pdata['data'][player_id]:
- wotname = pdata['data'][player_id]['nickname'] + ('[%s]' % cname if claninfo['data'][player_id] else '')
+ if pdata and pdata[player_id]:
+ wotname = pdata[player_id]['nickname'] + ('[%s]' % cname if claninfo[player_id] else '')
if tank:
if len(tank) == 1:
@@ -90,7 +84,7 @@ def wot(type, jid, nick, text): 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()]
- for t in vdata['data'][player_id]:
+ for t in vdata[player_id]:
if str(t['tank_id']) in tids:
tank_win = t['all']['wins']
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)
DEF = stat['all']['dropped_capture_points'] / float(battles)
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])
TIER = s / float(battles)
WINRATE = wins / float(battles)
@@ -193,7 +187,7 @@ def wot(type, jid, nick, text): elif wn6 >= 1925:
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]
@@ -240,18 +234,16 @@ def wotclan(type, jid, nick, text): text = text.strip().upper()
if text:
try:
- data = load_page('%s/2.0/clan/list/?application_id=%s&search=%s' % (API_ADDR, APP_ID, text))
- data = json.loads(data)
- claninfo = [i for i in data['data'] if i['abbreviation'] == text]
+ data = wot_api.fetch('wot/clan/list/', 'search=%s' % text)
+ claninfo = [i for i in data if i['abbreviation'] == text]
if claninfo:
claninfo = claninfo[0]
clid = claninfo['clan_id']
owner = claninfo['owner_name']
created_at = claninfo['created_at']
abbrev = claninfo['abbreviation']
- data = load_page('%s/2.0/clan/info/?application_id=%s&clan_id=%s' % (API_ADDR, APP_ID, clid))
- data = json.loads(data)
- claninfo2 = data['data'][str(clid)]
+ data = wot_api.fetch('wot/clan/info/', 'clan_id=%s' % clid)
+ claninfo2 = data[str(clid)]
msg = L('Name: %s [%s]','%s/%s'%(jid,nick)) % (claninfo2['name'], abbrev)
msg += L('\nOwner: %s','%s/%s'%(jid,nick)) % owner
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))
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
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, 'wotoffers', wotoffers, 2, 'World of Tanks - info about offers. Usage: wotoffers [active|all] [real|prem|info]')]
+ (3, 'wotclan', wotclan, 2, 'World of Tanks - info about clan. Usage: wotclan clan')]
|