mirror of
https://github.com/Jylpah/blitz-tools.git
synced 2026-10-24 19:49:52 +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 True
|
||||||
return False
|
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:
|
def debug(msg = "", id = None, exception = None, force: bool = False) -> bool:
|
||||||
"""print a conditional debug message"""
|
"""print a conditional debug message"""
|
||||||
if (_log_level >= DEBUG) or force:
|
if (_log_level >= DEBUG) or force:
|
||||||
@@ -445,6 +452,40 @@ def bld_dict_hierarcy(d : dict, key : str, value) -> dict:
|
|||||||
error('Unexpected Exception', err)
|
error('Unexpected Exception', err)
|
||||||
return None
|
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
|
#### Class SlowBar
|
||||||
## -----------------------------------------------------------
|
## -----------------------------------------------------------
|
||||||
@@ -1462,41 +1503,39 @@ class WoTinspector:
|
|||||||
error(msg_str + 'Unexpected Exception', err)
|
error(msg_str + 'Unexpected Exception', err)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
json_resp = None
|
||||||
for retry in range(MAX_RETRIES):
|
for retry in range(MAX_RETRIES):
|
||||||
debug(msg_str + 'Posting: ' + title + ' Try #: ' + str(retry + 1) + '/' + str(MAX_RETRIES) )
|
debug(msg_str + 'Posting: ' + title + ' Try #: ' + str(retry + 1) + '/' + str(MAX_RETRIES) )
|
||||||
try:
|
try:
|
||||||
async with self.session.post(url, headers=headers, data=payload) as resp:
|
async with self.session.post(url, headers=headers, data=payload) as resp:
|
||||||
debug(msg_str + 'HTTP response: '+ str(resp.status))
|
debug(msg_str + 'HTTP response: '+ str(resp.status))
|
||||||
if resp.status == 200:
|
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()
|
json_resp = await resp.json()
|
||||||
if json_resp.get('status', None) == None:
|
if self.chk_JSON_replay(json_resp):
|
||||||
error(msg_str +' : ' + title + ' : Received invalid JSON')
|
debug(msg_str + 'Response data read. Status OK')
|
||||||
elif (json_resp['status'] == 'ok'):
|
|
||||||
debug('Response data read. Status OK')
|
|
||||||
return json_resp
|
return json_resp
|
||||||
elif (json_resp['status'] == 'error'):
|
debug(msg_str +' : ' + title + ' : Receive invalid JSON')
|
||||||
error(msg_str + json_resp['error']['message'] + ' : ' + title)
|
|
||||||
else:
|
|
||||||
error(msg_str + ' Unspecified error: ' + title)
|
|
||||||
else:
|
else:
|
||||||
debug(msg_str + 'Got HTTP/' + str(resp.status))
|
debug(msg_str + 'Got HTTP/' + str(resp.status))
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
error(exception=err)
|
debug(msg_str, exception=err)
|
||||||
await asyncio.sleep(SLEEP)
|
await asyncio.sleep(SLEEP)
|
||||||
|
|
||||||
error(msg_str + ' Could not post replay: ' + title)
|
debug(msg_str + ' Could not post replay: ' + title)
|
||||||
return None
|
return json_resp
|
||||||
|
|
||||||
|
|
||||||
async def get_replay_listing(self, page: int = 0) -> aiohttp.ClientResponse:
|
async def get_replay_listing(self, page: int = 0) -> aiohttp.ClientResponse:
|
||||||
url = self.get_url_replay_listing(page)
|
url = self.get_url_replay_listing(page)
|
||||||
return await self.session.get(url)
|
return await self.session.get(url)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_url_replay_listing(cls, page : int):
|
def get_url_replay_listing(cls, page : int):
|
||||||
return cls.URL_REPLAY_LIST + str(page) + '?vt=#filters'
|
return cls.URL_REPLAY_LIST + str(page) + '?vt=#filters'
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_replay_links(cls, doc: str):
|
def get_replay_links(cls, doc: str):
|
||||||
"""Get replay download links from WoTinspector.com replay listing page"""
|
"""Get replay download links from WoTinspector.com replay listing page"""
|
||||||
@@ -1513,21 +1552,24 @@ class WoTinspector:
|
|||||||
error(exception=err)
|
error(exception=err)
|
||||||
return replay_links
|
return replay_links
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_replay_id(cls, url):
|
def get_replay_id(cls, url):
|
||||||
return url.rsplit('/', 1)[-1]
|
return url.rsplit('/', 1)[-1]
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def chk_JSON_replay(cls, json_resp):
|
def chk_JSON_replay(cls, json_resp):
|
||||||
""""Check String for being a valid JSON file"""
|
""""Check String for being a valid JSON file"""
|
||||||
try:
|
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")
|
debug("JSON check OK")
|
||||||
return True
|
return True
|
||||||
except KeyError as err:
|
except KeyError as err:
|
||||||
error('Key not found', err)
|
debug('Replay JSON check failed', exception=err)
|
||||||
except:
|
except:
|
||||||
debug("JSON check failed: " + str(json_resp))
|
debug("Replay JSON check failed: " + str(json_resp))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
class BlitzStars:
|
class BlitzStars:
|
||||||
|
|||||||
Reference in new issue
Block a user