def get_leagues(): api = dota2api.Initialise(API_KEY) leagues = api.get_league_listing()['leagues'] write_json("data/leagues.json", leagues) for league in leagues: file_path = "data/leagues/" + str(league['leagueid']) + ".json" if not os.path.exists(file_path): # if league['leagueid'] == 1640: data = {} league_data = api.get_match_history(league_id=league['leagueid'], matches_requested=100, tournament_games_only='1') while league_data['results_remaining'] > 0: print(league_data['total_results']) print(league_data['results_remaining']) for match in league_data['matches']: match['detail'] = api.get_match_details( match_id=match['match_id']) data.update({m['match_id']: m for m in league_data['matches']}) league_data = api.get_match_history( league_id=league['leagueid'], start_at_match_id=league_data['matches'][-1]['match_id'], matches_requested=100, tournament_games_only='1') write_json(file_path, data)
def __init__(self, match_id): self.api = dota2api.Initialise(settings.STEAM_API_KEY) self.results = self.api.get_match_details(match_id=match_id) self.radiant_win = self.results["radiant_win"] self.is_valid = True self.players = {"radiant": [], "dire": []} self.game_mode = self.results["game_mode"]
def __init__(self): with open('data/key.json', 'r') as file: key = json.load(file)[1] with open('../ref/heroes.json', 'r') as file: self.heroes = json.load(file) self.api = dota2api.Initialise(key) self.location = 'server_log.txt'
def run(self): api = dota2api.Initialise(STEAM_KEY) try: match = api.get_match_details(match_id=self.matchid) print("match data ready") if match['lobby_name'] == 'Ranked': #some code to insert data into Firestore try: db = firestore.client() doc_ref = db.collection('match_data').document( 'patch').collection(f'{self.dotapatch}').document( f"{match['match_id']}") doc_count_ref = db.collection('match_data').document( 'patch').collection(f'{self.dotapatch}').document( 'count_table') num_of_docs = doc_count_ref.get(['count']).get('count') doc_ref.set(match) doc_count_ref.update({'count': num_of_docs + 1}) self.task_complete = True print(f"inserted match(id:{self.matchid})") except: print("failed to insert match-data.") self.task_complete = True else: self.task_complete = True print(f"it is not ranked(id:{self.matchid})") except: print("match id not found")
def get_raw_matches(): f = open(API_KEY_FILE, 'r') api = dota2api.Initialise(f.read()) matches = api.get_match_history_by_seq_num(matches_requested=NUM_MATCHES) # soup = json.loads(str(matches)) pp = pprint.PrettyPrinter(depth=6) pp.pprint(matches)
def samples(max=10000): """ Returns dictionary of match data up to max, default 1000 """ list_results = [] with open( "data/key.json", "r" ) as file: # Requires web api key to be saved in a file named "key" with quotes around the key key = json.load(file) i = 0 while (len(list_results) != max): try: results = dota2api.Initialise(key).get_match_history_by_seq_num( start_at_match_seq_num=(2416543502 - (100 * i))) for j in range(len(results["matches"])): if (results["matches"][j]["duration"] > 900 and results["matches"][j]["duration"] < 3600 and results["matches"][j]["game_mode"] == 22): if (len(list_results) == max): print("Match threshold acquired, saving file...") break else: list_results.append(results["matches"][j]) i += 1 print("Analyzed %d matches; using %d." % (i * 100, len(list_results))) except: pass with open("data/matchdata.json", "w") as file: json.dump(list_results, file) file.close()
def get_doAPI(): try: with open('row_data/doAPI.pickle', 'rb') as f: api = pickle.loads(f.read()) except (FileNotFoundError, TypeError): print(traceback.format_exc()) api = dota2api.Initialise(config.API_TOKEN, language='Zh-cn') return api
def __init__(self): self.api = dota2api.Initialise(API_KEY) self.db = MySQLdb.connect(host=DB_HOST, port=DB_PORT, user=DB_USER, passwd=DB_PASS, db=DB_NAME) self.c = self.db.cursor()
def __init__(self, from_day=2, to_day=1): assert from_day > to_day, "Error" date_max = datetime.today() - timedelta(days=to_day) date_min = datetime.today() - timedelta(days=from_day) self.date_max = calendar.timegm(date_max.utctimetuple()) self.date_min = calendar.timegm(date_min.utctimetuple()) self.api = dota2api.Initialise(settings.STEAM_API_KEY)
def getItems(): api = dota2api.Initialise("F92426FF6096299606F88D87F7B2D0AD") items = api.get_game_items() for item in items['items']: id = item.get('id') name = item.get('localized_name') print(str(id) + " " + name) Item.objects.create(item_id=id, item_name=name)
def getHeroes(): api = dota2api.Initialise("F92426FF6096299606F88D87F7B2D0AD") heroes = api.get_heroes() for hero in heroes['heroes']: id = hero.get('id') name = hero.get('localized_name') print(str(id) + " " + name) Hero.objects.create(hero_id=id, hero_name=name)
def getMatches(matchId, stopNum): api = dota2api.Initialise("C93CC31153AA2B1F5D044EB03E3E13D6") # matchId = mId ### matchSeqNum = 2242825642 outFile = open('dotaMatchasdf.out', 'a') serialLoop(api, matchId, stopNum, outFile) #parallelLoop( api, matchId, stopNum, writer ) outFile.close()
def way2(): api = dota2api.Initialise("EB913C00E4E04F71DF73DE8A05578B83", raw_mode=True,language="zh_CN") data=api.get_heroes() id_name={} for i in data['heroes']: id_name[i['id']]=i['localized_name'] #print(id_name) with open("heroes.txt",'w') as hero: for id,name in id_name.items(): hero.write(str(id)+' '+name+'\n')
def get_live_data_for_match(self, match_id): # Api для доты2 чтобы доставать данные из лайва api = dota2api.Initialise("F976A5435E1C8C0B8F3992D8CCA9B619", executor=self.get_url_from_proxy) """Cоздает переменную data_match, либо из лайва, либо из прошедшего матча""" for game in api.get_live_league_games().get('games'): """Если матч в лайве то беруться данные из лайва""" if game.get('match_id') == match_id: return (game) return (api.get_match_details(match_id))
def index(request): print("Start Dota Api") api = dota2api.Initialise("B279ECFCF25AF5B787A87DB0CCEB5BA5") hist = api.get_match_history(account_id=84331215) for x in range(0,hist["num_results"]): print(x) match = hist["matches"][x] r = Match( match_id=match["match_id"],match_seq_num=match["match_seq_num"],start_time=match["start_time"],lobby_type=match["lobby_type"]) r.save() print(":)") return HttpResponse("Save Success")
def initialise_keys(): # Initialise api keys if D2API_KEY is None: raise NameError( "Dota 2 Api key needs to be set as an environment variable") if OD_KEY is None: raise NameError( "Opendota Api key needs to be set as an environment variable") api = dota2api.Initialise( D2API_KEY, raw_mode=True) #raw mode to get hero id instead of names return api
def final_data(acc_id, n, opendota): api = dota2api.Initialise("8AC9317F6C4C6AA98EECEC8638314A11") if opendota == "dota2api": match_data = pull_match_id(acc_id, n) else: match_data = pull_match_opendota(acc_id, n) match_final_data = get_match_final(match_data) final_df = add_result_data(match_data, match_final_data) full_extra = add_extra_data(final_df) return (full_extra)
def no_environment_variable_set_test(self): # Pass the API key as a positional argument self.api_key = os.environ['D2_API_KEY'] api = dota2api.Initialise(self.api_key) match = api.get_match_details(match_id=988604774) # Do we get a match back mock = RequestMock() stored_match = mock.configure_single_match_result().json_result # Check the response is the same as the stored one self.assertEqual(stored_match['result']['match_id'], match['match_id']) # Check it is our custom dict type self.assertEqual(type(Dota2Dict()), type(match))
def test_raw_mode(self): # Pass the API key as a positional argument self.api_key = os.environ['D2_API_KEY'] api = dota2api.Initialise(self.api_key, raw_mode=True) match = api.get_match_details(match_id=988604774) # Do we get a match back mock = RequestMock() stored_match = mock.configure_single_match_result().json_result # hero_id should not be parsed into hero_name self.assertEqual('hero_name' in match['players'][0], False) self.assertEqual(stored_match['result']['players'][0]['hero_id'], match['players'][0]['hero_id']) # Check it is our custom dict type self.assertEqual(type(Dota2Dict()), type(match))
def __init__(self, bot, channel): super().__init__(bot, channel) api_key = self.bot.config.get('dota2_apikey') self.api = dota2api.Initialise(api_key) db = os.path.join(self.bot.storage_dir, 'dota2.db') if not os.path.isfile(db): open(db, "w+") self.conn = sqlite3.connect(db, check_same_thread=False) self.cur = self.conn.cursor() self._create_table() self.colours = [ "02", # Blue "03", # Green "04", # Red "06", # Purple "08", # Yellow ]
class Info: api=dota2api.Initialise(api_key="my_key",language="ko_KR",raw_mode=False) def __init__(self): pass def setInfo(self,name): pass def setDataSet(self): with open("./dota_info/json/hero.json","w") as f: a_hero=self.api.get_heroes() f.write(json.dumps(a_hero, ensure_ascii=False, indent="\t")) print(json.dumps(a_hero, ensure_ascii=False, indent="\t")) with open("./dota_info/json/item.json","w") as f: a_item=self.api.get_game_items() f.write(json.dumps(a_item, ensure_ascii=False, indent="\t")) print(json.dumps(a_item, ensure_ascii=False, indent="\t"))
def run(): api = dota2api.Initialise(settings.STEAM_API_KEY) response = api.get_heroes() if response["status"] != 200: print("Failed to retrieve hero list.") return for hero_data in response["heroes"]: try: DotaHero.objects.create(id=hero_data["id"], popular_name=hero_data["localized_name"], system_name=hero_data["name"], image=hero_data["url_full_portrait"]) print("Successfully created Hero \'{0}\'".format( hero_data["name"])) except Exception as ex: print("Failed to create Hero \'{0}\', reason: {1}".format( hero_data["name"], str(ex))) continue
def __init__(self): self.players = {} self.key = None self.data = dota2api.Initialise('44D2B9F7C72B1931CC601FF4086C9014') # Attempt to load existing player list from file try: with open("data/players.json", "r") as file: self.players = json.load(file) with open("data/key.json", "r") as file: self.key = json.load(file) except (FileNotFoundError, json.decoder.JSONDecodeError) as e: if e.filename == "data/players.json": with open("data/players.json", "w") as file: json.dump(self.players, file) else: print(e.strerror) file.close()
def get_profile_match_hist(self): api = dota2api.Initialise("BFF23F667B3B31FD01663D230DF11C25") hist = api.get_match_history(account_id=tether.models.UserProfile1.objects.values('steam_id')) # steam id queryset match_list2 = hist # nested_lookup('match_id', recent_matches) ids = nested_lookup('match_id', match_list2) # return in list all ids and dates s_time = nested_lookup('start_time', match_list2) match_list = dict(zip(s_time, ids)) # zip ids and dates into dict num_ids = range(4) num_matches2 = range(5) match_incre2 = [] for i in num_matches2: match_incre2.append('id_match' + str(i)) match_ids = ids[:5] recent_matches2 = dict(zip(match_incre2, match_ids)) matches = {str(k): str(v) for k, v in recent_matches2.items()} print(matches) new_entry = tether.models.NewRecentMatches1(**matches) #new_entry = p_entry.players.create(tether.models.NewRecentMatches(**matches)) new_entry.save() ''' prof = tether.models.UserProfile.objects.values_list('steam_id', flat=True).get(pk=1) prof.save() prof.recent_matches.add(new_entry) ''' prof_id = tether.models.UserProfile1.objects.get(steam_id='101869174') prof_id.save() prof = tether.models.Profiles_Matches(profile_id=prof_id, match_id=new_entry) prof.save() plrs = self.get_match_players() PIM = tether.models.PlayersInMatch(players_id=plrs, match_id=new_entry) PIM.save()
def collect_match_details(threadname, start_sequence_num, end_sequence_num): api = dota2api.Initialise("4B1FD9D1888114755F2A9C7B5788A085") seq_num = start_sequence_num #2945963526 while (seq_num < end_sequence_num): try: print("thread:" + str(threadname) + " get match history from seq num: " + str(seq_num) + " count:100") all = api.get_match_history_by_seq_num(seq_num, matches_requested=100) match_id = "" if all['status'] == 1: for match in all['matches']: match_id = match['match_id'] SteamMemberInfo.get_game_details(match_id) seq_num = match['match_seq_num'] else: print("stopped at " + str(seq_num)) break if seq_num > end_sequence_num: break except Exception as e: print("thread:" + str(threadname) + " " + str(e)) pass #with open("seq_num", 'a') as file: # file.write("start at:" + str(start_sequence_num) + " and ends at: " + str(seq_num)) print("stopped at " + str(seq_num)) with open("seq_num", 'a') as file: file.write("start at:" + str(start_sequence_num) + " and ends at: " + str(seq_num))
def get_live_game_list(): API_KEY = "BAF729E344FED5C902E36A75FC753271" api = dota2api.Initialise(API_KEY, raw_mode=True) try: #test1 = api.get_heroes() #test2 = api.get_league_listing() #test3 = api.get_live_league_games() test4 = api.get_game_items() #test = api.get_live_league_games() print(test4) #json_str = json.dumps(test2) #print(json_str) file_name = open( "C:\\pro\\LX\\testfile\\game_list.txt", 'w', ) #file_name.write(json_str) file_name.close() except: get_live_game_list()
def get(accountId): api = dota2api.Initialise("3696FD54789FF3D37C752351029AD9D9") match = api.get_match_history(account_id=accountId) matchData = json.loads(match.json) matchList = [] heroId = [] heroName = [] playerSlot = [] for i in range(30): matchTime = matchData['matches'][i]['start_time'] matchId = matchData['matches'][i]['match_id'] player = matchData['matches'][i]['players'] nowTime = time.time() if nowTime - matchTime < 86400: matchList.append(matchId) heroes = api.get_heroes() for j in range(10): if player[j]['account_id'] == accountId: heroId.append(player[j]['hero_id']) n = 0 while heroes['heroes'][n]['id'] != player[j]['hero_id']: n = n + 1 heroName.append(heroes['heroes'][n]['localized_name']) playerSlot.append(player[j]['player_slot']) break matchNumber = len(matchList) winNumber = 0 lostNumber = 0 for i in range(matchNumber): matchDetail = api.get_match_details(match_id=matchList[i]) radiantWin = matchDetail['radiant_win'] if playerSlot[i] >> 7 == radiantWin: lostNumber += 1 else: winNumber += 1 return heroName, winNumber, lostNumber
def setUp(self): self.api = dota2api.Initialise()
connection.commit() connection.close() def update_match_in_db(): print('Inside update_match_in_db') has_leavers = False for p in match["players"]: if p["leaver_status"] in [2, 3, 4, 5, 6]: has_leavers = True connection = sqlite3.connect(database, timeout=10) connection.execute( "UPDATE OR IGNORE matches SET game_mode=?, has_leaver=?, duration=?, radiant_win=? WHERE match_id=?", (match["game_mode"], has_leavers, match["duration"], match["radiant_win"], match["match_id"])) connection.commit() connection.close() api = dota2api.Initialise(api_key, raw_mode=True) match_history_thread = Thread(target=match_history) match_history_thread.daemon = True match_details_thread = Thread(target=match_details) match_details_thread.daemon = True match_history_thread.start() match_details_thread.start() print("Starting MatchHistory and MatchDetails threads.") # This is needed (instead of joining the threads) to respond to crl-c while True: time.sleep(1)
def wrong_api_key_test(self): # Test the wrong API key try: dota2api.Initialise("sdfsdfsdf").get_match_history() except APIAuthenticationError: assert True