public async Task<SearchResult> SearchBigTurkishDictionary(BigTurkishDictionaryFilter filter, Action onLoadingStarts, Action onLoadingEnds) { onLoadingStarts(); string responseBody = null; string uri = DictionaryServiceConstants.SITE_ENDPOINT; if (filter.SearchId == null) { uri = uri + "?option=com_bts&arama=kelime"; Dictionary<String, String> parameters = new Dictionary<String, String> { {"gonder", "ARA"}, {"kategori", "verilst"}, {"kelime", System.Net.WebUtility.UrlEncode(filter.SearchString)} }; switch (filter.MatchType) { case BigTurkishDictionaryFilter.MatchTypeFilter.FULL_MATCH: parameters.Add("ayn", "tam"); break; case BigTurkishDictionaryFilter.MatchTypeFilter.PARTIAL_MATCH: parameters.Add("ayn", "dzn"); break; default: parameters.Add("ayn", "tam"); break; } responseBody = await PostRequest(uri, parameters); } else { uri = uri + "?option=com_bts&view=bts&kategori1=veritbn&kelimesec=" + filter.SearchId; responseBody = await GetRequest(uri); } List<Word> words = new List<Word>(); Boolean isSuggestion = false; using (StreamReader reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(responseBody)))) { String responseHtml = reader.ReadToEnd(); // Clear illegal tags as they cause errors while parsing responseHtml = responseHtml.Replace("< ar", "ar"); responseHtml = responseHtml.Replace("<ar", "ar"); responseHtml = responseHtml.Replace("< Ar", "Ar"); responseHtml = responseHtml.Replace("<Ar", "Ar"); HtmlDocument doc = new HtmlDocument(); doc.OptionFixNestedTags = true; doc.LoadHtml(responseHtml); HtmlNode rootNode = doc.DocumentNode .Descendants("div") .Where(div => div.GetAttributeValue("class", "") == "main_body") .First(); if (rootNode != null) { if (filter.MatchType == BigTurkishDictionaryFilter.MatchTypeFilter.PARTIAL_MATCH) { List <HtmlNode> nodes = doc.DocumentNode .Descendants() .Where(node => (node.Name == "a" && node.GetAttributeValue("href", "").StartsWith("/index.php?option=com_bts") && (node.ParentNode.Name != "span" || node.ParentNode.GetAttributeValue("class", "") != "comicm") && node.ParentNode.Name != "li")).ToList(); if (nodes.Count() > 0) { // Check for suggestions foreach (HtmlNode node in nodes) { Word word = new Word(); int id; if (Int32.TryParse(node.GetAttributeValue("href", "").Substring(node.GetAttributeValue("href", "").LastIndexOf("kelimesec=") + 10), out id)) word.Id = id; else word.Id = null; word.Name = node.InnerText.Replace(" ", "").Trim(); word.Origin = String.Empty; word.Description = String.Empty; word.DictionaryName = String.Empty; word.Year = null; words.Add(word); } List<String> pages = doc.DocumentNode .Descendants() .Where(node => (node.Name == "option" && node.ParentNode.GetAttributeValue("name", "") != "ayn")) .Select(node => (node.GetAttributeValue("value", ""))) .ToList(); foreach (String page in pages) { String nextUri = DictionaryServiceConstants.SITE_ENDPOINT + "?option=com_bts&view=bts&kategori1=verilst&ayn1=dzn&konts=" + page + "&kelime1=" + System.Net.WebUtility.UrlEncode(filter.SearchString); responseBody = await GetRequest(nextUri); using (StreamReader nextReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(responseBody)))) { HtmlDocument nextdoc = new HtmlDocument(); nextdoc.OptionFixNestedTags = true; nextdoc.LoadHtml(nextReader.ReadToEnd()); rootNode = nextdoc.DocumentNode .Descendants("div") .Where(div => div.GetAttributeValue("class", "") == "main_body") .First(); if (rootNode != null) { nodes = nextdoc.DocumentNode .Descendants() .Where(node => (node.Name == "a" && node.GetAttributeValue("href", "").StartsWith("/index.php?option=com_bts") && (node.ParentNode.Name != "span" || node.ParentNode.GetAttributeValue("class", "") != "comicm") && node.ParentNode.Name != "li")).ToList(); foreach (HtmlNode node in nodes) { Word word = new Word(); int id; if (Int32.TryParse(node.GetAttributeValue("href", "").Substring(node.GetAttributeValue("href", "").LastIndexOf("kelimesec=") + 10), out id)) word.Id = id; else word.Id = null; word.Name = node.InnerText.Replace(" ", "").Trim(); word.Origin = String.Empty; word.Description = String.Empty; word.DictionaryName = String.Empty; word.Year = null; words.Add(word); } } } } if (words.Count > 0) isSuggestion = true; } } else { if (doc.DocumentNode .Descendants() .Where(node => (node.Name == "span" && (node.GetAttributeValue("class", "") == "comick" || node.GetAttributeValue("class", "") == "comicb" || node.GetAttributeValue("class", "") == "comics"))).Count() > 0) { // Words Found isSuggestion = false; List<HtmlNode> nodes = doc.DocumentNode .Descendants() .Where(node => (node.Name == "span" && (node.GetAttributeValue("class", "") == "comick" || node.GetAttributeValue("class", "") == "comicb" || node.GetAttributeValue("class", "") == "comics")) || (node.Name == "p" && node.GetAttributeValue("class", "") == "thomicb")).ToList(); for (int i = 0; i < nodes.Count; i = i + 5) { Word word = new Word(); word.Id = null; word.Name = nodes[i + 0].InnerText; word.Origin = nodes[i + 1].InnerText; word.Description = nodes[i + 2].InnerHtml; word.DictionaryName = nodes[i + 3].InnerText; int year; if (Int32.TryParse(nodes[i + 4].InnerText, out year)) word.Year = year; else word.Year = null; words.Add(word); } } else { // Check for suggestions List<HtmlNode> nodes = doc.DocumentNode .Descendants() .Where(node => (node.Name == "a" && node.ParentNode.Name == "table" && node.GetAttributeValue("href", "").StartsWith("/index.php?option=com_bts"))).ToList(); foreach (HtmlNode node in nodes) { Word word = new Word(); word.Name = node.InnerText; word.Origin = String.Empty; word.Description = String.Empty; word.DictionaryName = String.Empty; word.Year = null; words.Add(word); } if (words.Count > 0) isSuggestion = true; } } } } onLoadingEnds(); return new SearchResult(words, isSuggestion); }
private async void ListWords(Nullable<int> id, String name) { if (String.IsNullOrWhiteSpace(name) || MatchType == null) return; ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (internetConnectionProfile == null || internetConnectionProfile.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.InternetAccess) { ResourceLoader resourceLoader = new ResourceLoader(); await AlertService.ShowAlertAsync(resourceLoader.GetString("ErrorHeader"), resourceLoader.GetString("InternetNotAvailableErrorMessage")); return; } if (Words != null) Words.Clear(); IsNoResultFound = false; IsSuggestion = false; BigTurkishDictionaryFilter filter = new BigTurkishDictionaryFilter(); filter.SearchString = name; filter.SearchId = id; if (MatchType.Key.Equals("FullMatch")) { filter.MatchType = BigTurkishDictionaryFilter.MatchTypeFilter.FULL_MATCH; IsPartialMatch = false; } else { filter.MatchType = BigTurkishDictionaryFilter.MatchTypeFilter.PARTIAL_MATCH; IsPartialMatch = true; } try { SearchResult result = await _dataService.SearchBigTurkishDictionary(filter, () => { IsResultsLoading = true; }, () => { IsResultsLoading = false; } ); IsSuggestion = result.IsSuggestion; Words = new ObservableCollection<Word>(result.Words); if (Words.Count == 0) IsNoResultFound = true; } catch (Exception e) { if (e.Message == HttpStatusCode.InternalServerError.ToString()) { IsNoResultFound = false; IsResultsLoading = false; ResourceLoader resourceLoader = new ResourceLoader(); AlertService.ShowAlertAsync(resourceLoader.GetString("ErrorHeader"), resourceLoader.GetString("ServerErrorMessage")); } else { throw; } } }