def stacher_thread(task, ranking_type, ranking_subtype, avatar, url): while True: start, end, results = task.get() if start is None: break try: data = { 'controller': 'ranking', 'action': 'getRanking', 'params': { 'start': start, 'end': end, 'rankingType': ranking_type, 'rankingSubtype': ranking_subtype }, 'session': avatar.session_gameworld } r = post(url + f'c=ranking&a=getRanking&t{(time.time()*1000):.0f}', headers=avatar.headers_gameworld, json=data, cookies=avatar.cookies_gameworld, timeout=60) results.extend(r.json()['response']['results']) except Exception as e: logging.debug(f'{e}') finally: task.task_done()
def data_get_all(obj): url = obj.lobby_api session = obj.session_lobby headers = obj.headers_lobby cookies = obj.cookies_lobby data = { 'action': 'getAll', 'controller': 'player', 'params': {}, 'session': session } r = post(url, headers=headers, json=data, cookies=cookies, timeout=60) return r.json()
def login(email, password): account = Account() # looking msid url = 'https://mellon-t5.traviangames.com/authentication/login/' + \ 'ajax/form-validate?' ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101' + \ ' Firefox/63.0' headers = { 'user-agent': ua, 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.5' } r = get(url, headers=headers, hooks={'response': get_msid}, timeout=60) account.msid = r.msid # looking session lobby url = 'https://mellon-t5.traviangames.com/authentication/login/ajax/' + \ f'form-validate?msid={account.msid}&msname=msid' r = post(url, headers=headers, data={ 'email': email, 'password': password }, hooks={'response': get_token}, timeout=60) headers['cookie'] = f'msid={account.msid};' r = get(r.url_token, headers=headers, timeout=60, allow_redirects=False) r = get(r.headers['location'], headers=headers, hooks={'response': get_session}, timeout=60, allow_redirects=False) account.session_lobby = r.session # set cookies & headers lobby account.cookies_lobby = r.cookies temp_cookie = {k: v for k, v in r.cookies.items()} for k, v in temp_cookie.items(): headers['cookie'] += f' {k}={v};' account.headers_lobby = headers lobby_details = data_get_all(account) account.details = { k: v for caches in lobby_details['cache'] if 'Player:' in caches['name'] for k, v in caches['data'].items() } save_account(account) return account
def check_session(obj, state=None): if state == 'lobby': url = obj.lobby_api session = obj.session_lobby headers = obj.headers_lobby cookies = obj.cookies_lobby elif state == 'gameworld': url = obj.lobby_api + f'a=get&c=cache&t{(time.time()*1000):.0f}' session = obj.session_gameworld headers = obj.headers_gameworld cookies = obj.cookies_gameworld else: url, session, headers, cookies = None, None, None, None data = { 'action': 'get', 'controller': 'cache', 'params': { 'names': [f'Session:{session}'] }, 'session': session } r = post(url, headers=headers, json=data, cookies=cookies, timeout=60) return r.text
def get_ranking(avatar, ranking_type, ranking_subtype, table_name): # get total player url = avatar.lobby_api data = { 'controller': 'cache', 'action': 'get', 'params': { 'names': [f'GameWorld:{avatar.gameworld_id}'] }, 'session': avatar.session_lobby } r = post(url, headers=avatar.headers_lobby, json=data, cookies=avatar.cookies_lobby, timeout=60) total_player = int(r.json()['cache'][0]['data']['playersRegistered']) # prepare thread url = avatar.gameworld_api start, end = 0, 9 results = [] threads = [] task = Queue() for _ in range(2): worker = threading.Thread(target=Stacher.stacher_thread, args=(task, ranking_type, ranking_subtype, avatar, url)) worker.start() threads.append(worker) # dispatch thread for _ in range((total_player // 10) + 1): task.put((start, end, results)) time.sleep(0.1) start, end = start + 10, end + 10 # threading done task.join() for _ in range(2): task.put((None, None, None)) for t in threads: t.join() # save results path = create_path(avatar.gameworld.lower(), avatar.gameworld_id, avatar.path) try: cache = open(path, 'r') cache = json.load(cache) try: cache[table_name] except KeyError: cache[table_name] = {} except FileNotFoundError: cache = {} cache[table_name] = {} result = (line for line in results) data = ({ x['playerId']: { 'name': x['name'], 'data': [{ 'timestamp': time.time(), 'points': x['points'] }] } } for x in result) for x in data: for pid in x: if pid in cache[table_name]: cache[table_name][pid]['data'].extend(x[pid]['data']) else: cache[table_name][pid] = x[pid] with open(path, 'w') as f: f.write(json.dumps(cache, indent=4)) logging.info(f'{table_name} on {avatar.gameworld} done.')