/
WowApiHelper.cs
405 lines (308 loc) · 13.9 KB
/
WowApiHelper.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Runtime.Caching;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Configuration;
namespace WoWApiHelper
{
public static class WowApiHelper
{
public static IEnumerable<CharacterModel> GetGuildMembers()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
List<CharacterModel> characters = cache["guildMembers"] as List<CharacterModel>;
if (characters == null)
{
characters = new List<CharacterModel>();
foreach (KeyValuePair<string, string> kvp in settings.Guilds)
{
characters.AddRange(GetGuildMembers(settings.Region, kvp.Key, kvp.Value));
}
cache.Set("guildMembers", characters, DateTimeOffset.Now.AddHours(1));
}
return characters;
}
private static IEnumerable<CharacterModel> GetGuildMembers(RegionsLookup region, string realm, string guildName)
{
List<CharacterModel> characters = new List<CharacterModel>();
dynamic guild = GetGuildInfo(region, realm, guildName);
foreach (dynamic character in guild["members"])
{
try
{
string name = character["character"]["name"];
CharacterModel c = GetCharacter(region, realm, name);
if (c != null)
{
characters.Add(c);
}
}
finally { }
}
return characters;
}
public static IEnumerable<dynamic> GetGuildMembersDetails()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
List<dynamic> characters = cache["guildMembers"] as List<dynamic>;
if (characters == null)
{
characters = new List<dynamic>();
foreach (KeyValuePair<string, string> kvp in settings.Guilds)
{
characters.AddRange(GetGuildMembersDetails(settings.Region, kvp.Key, kvp.Value));
}
cache.Set("guildMembers", characters, DateTimeOffset.Now.AddHours(1));
}
return characters;
}
private static IEnumerable<dynamic> GetGuildMembersDetails(RegionsLookup region, string realm, string guildName)
{
List<dynamic> characters = new List<dynamic>();
dynamic guild = GetGuildInfo(region, realm, guildName);
foreach (dynamic character in guild["members"])
{
try
{
string name = character["character"]["name"];
dynamic c = GetCharacterDetail(region, realm, name);
if (c != null)
{
characters.Add(c);
}
}
catch { }
}
return characters;
}
public static IEnumerable<dynamic> GetGuildInfo()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
List<dynamic> result = new List<dynamic>();
foreach (KeyValuePair<string, string> kvp in settings.Guilds)
{
result.Add(GetGuildInfo(settings.Region,kvp.Key,kvp.Value));
}
return result;
}
private static dynamic GetGuildInfo(RegionsLookup region, string server, string guild)
{
string key =string.Format("guild_{0}_{1}_{2}", region, server, guild);
ObjectCache cache = MemoryCache.Default;
dynamic item = cache[key];
if (item == null)
{
string requestUrl = string.Format("{0}/guild/{1}/{2}?fields=news,members,achievements,challenge", GetBaseRequestURL(region), server, guild);
item = MakeRequest(requestUrl);
cache.Set(key, item, DateTimeOffset.Now.AddHours(1));
}
return item;
}
public static dynamic GetCharacterRaces()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
dynamic item = cache["characterraces"];
if (item == null)
{
string requestUrl = string.Format("{0}/data/character/races", GetBaseRequestURL(settings.Region));
item = MakeRequest(requestUrl);
cache.Set("characterraces", item, DateTimeOffset.Now.AddDays(7));
}
return item;
}
public static dynamic GetCharacterClasses()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
dynamic item = cache["characterclasses"];
if(item==null)
{
string requestUrl = string.Format("{0}/data/character/classes", GetBaseRequestURL(settings.Region));
item = MakeRequest(requestUrl);
cache.Set("characterclasses",item,DateTimeOffset.Now.AddDays(7));
}
return item;
}
public static dynamic GetCharacterAchievements()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
dynamic item = cache["characterachievements"];
if (item == null)
{
string requestUrl = string.Format("{0}/data/character/achievements", GetBaseRequestURL(settings.Region));
item = MakeRequest(requestUrl);
cache.Set("characterachievements", item, DateTimeOffset.Now.AddDays(7));
}
return item;
}
public static dynamic GetGuildAchievements()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
dynamic item = cache["guildachievements"];
if (item == null)
{
string requestUrl = string.Format("{0}/data/guild/achievements", GetBaseRequestURL(settings.Region));
item = MakeRequest(requestUrl);
cache.Set("guildachievements", item, DateTimeOffset.Now.AddDays(7));
}
return item;
}
private static dynamic MakeRequest(string requestUrl)
{
try
{
WebClient client = new WebClient();
string result = client.DownloadString(requestUrl);
JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic item = jss.Deserialize<dynamic>(result);
return item;
}
catch (WebException webEx)
{
throw webEx;
}
}
/// <summary>
/// Get's the base url to the Blizzard API for the given region.
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
private static string GetBaseRequestURL(RegionsLookup region)
{
string r=string.Empty;
switch (region)
{
case RegionsLookup.EU:
r = "eu";
break;
case RegionsLookup.US:
r = "us";
break;
}
return string.Format("http://{0}.battle.net/api/wow", r);
}
public static dynamic GetCharacterDetail(RegionsLookup region, string server, string name)
{
string requestUrl = string.Format("{0}/character/{1}/{2}?fields=items", GetBaseRequestURL(region), server, name);
return MakeRequest(requestUrl);
}
public static CharacterModel GetCharacter(RegionsLookup region, string server, string name)
{
string key = string.Format("character_{0}_{1}_{2}", region, server, name);
ObjectCache cache = MemoryCache.Default;
CharacterModel item = cache[key] as CharacterModel;
if (item == null)
{
string requestUrl = string.Format("{0}/character/{1}/{2}", GetBaseRequestURL(region), server, name);
try
{
WebClient client = new WebClient();
string result = client.DownloadString(requestUrl);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CharacterModel));
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(result)))
{
item = serializer.ReadObject(ms) as CharacterModel;
}
DateTimeOffset dto = new DateTimeOffset();
dto.AddHours(1);
cache.Set(key, item, DateTimeOffset.Now.AddMinutes(30));
}
catch (Exception ex)
{
}
}
return item;
}
public static List<GuildNews> GetGuildNews()
{
ApiAccessSettings settings = SettingsHelper.GetSettings();
ObjectCache cache = MemoryCache.Default;
List<GuildNews> guildNews = cache["guildNews"] as List<GuildNews>;
if (guildNews == null)
{
guildNews = new List<GuildNews>();
foreach (KeyValuePair<string, string> kvp in settings.Guilds)
{
string requestUrl = string.Format("{0}/guild/{1}/{2}?fields=news", GetBaseRequestURL(settings.Region), kvp.Key, kvp.Value);
WebClient client = new WebClient();
string result = client.DownloadString(requestUrl);
JavaScriptSerializer jss = new JavaScriptSerializer();
var item = jss.Deserialize<dynamic>(result);
foreach (var newsItem in item["news"])
{
string middle = string.Empty;
ItemDetail itemDetail;
switch ((String)newsItem["type"])
{
case "itemPurchase":
itemDetail = GetItemDetail(newsItem["itemId"]);
middle = string.Format("{0} has bought <a href='http://www.wowhead.com/item={1}' rel='item={1}'>{2}</a>", newsItem["character"], itemDetail.ID, itemDetail.Name);
break;
case "itemLoot":
itemDetail = GetItemDetail(newsItem["itemId"]);
middle = string.Format("{0} has found <a href='http://www.wowhead.com/item={1}' rel='item={1}'>{2}</a>", newsItem["character"], itemDetail.ID, itemDetail.Name);
break;
case "itemCraft":
itemDetail = GetItemDetail(newsItem["itemId"]);
middle = string.Format("{0} has crafted <a href='http://www.wowhead.com/item={1}' rel='item={1}'>{2}</a>", newsItem["character"], itemDetail.ID, itemDetail.Name);
break;
case "playerAchievement":
middle = string.Format("{0} has earned the achievement <a href='http://www.wowhead.com/achievement={1}' rel='achievement={1}'>{2}</a>", newsItem["character"], newsItem["achievement"]["id"], newsItem["achievement"]["title"]);
break;
case "guildAchievement":
middle = string.Format("The guild has earned the achievement <a href='http://www.wowhead.com/achievement={1}' rel='achievement={1}'>{2}</a>", newsItem["character"], newsItem["achievement"]["id"], newsItem["achievement"]["title"]);
break;
case "guildLevel":
middle = string.Format("The guild has reached level {0}", newsItem["levelUp"]);
break;
}
guildNews.Add(new GuildNews() { Text = middle });
}
}
//Add to cache
cache.Set("guildNews", guildNews, DateTimeOffset.Now.AddMinutes(30));
}
return guildNews;
}
public static ItemDetail GetItemDetail(int ID)
{
string requestUrl = string.Format("http://eu.battle.net/api/wow/item/{0}", ID);
WebClient client = new WebClient();
string result = client.DownloadString(requestUrl);
JavaScriptSerializer jss = new JavaScriptSerializer();
var item = jss.Deserialize<dynamic>(result);
ItemDetail itemDetail = new ItemDetail();
itemDetail.ID = ID;
itemDetail.Name = (string)item["name"];
return itemDetail;
}
}
[DataContract()]
public class GuildNews : IExtensibleDataObject
{
[DataMember(Name = "text")]
public string Text { get; set; }
public ExtensionDataObject ExtensionData { get; set; }
}
[DataContract()]
public class ItemDetail : IExtensibleDataObject
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "id")]
public int ID { get; set; }
public ExtensionDataObject ExtensionData { get; set; }
}
}