Tutorial¶
Getting started¶
Initialize wrapper via environment variable¶
Create a new environment variable D2_API_KEY
and set its value to the API key. You can then, just use
the following code to initialize the wrapper.
api = d2api.APIWrapper()
Initialize wrapper inline¶
Literally just initialize the wrapper inline. That’s about it.
# overrides the environment variable key
api = d2api.APIWrapper('YOUR_API_KEY')
Unparsed response¶
There’s a good chance you’d like your responses au naturel. Just set parse_response = False
.
The wrapper returns the response text as is (without using the built-in json parser).
api = d2api.APIWrapper(api_key = 'YOUR_API_KEY', parse_response = False)
Note
While it is highly recommended that a json response have unique key-value pairs, it is not mandatory that they be unique.
Some responses of the Steam WebAPI consists of such repeated key-value pairs. Use d2api.src.util.decode_json
to parse
these results to avoid losing content.
Examples¶
Hero frequency in last 100 games¶
import d2api
from d2api.src import entities
api = d2api.APIWrapper()
# fetch latest matches
match_history = api.get_match_history()
# get frequency of heroes played in the latest 100 games
heroes = {}
for match in match_history['matches']:
for player in match['players']:
hero_id = player['hero']['hero_id']
if not hero_id in heroes:
heroes[hero_id] = 0
heroes[hero_id] += 1
# print hero frequency by name
for hero_id, freq in heroes.items():
print(entities.Hero(hero_id)['hero_name'], freq)
Using the API without the API¶
from d2api.src import entities
# Hero/Item/Ability information is available without having to specify a key
print(entities.Hero(67)['hero_name'])
print(entities.Item(208)['item_aliases'])
print(entities.Ability(6697)['ability_name'])
# Use steam32/steam64 IDs interchangeably
steam_account = entities.SteamAccount(1020002)
print(steam_account['id32'], steam_account['id64'])
Matches without leavers¶
# Fetch last 100 very high skill games and filter out games that have leavers
import d2api
api = d2api.APIWrapper()
vhs = api.get_match_history(skill = 3)
matches = [api.get_match_details(m['match_id']) for m in vhs['matches']]
# now filter out matches that have leavers
matches = [m for m in matches if not m.has_leavers()]
# number of matches that remain
print(len(matches))
# print the first match
print(matches[0])