public async Task<ActionResult> Search(string questText, bool? alliance, bool? horde) { var search = new QuestQuery() { Text = questText, }; if (alliance == true) search.FactionSide = FactonSide.Alliance; else if (horde == true) search.FactionSide = FactonSide.Horde; var searcher = new WowheadClient(); var quests = await searcher.SearchQuests(search); var viewModel = new QuestSearchViewModel() { SearchExpression = questText, Quests = quests, }; return View(viewModel); }
public async Task<IEnumerable<Quest>> SearchQuests(QuestQuery query) { var resultList = new List<Quest>(); using (WebClient client = CreateWebClient()) { var searchTerm = HttpUtility.UrlEncode(query.Text); var address = "http://www.wowhead.com/quests?filter=na=" + searchTerm; if (query.FactionSide != FactonSide.All) address += ";si=" + (int)query.FactionSide; if (query.IsDaily) address += ";cr=27;crs=1;crv=0"; var htmlResult = await client.DownloadStringTaskAsync(address); var jsonStart = htmlResult.IndexOf("new Listview({template: 'quest'"); if (jsonStart > 0) { jsonStart += "new Listview(".Length; var jsonEnd = htmlResult.IndexOf("});", jsonStart); if (jsonEnd > 0) { var jsonResult = htmlResult.Substring(jsonStart, 1 + jsonEnd - jsonStart); var serializer = new JavaScriptSerializer(); var jsonObject = (Dictionary<string, object>)serializer.DeserializeObject(jsonResult); var rawQuests = (object[])jsonObject["data"]; foreach (Dictionary<string, object> rawQuest in rawQuests) { var quest = new Quest() { Id = Convert.ToInt64(rawQuest["id"]), Name = rawQuest["name"].ToString(), }; if (rawQuest.ContainsKey("reqlevel")) quest.RequiredLevel = Convert.ToInt32(rawQuest["reqlevel"]); if (rawQuest.ContainsKey("level")) quest.Level = Convert.ToInt32(rawQuest["level"]); if (rawQuest.ContainsKey("money")) quest.Gold = Convert.ToInt32(rawQuest["money"]); if (rawQuest.ContainsKey("xp")) quest.Experience = Convert.ToInt32(rawQuest["xp"]); if (rawQuest.ContainsKey("rawFaction")) { var rawFaction = Convert.ToInt32(rawQuest["side"]); if (rawFaction >= 1 && rawFaction <= 2) quest.Faction = (FactonSide)rawFaction; } resultList.Add(quest); } } } return resultList; } }