-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamapi.py
112 lines (99 loc) · 2.98 KB
/
steamapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'''
Thin wrapper to make requests to Steam's web API.
Prerequisites:
Set an appropriate STEAM_APIKEY environment variable.
Requires Tornado framework (running IOLoop) for async HTTP requests
'''
import logging
import sys
import os
import json
from urllib.parse import urlencode
from tornado.httpclient import AsyncHTTPClient
from tornado import gen
#The Steam API key is mandatory (obtain through a Steam account)
APIKEY = os.environ.get('STEAM_APIKEY')
if not APIKEY:
print("Specify the 'STEAM_APIKEY' environment variable.")
sys.exit(1)
logging.info("STEAM_APIKEY={}".format(APIKEY))
BASE_URL = 'http://api.steampowered.com'
STEAM_REMOTE_STORAGE = 'ISteamRemoteStorage'
DOTA2_ID = '570'
DOTA2_MATCH = 'IDOTA2Match_' + DOTA2_ID
DOTA2_ECON = 'IEconDOTA2_' + DOTA2_ID
CLIENT = AsyncHTTPClient()
def build_endpoint(endpoint):
'''
Builds a conforming URL for this API, game ID, and method
'''
req = '{}/{}'.format(BASE_URL, endpoint)
return req
@gen.coroutine
def request(url, params={}):
'''
Asynchronously make a request to any URL to get arbitrary resources
using AsyncHTTPClient.
'''
req = '{}?{}'.format(url, urlencode(params))
logging.debug(req)
res = yield CLIENT.fetch(req)
return res.body
@gen.coroutine
def async_request(url, params={}):
'''
Asynchronously make a request to Steam API.
Decodes the result and returns it as a dict.
'''
query = dict(params, key=APIKEY)
req = '{}?{}'.format(url, urlencode(query))
logging.debug(req)
res = yield CLIENT.fetch(req)
#TODO: handle exceptions
return json.loads(res.body.decode())
@gen.coroutine
def get_live_league_games():
'''
Calls GetLiveLeagueGames endpoint which details a list of
"league" games in progress.
Returns a Future containing the response body.
'''
logging.debug('')
url = build_endpoint('/'.join([DOTA2_MATCH, 'GetLiveLeagueGames', 'v1']))
data = yield async_request(url)
#logging.debug(data) #TODO: remove
return data
@gen.coroutine
def get_heroes():
'''
Calls GetHeroes to get the list of hero IDs and names
'''
logging.debug('')
url = build_endpoint('/'.join([DOTA2_ECON, 'GetHeroes', 'v1']))
data = yield async_request(url)
logging.debug(data) #TODO: remove
return data
@gen.coroutine
def get_league_listing():
'''
Calls GetLeagueListing for data of DotaTV supported leagues.
Returns a list of leagues
'''
logging.debug('')
url = build_endpoint('/'.join([DOTA2_MATCH, 'GetLeagueListing', 'v1']))
data = yield async_request(url)
#logging.debug(data) #TODO: remove
return data
@gen.coroutine
def get_ugc_file_details(ugcid):
'''
Get UGC file details (filename, download path) for a Dota2 UGC File
'''
logging.debug('')
url = build_endpoint('/'.join([STEAM_REMOTE_STORAGE,
'GetUGCFileDetails', 'v1']))
data = yield async_request(url, {
'appid': DOTA2_ID,
'ugcid': str(ugcid),
})
return data