python dota2 data 3 download winner data
Goal: Download five heroes from each winner or loser for subsequent analysis.
Because get_match_history() does not include wins or losses in the return, but also uses get_for each matchMatch_Details() query, less efficient.
Use another API to query the contest, get_match_history_by_seq_num():
{
status
1 - Success
8 - Matches_requested must be greater than 0
statusDetail - Message explaining a status that is not equal to 1
[matches] - See get_match_details()
}
This function queries directly from the specified sequence_The game started with ID and returned contains get_Match_Contest details in details ().(sequence_id and match_id is different)
Take Start Competition Sequence_The ID is 310000 000 000, queried for November 16, 2017, a few days after the release of version 7.07.
There is a file for this numberLatest.datFor reading and updating before and after each query, to facilitate interruption and continued download.
Get the data and process it so that each row stores the ID of the winner's five heroes, then the ID of the loser's five heroes, and finally the sequence of the match_Id, stored in this formMatches.datMedium.
import dota2api
import time
api = dota2api.Initialise()
counter = 0
while 1:
#Read the sequence of the latest downloaded match_ID
f_latest = open('latest.dat', 'r')
latest = int(f_latest.readline())
latest += 1
f_latest.close()
#Call API to get match information
while(1):
try:
data_got = api.get_match_history_by_seq_num(matches_requested = 50, start_at_match_seq_num = latest)
except (Exception):
print('error, wait 3 seconds.******************************************')
time.sleep(3)
continue
else:
break
matches = data_got['matches']
#write file
f_data = open('matches.dat', 'a')
#For each match acquired
for m in matches:
#Competition ID
id = m['match_id']
heroes = []
#Winner
win = m['radiant_win']
#All players
players = m['players']
#Exclude numbers less than 10, such as solo
if len(players) != 10:
print('player num error: ', len(players))
continue
#Get all the heroes on stage
for p in players:
hero_id = p['hero_id']
heroes.append(hero_id)
#Twilight Heroes
radiant_team = heroes[0:5]
#Night hero
dire_team = heroes[5:10]
#Sort by ID
radiant_team.sort()
dire_team.sort()
#Eliminate hero ID error
if radiant_team[0] == 0:
print('player hero error: 0')
continue
if dire_team[0] == 0:
print('player hero error: 0')
continue
#Store hero ID s by winner and loser
if win:
win_team = radiant_team
lose_team = dire_team
else:
win_team = dire_team
lose_team = radiant_team
#Sequence_of Current CompetitionID
latest = m['match_seq_num']
counter += 1
print(counter, id, win, win_team, lose_team, latest)
#write file
for h in win_team:
f_data.write(str(h) + ' ')
for h in lose_team:
f_data.write(str(h) + ' ')
f_data.write(str(latest))
f_data.write('\n')
#To updateLatest.dat
f_update = open('latest.dat', 'w')
f_update.writelines(str(latest))
f_update.close()
f_data.close()
After runningMatches.dat: