private static DictCCSearchResult[] ExtractDictCCSearchResults(string html, string originLanguageCode, string destinationLanguageCode) { Match match = Regex.Match(html, @"var idArr = new Array\((?:(\d+),?)*\);"); if (!match.Success) { return(Array.Empty <DictCCSearchResult>()); } int[] ids = match.Groups[1].Captures.Cast <Capture>().Select(capture => Convert.ToInt32(capture.Value)).ToArray(); match = Regex.Match(html, @"var c1Arr = new Array\((?:(""([^""]*)""),?)*\);"); if (!match.Success) { return(Array.Empty <DictCCSearchResult>()); } string[] words1 = match.Groups[2].Captures.Cast <Capture>().Select(capture => capture.Value).ToArray(); match = Regex.Match(html, @"var c2Arr = new Array\((?:(""([^""]*)""),?)*\);"); if (!match.Success) { return(Array.Empty <DictCCSearchResult>()); } string[] words2 = match.Groups[2].Captures.Cast <Capture>().Select(capture => capture.Value).ToArray(); if (ids.Length != words1.Length || ids.Length != words2.Length) { return(Array.Empty <DictCCSearchResult>()); } DictCCSearchResult[] results = new DictCCSearchResult[ids.Length]; bool word2 = originLanguageCode == "DE" || (originLanguageCode == "EN" && destinationLanguageCode != "DE"); for (int i = 0; i < ids.Length; i++) { results[i] = new DictCCSearchResult { ID = ids[i], Word1 = word2 ? words2[i] : words1[i], Word2 = word2 ? words1[i] : words2[i] } } ; return(results); }
public static async Task <Uri> GetAudioRecordingUri(DictionaryEntryViewModel dictionaryEntryViewModel, bool word2) { DirectionViewModel selectedDirection = dictionaryEntryViewModel.SearchContext.SelectedDirection; string word = selectedDirection.ReverseSearch ^ word2 ? dictionaryEntryViewModel.DictionaryEntry.Word2 : dictionaryEntryViewModel.DictionaryEntry.Word1; string otherWord = !selectedDirection.ReverseSearch ^ word2 ? dictionaryEntryViewModel.DictionaryEntry.Word2 : dictionaryEntryViewModel.DictionaryEntry.Word1; string originLanguageCode = word2 ? selectedDirection.DestinationLanguageCode : selectedDirection.OriginLanguageCode; string destinationLanguageCode = word2 ? selectedDirection.OriginLanguageCode : selectedDirection.DestinationLanguageCode; if (urlCache.TryGetValue(new LanguageWordPair(originLanguageCode, word), out Uri audioUri)) { return(audioUri); } Uri requestUri = GetSearchPageUri(originLanguageCode, destinationLanguageCode, word); string response = await httpClient.GetStringAsync(requestUri); DictCCSearchResult[] dictCCSearchResults = ExtractDictCCSearchResults(response, originLanguageCode, destinationLanguageCode); string wordJSLiteral = GetJSLiteralOfWord(word); string otherWordJSLiteral = GetJSLiteralOfWord(otherWord); DictCCSearchResult dictCCSearchResult = null; if (wordJSLiteral != string.Empty || otherWordJSLiteral != string.Empty) { dictCCSearchResult = dictCCSearchResults.FirstOrDefault(result => result.Word1 == wordJSLiteral && result.Word2 == otherWordJSLiteral); } if (dictCCSearchResult != null) { audioUri = GetAudioRecordingUri(dictCCSearchResult.ID, originLanguageCode, destinationLanguageCode, originLanguageCode); } else { audioUri = null; } urlCache.Add(new LanguageWordPair(originLanguageCode, word), audioUri); return(audioUri); }