Skip to content

Instantly share code, notes, and snippets.

@NikolasTzimoulis
Created January 16, 2015 15:05
Show Gist options
  • Save NikolasTzimoulis/9e1337b4336a62de5de4 to your computer and use it in GitHub Desktop.
Save NikolasTzimoulis/9e1337b4336a62de5de4 to your computer and use it in GitHub Desktop.
Dota API, Get match history by sequence number
import requests, json, pickle, time, datetime, sqlite3 as lite, sys
steamKeyFileName = 'steamwebapikey.txt'
matchesFileName = 'matches.pkl'
# get steam api key
try:
steamKeyFile = open(steamKeyFileName, 'rb')
steamKey = pickle.load(steamKeyFile)
steamKeyFile.close()
except:
print "Steam API Key:",
steamKey = raw_input()
steamKeyFile = open(steamKeyFileName, 'wb')
pickle.dump(steamKey, steamKeyFile)
steamKeyFile.close()
# connect to API
client = requests.session()
address = r'https://api.steampowered.com/IDOTA2Match_570/GetMatchHistoryBySequenceNum/v0001/'
# initialise database for local storage
con = lite.connect('matches.db')
with con:
cur = con.cursor()
# cur.execute("DROP TABLE IF EXISTS Matches")
# cur.execute("CREATE TABLE Matches(match_id INT PRIMARY KEY, match_seq_num INT, start_time INT, duration INT, game_mode INT, lobby_type INT, leagueid INT, human_players INT, player0_hero_id INT, player1_hero_id INT, player2_hero_id INT, player3_hero_id INT, player4_hero_id INT, player5_hero_id INT, player6_hero_id INT, player7_hero_id INT, player8_hero_id INT, player9_hero_id INT, radiant_win INT)")
startId = 501088752
numPages = 1000
fails = 0
for i in range(numPages):
parameters = {'key': steamKey, 'start_at_match_seq_num': startId}
r = client.get(address, params=parameters)
page = json.loads(r.content)['result']['matches']
for match in page:
try:
matchData = [ match['match_id'], match['match_seq_num'], match['start_time'], match['duration'], match['game_mode'], match['lobby_type'], match['leagueid'], match['human_players'] ]
playerData = [-1]*10
for p in range(10):
if match['players'][p]['player_slot'] < 128:
teamOffset = 0
else:
teamOffset = 5
match['players'][p]['player_slot'] -= 128
playerData[match['players'][p]['player_slot']+teamOffset] = match['players'][p]['hero_id']
matchData.extend(playerData)
matchData.append(match['radiant_win'])
print(datetime.datetime.fromtimestamp(match['start_time']).strftime('%Y-%m-%d %H:%M:%S')), matchData
cur.execute("INSERT INTO Matches VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", matchData)
con.commit()
except:
print "Failed to add match."
fails += 1
startId = page[-1]['match_seq_num'] + 1
print 'End of page', i+1
time.sleep(1)
con.close()
if fails > 0: print "Encountered " + str(fails) + " matches that could not be added."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment