mirror of
https://github.com/Jylpah/blitz-tools.git
synced 2026-10-21 10:17:05 +02:00
Add: new utils funcs, update WoTinspector.chk_replay_JSON()
New util funcs: - warning() - get_JSON_keypath() - get_JSON_value()
This commit is contained in:
1 parent
99bfb37636
commit
a95e0afdb0
1 file changed
+57
-15
+57
-15
@@ -210,6 +210,13 @@ def verbose_std(msg = "", id = None) -> bool:
|
||||
return True
|
||||
return False
|
||||
|
||||
def warning(msg = "", id = None) -> bool:
|
||||
"""Print a warning message"""
|
||||
if _log_level >= NORMAL:
|
||||
_print_log_msg('', 'Warning: ' + msg, None, id)
|
||||
return True
|
||||
return False
|
||||
|
||||
def debug(msg = "", id = None, exception = None, force: bool = False) -> bool:
|
||||
"""print a conditional debug message"""
|
||||
if (_log_level >= DEBUG) or force:
|
||||
@@ -445,6 +452,40 @@ def bld_dict_hierarcy(d : dict, key : str, value) -> dict:
|
||||
error('Unexpected Exception', err)
|
||||
return None
|
||||
|
||||
def get_JSON_keypath(keypath: str, key: str):
|
||||
if keypath == None:
|
||||
return key
|
||||
else:
|
||||
return '.'.join([keypath, key])
|
||||
|
||||
def get_JSON_value(json, key : str = None, keys : list = None, keypath = None):
|
||||
if keys == None:
|
||||
keys = key.split('.')
|
||||
|
||||
if len(keys) == 0:
|
||||
return json
|
||||
|
||||
key = keys.pop(0)
|
||||
if type(json) == dict:
|
||||
if key in json:
|
||||
return get_JSON_value(json[key], keys=keys, keypath=get_JSON_keypath(keypath, key))
|
||||
else:
|
||||
raise KeyError('Key: '+ get_JSON_keypath(keypath, key) + ' not found')
|
||||
|
||||
if type(json) == list:
|
||||
p = re.compile(r'^\[(\d+)\]$')
|
||||
m = p.match(key)
|
||||
if len(m.groups()) != 1:
|
||||
raise KeyError('Invalid key given: ' + get_JSON_keypath(keypath, key))
|
||||
ndx = m.group(1)
|
||||
try:
|
||||
return get_JSON_value(json[ndx], keys=keys, keypath=get_JSON_keypath(keypath, key))
|
||||
except IndexError:
|
||||
raise KeyError('JSON array index out of range: ' + get_JSON_keypath(keypath, key))
|
||||
|
||||
raise KeyError('Key not found: ' + get_JSON_keypath(keypath, keys[0]))
|
||||
|
||||
|
||||
## -----------------------------------------------------------
|
||||
#### Class SlowBar
|
||||
## -----------------------------------------------------------
|
||||
@@ -1462,41 +1503,39 @@ class WoTinspector:
|
||||
error(msg_str + 'Unexpected Exception', err)
|
||||
return None
|
||||
|
||||
json_resp = None
|
||||
for retry in range(MAX_RETRIES):
|
||||
debug(msg_str + 'Posting: ' + title + ' Try #: ' + str(retry + 1) + '/' + str(MAX_RETRIES) )
|
||||
try:
|
||||
async with self.session.post(url, headers=headers, data=payload) as resp:
|
||||
debug(msg_str + 'HTTP response: '+ str(resp.status))
|
||||
if resp.status == 200:
|
||||
debug('HTTP POST 200 = Success. Reading response data')
|
||||
debug(msg_str + 'HTTP POST 200 = Success. Reading response data')
|
||||
json_resp = await resp.json()
|
||||
if json_resp.get('status', None) == None:
|
||||
error(msg_str +' : ' + title + ' : Received invalid JSON')
|
||||
elif (json_resp['status'] == 'ok'):
|
||||
debug('Response data read. Status OK')
|
||||
if self.chk_JSON_replay(json_resp):
|
||||
debug(msg_str + 'Response data read. Status OK')
|
||||
return json_resp
|
||||
elif (json_resp['status'] == 'error'):
|
||||
error(msg_str + json_resp['error']['message'] + ' : ' + title)
|
||||
else:
|
||||
error(msg_str + ' Unspecified error: ' + title)
|
||||
debug(msg_str +' : ' + title + ' : Receive invalid JSON')
|
||||
else:
|
||||
debug(msg_str + 'Got HTTP/' + str(resp.status))
|
||||
except Exception as err:
|
||||
error(exception=err)
|
||||
debug(msg_str, exception=err)
|
||||
await asyncio.sleep(SLEEP)
|
||||
|
||||
error(msg_str + ' Could not post replay: ' + title)
|
||||
return None
|
||||
debug(msg_str + ' Could not post replay: ' + title)
|
||||
return json_resp
|
||||
|
||||
|
||||
async def get_replay_listing(self, page: int = 0) -> aiohttp.ClientResponse:
|
||||
url = self.get_url_replay_listing(page)
|
||||
return await self.session.get(url)
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_url_replay_listing(cls, page : int):
|
||||
return cls.URL_REPLAY_LIST + str(page) + '?vt=#filters'
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_replay_links(cls, doc: str):
|
||||
"""Get replay download links from WoTinspector.com replay listing page"""
|
||||
@@ -1513,21 +1552,24 @@ class WoTinspector:
|
||||
error(exception=err)
|
||||
return replay_links
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_replay_id(cls, url):
|
||||
return url.rsplit('/', 1)[-1]
|
||||
|
||||
|
||||
@classmethod
|
||||
def chk_JSON_replay(cls, json_resp):
|
||||
""""Check String for being a valid JSON file"""
|
||||
try:
|
||||
if ('status' in json_resp) and json_resp['status'] == 'ok' and ('data' in json_resp) and json_resp['data'] != None:
|
||||
if ('status' in json_resp) and json_resp['status'] == 'ok' and \
|
||||
(get_JSON_value(json_resp, key='data.summary.exp_base') != None) :
|
||||
debug("JSON check OK")
|
||||
return True
|
||||
except KeyError as err:
|
||||
error('Key not found', err)
|
||||
debug('Replay JSON check failed', exception=err)
|
||||
except:
|
||||
debug("JSON check failed: " + str(json_resp))
|
||||
debug("Replay JSON check failed: " + str(json_resp))
|
||||
return False
|
||||
|
||||
class BlitzStars:
|
||||
|
||||
Reference in new issue
Block a user