From 662171f10ec89d76c0172d786facd475e03b9841 Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sat, 19 Sep 2020 19:23:40 +0300 Subject: [PATCH 1/3] get_tank_name(), get_map_name --- blitzutils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/blitzutils.py b/blitzutils.py index 728938a..006f5f7 100644 --- a/blitzutils.py +++ b/blitzutils.py @@ -760,15 +760,25 @@ class WG: @classmethod - def get_map(cls, map_str: str) -> str: + def get_map_name(cls, map_str: str) -> str: """Return map name from short map string in replays""" try: return cls.maps[map_str] except: error('Map ' + map_str + ' not found') - return None + return map_str + @classmethod + def get_tank_name(cls, tank_str: str) -> str: + """Return tnak name from short tank string in replays""" + try: + return cls.tanks["userStr"][tank_str] + except: + error('Tank ' + tank_str + ' not found') + return tank_str + + @classmethod def get_map_user_strs(cls) -> str: return cls.maps.keys() From 66546ec57d83cba4be361d0264615f60623890fe Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sat, 19 Sep 2020 19:24:14 +0300 Subject: [PATCH 2/3] getTitle(): read tank/map names from the replay file itself --- upload_wotb_replays.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/upload_wotb_replays.py b/upload_wotb_replays.py index 15a1d2f..74d303e 100644 --- a/upload_wotb_replays.py +++ b/upload_wotb_replays.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3.8 import json, argparse, inspect, sys, os, base64, aiohttp, urllib, asyncio, aiofiles, aioconsole -import logging, re, concurrent.futures, configparser, motor.motor_asyncio, ssl +import logging, re, concurrent.futures, configparser, motor.motor_asyncio, ssl, zipfile import blitzutils as bu from blitzutils import WG from blitzutils import WoTinspector @@ -231,7 +231,7 @@ async def replayWorker(queue: asyncio.Queue, workerID: int, account_id: int, pri return None -def getTitle(replayfile: str, title: str, i : int) -> str: +def getTitle_old(replayfile: str, title: str, i : int) -> str: global wg if title == None: @@ -270,6 +270,33 @@ def getTitle(replayfile: str, title: str, i : int) -> str: return title +def getTitle(replayfile: str, title: str, i : int) -> str: + global wg + + if title == None: + try: + filename = os.path.basename(replayfile) + bu.debug(filename) + player = None + tank = None + map_name = None + + with zipfile.ZipFile(replayfile, 'r') as rp_archive: + with rp_archive.open('meta.json') as rp_metadata: + metadata_json = json.loads(rp_metadata) + player = metadata_json['playerName'] + tank = wg.get_tank_name(metadata_json['playerVehicleName']) + map_name = wg.get_map_name(metadata_json['mapName']) + if (player != None) and (tank != None) and (map_name != None): + title = tank + ' @ ' + map_name + else: + title = re.sub('\\.wotbreplay$', '', filename) + except Exception as err: + bu.error(err) + else: + title.replace('NN', str(i)) + return title + ### main() ------------------------------------------- if __name__ == "__main__": # To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp From ac74d450548391164a12578f02dd368fc35600aa Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sat, 19 Sep 2020 20:39:10 +0300 Subject: [PATCH 3/3] getTitle(): read tank & map from replay metadata. DONE --- upload_wotb_replays.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/upload_wotb_replays.py b/upload_wotb_replays.py index 74d303e..ea8f3d4 100644 --- a/upload_wotb_replays.py +++ b/upload_wotb_replays.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3.8 -import json, argparse, inspect, sys, os, base64, aiohttp, urllib, asyncio, aiofiles, aioconsole +import json, argparse, inspect, sys, os, io, base64, aiohttp, urllib, asyncio, aiofiles, aioconsole import logging, re, concurrent.futures, configparser, motor.motor_asyncio, ssl, zipfile import blitzutils as bu from blitzutils import WG @@ -67,7 +67,7 @@ async def main(argv): parser = argparse.ArgumentParser(description='Post replays(s) to WoTinspector.com and retrieve replay data as JSON') parser.add_argument('-id', dest='accountID', type=int, default=WG_ID, help='WG account_id') parser.add_argument('-a', '--account', dest='account', type=str, default=WG_ACCOUNT, help='Uploader\'s WG account name. Format: ACCOUNT_NAME@SERVER') - parser.add_argument('-t','--title', type=str, default=None, help='Title for replays. Use NN for continous numbering. Default is filename-based numbering') + parser.add_argument('-t','--title', type=str, default=None, help='Title for replays. Use "NN" for continous numbering. Default is automatic naming') parser.add_argument('-p', '--private', dest="private", action='store_true', default=False, help='Set replays private on WoTinspector.com') parser.add_argument('--tankopedia', type=str, default='tanks.json', help='JSON file to read Tankopedia from. Default: "tanks.json"') parser.add_argument('--mapfile', type=str, default='maps.json', help='JSON file to read Blitz map names from. Default: "maps.json"') @@ -272,7 +272,6 @@ def getTitle_old(replayfile: str, title: str, i : int) -> str: def getTitle(replayfile: str, title: str, i : int) -> str: global wg - if title == None: try: filename = os.path.basename(replayfile) @@ -281,13 +280,19 @@ def getTitle(replayfile: str, title: str, i : int) -> str: tank = None map_name = None - with zipfile.ZipFile(replayfile, 'r') as rp_archive: - with rp_archive.open('meta.json') as rp_metadata: - metadata_json = json.loads(rp_metadata) - player = metadata_json['playerName'] - tank = wg.get_tank_name(metadata_json['playerVehicleName']) - map_name = wg.get_map_name(metadata_json['mapName']) - if (player != None) and (tank != None) and (map_name != None): + with zipfile.ZipFile(replayfile, 'r') as archive: + # bu.debug('Replay file: ' + replayfile + ' opened') + with io.TextIOWrapper(archive.open('meta.json')) as meta: + # bu.debug('Replay file\'s metadata: opened') + try: + metadata_json = json.load(meta) + #player = metadata_json['playerName'] + tank = wg.get_tank_name(metadata_json['playerVehicleName']) + map_name = wg.get_map_name(metadata_json['mapName']) + bu.debug('Tank: ' + tank + ' Map: ' + map_name) + except Exception as err: + bu.error(exception = err) + if (tank != None) and (map_name != None): title = tank + ' @ ' + map_name else: title = re.sub('\\.wotbreplay$', '', filename) @@ -295,6 +300,7 @@ def getTitle(replayfile: str, title: str, i : int) -> str: bu.error(err) else: title.replace('NN', str(i)) + bu.debug('Returning: ' + title) return title ### main() -------------------------------------------