diff --git a/wotbreplay_uploader.py b/wotbreplay_uploader.py index 1321775..affb839 100644 --- a/wotbreplay_uploader.py +++ b/wotbreplay_uploader.py @@ -1,12 +1,13 @@ #!/usr/bin/python3.7 import json, argparse, inspect, sys, os, base64, aiohttp, urllib, asyncio, aiofiles, aioconsole, logging, re +from WG_utils import WG logging.getLogger("asyncio").setLevel(logging.DEBUG) # WG account id of the uploader: # # Find it here: https://developers.wargaming.net/reference/all/wotb/account/list/ -WG_ID = USE_YOUR_OWN_WG_ID +WG_ID = None DEBUG = False VERBOSE = False @@ -14,37 +15,40 @@ SLEEP = 2 MAX_RETRIES = 3 REPLAY_N = 0 WIurl='https://wotinspector.com/api/replay/upload?' +WG_appID = '81381d3f45fa4aa75b78a7198eb216ad' +wg = None -TANKS = None -MAPS ={ - 'amigosville' : 'Falls Creek', - 'canal' : 'Canal', - 'canyon' : 'Canyon', - 'desert_train' : 'Desert Sands', - 'erlenberg' : 'Middleburg', - 'fort' : 'Fort Despair', - 'grossberg' : 'Dynasty\'s Pearl', - 'faust' : 'Faust', - 'himmelsdorf' : 'Himmelsdorf', - 'italy' : 'Vineyeards', - 'karelia' : 'Rockfield', - 'milbase' : 'Yamato Harbor', - 'mountain' : 'Black Goldville', - 'port' : 'Port Bay', - 'medvedkovo' : 'Dead Rail', - 'pliego' : 'Castilla', - 'rock' : 'Mayan Ruins', - 'rudniki' : 'Mines', - 'savanna' : 'Oasis Palms', - 'skit' : 'Naval Frontier' - } +# TANKS = None +# MAPS ={ +# 'amigosville' : 'Falls Creek', +# 'canal' : 'Canal', +# 'canyon' : 'Canyon', +# 'desert_train' : 'Desert Sands', +# 'erlenberg' : 'Middleburg', +# 'fort' : 'Fort Despair', +# 'grossberg' : 'Dynasty\'s Pearl', +# 'faust' : 'Faust', +# 'himmelsdorf' : 'Himmelsdorf', +# 'italy' : 'Vineyeards', +# 'karelia' : 'Rockfield', +# 'milbase' : 'Yamato Harbor', +# 'mountain' : 'Black Goldville', +# 'port' : 'Port Bay', +# 'medvedkovo' : 'Dead Rail', +# 'pliego' : 'Castilla', +# 'rock' : 'Mayan Ruins', +# 'rudniki' : 'Mines', +# 'savanna' : 'Oasis Palms', +# 'skit' : 'Naval Frontier' +# } async def main(argv): - global VERBOSE, DEBUG, TANKS + global VERBOSE, DEBUG, wg parser = argparse.ArgumentParser(description='Post replays(s) to WoTinspector.com and retrieve battle data') parser.add_argument('--output', default='single', choices=['file', 'files', 'db'] , help='Select output mode: single/multiple files or database') 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=None, 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('-p', '--private', dest="private", action='store_true', default=False, help='Set replays private on WoTinspector.com') parser.add_argument('--tasks', dest='N_tasks', type=int, default=10, help='Number of worker threads') @@ -54,14 +58,15 @@ async def main(argv): parser.add_argument('files', metavar='FILE1 [FILE2 ...]', type=str, nargs='+', help='Files to read. Use \'-\' for STDIN"') args = parser.parse_args() - print(args) VERBOSE = args.verbose DEBUG = args.debug if DEBUG: VERBOSE = True + + wg = WG(WG_appID, args.tankopedia) - if args.tankopedia != None: - with open(args.tankopedia, 'r', encoding='utf8') as f: - TANKS = json.load(f) + if args.account != None: + args.accountID = await wg.getAccountID(args.account) + debug('WG account_id: ' + str(args.accountID)) try: queue = asyncio.Queue() @@ -84,10 +89,13 @@ async def main(argv): debug('Waiting for workers to cancel') await asyncio.gather(*tasks, return_exceptions=True) verbose(str(REPLAY_N) + ' replays uploaded') - + except KeyboardInterrupt: print('Ctrl-c pressed ...') sys.exit(1) + finally: + ## Need to close the aiohttp.session since Python destructors do not support async methods... + await wg.session.close() return None @@ -196,22 +204,24 @@ async def storeReplayJSON(filename: str, json_data : dict): def getTitle(replayfile: str, title: str, i : int) -> str: - + global wg + if title == None: try: filename = os.path.basename(replayfile) # debug(filename) - p = re.compile('\\d{8}_\\d{4}_(.+)_(' + '|'.join(MAPS.keys()) + ')(?:-\\d)?\\.wotbreplay$') + map_usrStrs = wg.getMapUserStrs() + p = re.compile('\\d{8}_\\d{4}_(.+)_(' + '|'.join(map_usrStrs) + ')(?:-\\d)?\\.wotbreplay$') m = p.match(filename) if (m != None): # debug('Match') - if TANKS != None: - tank = TANKS['userStr'][m.group(1)] + if wg.tanks != None: + tank = wg.tanks['userStr'][m.group(1)] # debug('Tank from Tankopedia:' + tank) else: tank = m.group(1) # debug(tank) - blitz_map = MAPS[m.group(2)] + blitz_map = wg.maps[m.group(2)] title = tank + ' @ ' + blitz_map else: # debug('No match') @@ -252,5 +262,4 @@ def error(msg = ""): ### main() if __name__ == "__main__": - # use debug=True to better debug the code asyncio.run(main(sys.argv[1:]), debug=False)