mirror of
https://github.com/Jylpah/blitz-tools.git
synced 2026-10-26 03:29:53 +01:00
Bug fix
This commit is contained in:
1 parent
af3c46208e
commit
7f9bf1dedb
2 files changed
+24
-14
No files matched your search
+23
-13
@@ -57,11 +57,11 @@ def verbose_std(msg = ""):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def printWaiter(force = False):
|
def printWaiter(force = False):
|
||||||
if not SILENT or force:
|
if not DEBUG and (not SILENT or force):
|
||||||
print('.', end='', flush=True)
|
print('.', end='', flush=True)
|
||||||
|
|
||||||
def printNewline(force = False):
|
def printNewline(force = False):
|
||||||
if not SILENT or force:
|
if not DEBUG and (not SILENT or force):
|
||||||
print('', flush=True)
|
print('', flush=True)
|
||||||
|
|
||||||
def debug(msg = ""):
|
def debug(msg = ""):
|
||||||
@@ -80,7 +80,6 @@ def error(msg = ""):
|
|||||||
caller = calframe[1][3]
|
caller = calframe[1][3]
|
||||||
print('ERROR: ' + caller + '(): ' + msg)
|
print('ERROR: ' + caller + '(): ' + msg)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def NOW() -> int:
|
def NOW() -> int:
|
||||||
return int(time.time())
|
return int(time.time())
|
||||||
|
|
||||||
@@ -113,24 +112,26 @@ async def readJSON(filename: str, chkJSONfunc = None):
|
|||||||
error('Unexpected error when reading file: ' + filename + ' : ' + str(type(err)) + ' : '+ str(err))
|
error('Unexpected error when reading file: ' + filename + ' : ' + str(type(err)) + ' : '+ str(err))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def getUrlJSON(session: aiohttp.ClientSession, url: str, chkJSONfunc = None, max_retries = MAX_RETRIES) -> dict:
|
async def getUrlJSON(session: aiohttp.ClientSession, url: str, chkJSONfunc = None, max_tries = MAX_RETRIES) -> dict:
|
||||||
"""Retrieve (GET) an URL and return JSON object"""
|
"""Retrieve (GET) an URL and return JSON object"""
|
||||||
try:
|
try:
|
||||||
debug(url)
|
debug(url)
|
||||||
## To avoid excessive use of servers
|
## To avoid excessive use of servers
|
||||||
for retry in range(1,max_retries+1):
|
for retry in range(1,max_tries+1):
|
||||||
async with session.get(url) as resp:
|
async with session.get(url) as resp:
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
debug('HTTP request OK')
|
debug('HTTP request OK')
|
||||||
json_resp = await resp.json()
|
json_resp = await resp.json()
|
||||||
if (chkJSONfunc == None) or chkJSONfunc(json_resp):
|
if (chkJSONfunc == None) or chkJSONfunc(json_resp):
|
||||||
#debug("Received valid JSON: " + str(json_resp))
|
# debug("Received valid JSON: " + str(json_resp))
|
||||||
return json_resp
|
return json_resp
|
||||||
else:
|
else:
|
||||||
debug('Received JSON error. URL: ' + url)
|
debug('Received JSON error. URL: ' + url)
|
||||||
if retry == max_retries:
|
# Sometimes WG API returns JSON error even a retry gives valid JSON
|
||||||
|
# return None
|
||||||
|
if retry == max_tries:
|
||||||
raise aiohttp.ClientError('Request failed: ' + str(resp.status) + ' JSON Response: ' + str(json_resp) )
|
raise aiohttp.ClientError('Request failed: ' + str(resp.status) + ' JSON Response: ' + str(json_resp) )
|
||||||
verbose('Retrying URL [' + str(retry) + '/' + str(max_retries) + ']: ' + url )
|
verbose('Retrying URL [' + str(retry) + '/' + str(max_tries) + ']: ' + url )
|
||||||
await asyncio.sleep(SLEEP)
|
await asyncio.sleep(SLEEP)
|
||||||
|
|
||||||
except aiohttp.ClientError as err:
|
except aiohttp.ClientError as err:
|
||||||
@@ -171,7 +172,6 @@ class WG:
|
|||||||
URL_WG_playerTankStats = 'tanks/stats/?application_id='
|
URL_WG_playerTankStats = 'tanks/stats/?application_id='
|
||||||
URL_WG_accountID = 'account/list/?fields=account_id%2Cnickname&application_id='
|
URL_WG_accountID = 'account/list/?fields=account_id%2Cnickname&application_id='
|
||||||
URL_WG_playerStats = 'account/info/?application_id='
|
URL_WG_playerStats = 'account/info/?application_id='
|
||||||
|
|
||||||
SQLITE_STATS_CACHE = '.stats_cache.db'
|
SQLITE_STATS_CACHE = '.stats_cache.db'
|
||||||
|
|
||||||
sql_create_player_stats_tbl = """CREATE TABLE IF NOT EXISTS player_stats (
|
sql_create_player_stats_tbl = """CREATE TABLE IF NOT EXISTS player_stats (
|
||||||
@@ -319,6 +319,7 @@ class WG:
|
|||||||
|
|
||||||
## Class methods ------------------------------
|
## Class methods ------------------------------
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def getServer(cls, accountID: int) -> str:
|
def getServer(cls, accountID: int) -> str:
|
||||||
"""Get Realm/server of an account based on account ID"""
|
"""Get Realm/server of an account based on account ID"""
|
||||||
@@ -637,10 +638,18 @@ class WoTinspector:
|
|||||||
if self.session != None:
|
if self.session != None:
|
||||||
await self.session.close()
|
await self.session.close()
|
||||||
|
|
||||||
async def getReplayInfo(self, replay_id: str):
|
async def getReplayJSON(self, replay_id: str):
|
||||||
return await getUrlJSON(self.session, self.URL_WI + self.URL_REPLAY_INFO + replay_id, self.chkJSONreplay, 0)
|
json_resp = await getUrlJSON(self.session, self.URL_WI + self.URL_REPLAY_INFO + replay_id, None)
|
||||||
|
try:
|
||||||
|
if self.chkJSONreplay(json_resp):
|
||||||
|
return json_resp
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
except Exception as err:
|
||||||
|
error('Unexpected Exception: ' + str(type(err)) + ' : ' + str(err) )
|
||||||
|
return None
|
||||||
|
|
||||||
async def postReplay(self, data, filename = 'Replay', account_id = 0, title = 'Replay', priv = True, N = None):
|
async def postReplay(self, data, filename = 'Replay', account_id = 0, title = 'Replay', priv = False, N = None):
|
||||||
try:
|
try:
|
||||||
N = N if N != None else self.REPLAY_N
|
N = N if N != None else self.REPLAY_N
|
||||||
msg_str = 'Replay[' + str(N) + ']: '
|
msg_str = 'Replay[' + str(N) + ']: '
|
||||||
@@ -651,7 +660,7 @@ class WoTinspector:
|
|||||||
replay_id = hash.hexdigest()
|
replay_id = hash.hexdigest()
|
||||||
|
|
||||||
## Testing if the replay has already been posted
|
## Testing if the replay has already been posted
|
||||||
json_resp = await self.getReplayInfo(replay_id)
|
json_resp = await self.getReplayJSON(replay_id)
|
||||||
if json_resp != None:
|
if json_resp != None:
|
||||||
debug(msg_str + 'Already uploaded: ' + title)
|
debug(msg_str + 'Already uploaded: ' + title)
|
||||||
return json_resp
|
return json_resp
|
||||||
@@ -664,6 +673,7 @@ class WoTinspector:
|
|||||||
'key' : replay_id
|
'key' : replay_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
url = self.URL_WI + self.URL_REPLAY_UL + urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
|
url = self.URL_WI + self.URL_REPLAY_UL + urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
|
||||||
#debug('URL: ' + url)
|
#debug('URL: ' + url)
|
||||||
headers ={'Content-type': 'application/x-www-form-urlencoded'}
|
headers ={'Content-type': 'application/x-www-form-urlencoded'}
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ async def replayWorker(queue: asyncio.Queue, workerID: int, account_id: int, pri
|
|||||||
SKIPPED_N += 1
|
SKIPPED_N += 1
|
||||||
queue.task_done()
|
queue.task_done()
|
||||||
continue
|
continue
|
||||||
except concurrent.futures.CancelledError as err:
|
except asyncio.CancelledError as err:
|
||||||
raise err
|
raise err
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
bu.error(msg_str + 'Unexpected error: ' + str(type(err)) + ' : '+ str(err))
|
bu.error(msg_str + 'Unexpected error: ' + str(type(err)) + ' : '+ str(err))
|
||||||
|
|||||||
Reference in new issue
Block a user