您的位置:首页 > 编程语言 > Python开发

python dota2数据 1 API

2017-11-17 11:04 477 查看

python dota2数据 1 比赛历史

注册API Key

https://steamcommunity.com/dev/apikey

安装dota2api

使用python的dota2api库来调用API:
pip3 install dota2api


参考文档

http://dota2api.readthedocs.io/en/latest/installation.html

获取最近5场使用英雄ID

添加API KEY:

在linux中:
export D2_API_KEY=xxxxx


也可在python文件中初始化时将KEY作为dota2api.Initialise()的参数。

查找API

查找相关函数发现查询比赛用到的函数为get_match_history(),其参数为:

account_id 包含玩家ID

hero_id 查询的对应英雄的比赛,每个英雄对应一个ID

game_mode 游戏模式

skill 水平等级

min_players 最少玩家数量

league_id 所属联赛ID

start_at_match_id 起始比赛ID

matches_requested 查询数量,默认为100

tournament_games_only 是否只包含联赛比赛

所有参数均为可选。

在这里我们需要用到的仅有account_id和matches_requested。

再查看get_matches_history()函数的返回内容:

{
num_results             - Number of matches within a single response
total_results           - Total number of matches for this query
results_remaining       - Number of matches remaining to be retrieved with subsequent API calls
[matches]               - List of matches for this response
{
match_id            - Unique match ID
match_seq_num       - Number indicating position in which this match was recorded
start_time          - Unix timestamp of beginning of match
lobby_type          - See lobby_type table
[player]            - List of players in the match
{
account_id      - Unique account ID
player_slot     - Player's position within the team
hero_id         - Unique hero ID
}
}
}


返回的是一个dictionary对象,其中最主要的是matches键,该值为一个list对象,其中包含了所查询的所有比赛信息。解析得到每一场比赛对应的dictionary,再进一步解析得到比赛的详细信息,如比赛ID,所有玩家ID和所用英雄ID。

其中matches中的players在原文档中显示为player,经测试应为players。

get_match_history.py

import dota2api
import sys
#初始化api
api = dota2api.Initialise()

#接收命令行参数
#我的dota2用户ID
my_id = sys.argv[1]
#请求的比赛场数
matches = sys.argv[2]
#调用get_match_history函数
match_history = api.get_match_history(account_id = my_id, matches_requested = matches)
#解析获得的match_history字典对象,取其中的matches键
matches = match_history['matches']
#逐个解析每场比赛
for m in matches:
#获取比赛ID
m_id = m['match_id']
#获取所有玩家
players = m['players']
#逐个解析每个玩家
for p in players:
#寻找自己
if p['account_id'] == int(my_id):
#获取所用英雄ID
h_id = p['hero_id']
break
print('match id: ', m_id, 'hero: ', h_id)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python dota2