-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSteamWebAPIClient.cs
252 lines (184 loc) · 8.51 KB
/
SteamWebAPIClient.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SteamWebAPIWrapper.Data;
using SteamWebAPIWrapper.Responses;
namespace SteamWebAPIWrapper
{
public class SteamWebAPIClient
{
private string _key;
private DateTime _lastRequest;
private HttpClient _webClient;
private const int MillisecondsBetweenCalls = 1100; // slightly over 1 sec to ensure calls don't fail as often.
private const string BaseAddress = "https://api.steampowered.com/";
public SteamWebAPIClient(string key)
{
_key = key;
_lastRequest = new DateTime();
_webClient = new HttpClient { BaseAddress = new Uri(BaseAddress) };
}
private Task<GetMatchHistoryResponse> GetMatchHistoryPaged(int? leagueId = null, int? startAtMatch = null)
{
const string url = "IDOTA2Match_570/GetMatchHistory/v001/?key={0}";
var sb = new StringBuilder();
sb.AppendFormat(url, _key);
if (leagueId != null)
sb.AppendFormat("&league_id={0}", leagueId);
if (startAtMatch != null)
sb.AppendFormat("&start_at_match_id={0}", startAtMatch);
return GetRequest<GetMatchHistoryResponse>(sb.ToString());
}
public async Task<List<Match>> GetNewMatches(int leagueId, int lastSeenMatchId = 0)
{
var ret = new List<Match>();
var firstPage = await GetMatchHistoryPaged(leagueId);
ret = firstPage.Matches.Where(x => x.MatchId > lastSeenMatchId).ToList();
if (firstPage.ResultsRemaining > 0 && !firstPage.Matches.Any(x => x.MatchId < lastSeenMatchId)) // don't need to go to next page!
{
int resultsRemaining = firstPage.ResultsRemaining;
int lastMatchId = firstPage.Matches.Last().MatchId - 1; // query one below the last one returned
while (resultsRemaining > 0)
{
var page = await GetMatchHistoryPaged(leagueId, lastMatchId);
ret.AddRange(firstPage.Matches.Where(x => x.MatchId > lastSeenMatchId));
lastMatchId = page.Matches.Last().MatchId - 1; // as above
if (page.Matches.Any(x => x.MatchId < lastSeenMatchId))
break;
resultsRemaining = page.ResultsRemaining;
}
}
return ret;
}
public async Task<List<Match>> GetNewMatches(int leagueId, List<int> existingMatchIds)
{
var ret = new List<Match>();
var firstPage = await GetMatchHistoryPaged(leagueId);
ret = firstPage.Matches.Where(x => !existingMatchIds.Contains(x.MatchId) && x.Players.Count == 10).ToList();
if (firstPage.ResultsRemaining > 0 && ret.Any())
{
int resultsRemaining = firstPage.ResultsRemaining;
int lastMatchId = firstPage.Matches.Last().MatchId - 1; // query one below the last one returned
while (resultsRemaining > 0)
{
var page = await GetMatchHistoryPaged(leagueId, lastMatchId);
var retThisPage = page.Matches.Where(x => !existingMatchIds.Contains(x.MatchId) && x.Players.Count == 10).ToList();
ret.AddRange(retThisPage);
lastMatchId = page.Matches.Last().MatchId - 1; // as above
if (!retThisPage.Any())
break;
resultsRemaining = page.ResultsRemaining;
}
}
return ret;
}
public async Task<List<Match>> GetAllMatches(int leagueId)
{
var ret = new List<Match>();
var firstPage = await GetMatchHistoryPaged(leagueId);
ret.AddRange(firstPage.Matches);
if (firstPage.ResultsRemaining > 0)
{
int last = ret.Last().MatchId - 1; // get one lower than the last as the argument for the next page
while (true)
{
var page = await GetMatchHistoryPaged(leagueId, last);
ret.AddRange(page.Matches);
if (page.ResultsRemaining > 0)
last = ret.Last().MatchId - 1; // as above
else
break;
}
}
return ret;
}
public Task<Match> GetMatchDetails(int matchId)
{
const string url = "IDOTA2Match_570/GetMatchDetails/V001/?key={0}&match_id={1}";
return GetRequest<Match>(string.Format(url, _key, matchId));
}
public async Task<List<ScheduledLeagueGame>> GetScheduledLeagueGames()
{
const string url = "IDOTA2Match_570/GetScheduledLeagueGames/V001/?key={0}";
var games = await GetRequest<ScheduledLeageGames>(string.Format(url, _key));
return games.Games;
}
public async Task<List<League>> GetLeagueListing()
{
const string url = "IDOTA2Match_570/GetLeagueListing/v0001/?key={0}";
var leagues = await GetRequest<LeagueList>(string.Format(url, _key));
return leagues.Leagues;
}
public async Task<List<LiveLeagueGame>> GetLiveLeagueGames()
{
const string url = "IDOTA2Match_570/GetLiveLeagueGames/v0001/?key={0}";
var leagues = await GetRequest<LiveLeagueGames>(string.Format(url, _key));
if (leagues != null)
{
return leagues.Games;
}
return null;
}
private async Task<T> GetRequest<T>(string url)
{
var diff = DateTime.Now.Subtract(_lastRequest);
if (diff.TotalMilliseconds < MillisecondsBetweenCalls)
await Task.Delay((int)(MillisecondsBetweenCalls - diff.TotalMilliseconds)); // wait 1 sec between calls, as per fair use agreement.
_lastRequest = DateTime.Now;
string json = "";
// Ocassionally the request returns 503 because the server thinks we made too many requests
// this simply tries 3 times, waiting progressively longer // ToDo: Could be improved upon?
for (int i = 1; i <= 5; i++)
{
try
{
json = await _webClient.GetStringAsync(url);
break;
}
catch (Exception e)
{
if (i == 3)
return default(T);
// can't await in catch block (wait for next C# version!)
}
await Task.Delay(i * 10000);
}
var ret = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<SteamAPIResponse<T>>(json));
if (ret.Result != null)
return ret.Result; // dota data
return ret.Response; // different for account API, Todo: will need to look into JsonConverter in the future.
}
public async Task<List<int>> GetActiveLeagueIds()
{
var liveGames = await GetLiveLeagueGames();
var scheduledGames = await GetScheduledLeagueGames();
if (liveGames != null && scheduledGames != null)
{
var liveLeagues = liveGames.Select(x => x.LeagueId);
var scheduledLeagues = scheduledGames.Select(x => x.LeagueId);
return liveLeagues.Union(scheduledLeagues).Distinct().ToList();
}
return new List<int>();
}
public async Task<List<PlayerProfile>> GetPlayerSummaries(List<long> steamIds) // please convert with GetSteamId64
{
const string url = "ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}";
var playerSummaries = await GetRequest<PlayerSummaries>(string.Format(url, _key, string.Join(",", steamIds)));
return playerSummaries.Players;
}
// according to http://dev.dota2.com/showthread.php?t=58317
public static long GetSteamId64(int steamId32)
{
return steamId32 + 76561197960265728;
}
public static int GetSteamId32(long steamId64)
{
return (int)(steamId64 - 76561197960265728);
}
}
}