def collect_matches_by_seq_num(key): """collect matches by the sequence number :param key: the API key :return: null """ api = dota2api.Dota2api(key) last_seq_num = 0 while True: matches = api.get_match_history_by_sequence_num() last_seq_num = matches[-1]['match_seq_num'] for match in matches: if match['human_players'] != 10: continue # do not record the game that is not finished normally players = match['players'] finished = True for player in players: if player['leaver_status'] != 0: finished = False print("match " + str(match['match_id']) + " is not finished normally") break if finished: print('Storing match ' + str(match['match_id']) + ' record to the database...') dbmapper.insert_match_into_database(match)
def __retrieve_subroutine(start_from_match, to_match, key): """get matches details in a range :param start_from_match: the match id to start from :param to_match: the match id that ends up with :return: null """ start_time = time.time() last_retrieved_id = start_from_match api = dota2api.Dota2api(key) # write the retrieved match ids into a log file file = open('matches.log', 'a') while True: # matches are the first 100 results from last_retrieved_id, descending while True: try: matches = api.get_match_history(start_at_match_id=last_retrieved_id, min_players=10, game_mode=1, matches_requested=100)['matches'] except Exception: print('exception occurred, retry get_match_history after 5 seconds...') time.sleep(5) continue break match_ids = [] for match in matches: match_ids.append(match['match_id']) print("retrieving matches from " + str(last_retrieved_id)) print("the next 100 matches: ") for match_id in match_ids: if match_id == to_match: return print("processing match " + str(match_id)) while True: try: match_details = api.get_match_details(match_id) except Exception: print('exception occurred, retry get_match_details after 5 seconds...') time.sleep(5) continue break # do not record the game that is not finished normally players = match_details['players'] finished = True for player in players: if player['leaver_status'] != 0: finished = False print("match " + str(match_id) + " is not finished normally") break if finished: print(match_details) print('Storing match ' + str(match_id) + ' record to the database...') dbmapper.insert_match_into_database(match_details) file.write(str(match_id) + '\n') last_retrieved_id = match_ids[-1] end_time = time.time() time_elapsed = end_time - start_time print("time elapsed: " + str(time_elapsed)) file.close()