Exemple #1
0
        public RemoteTask <PopupContent> FetchHtml(RemoteCancellationToken ct, string url)
        {
            try
            {
                if (url.IsNullOrEmpty() || ct.IsNull())
                {
                    return(null);
                }

                Match Help = new Regex(UrlUtils.HelpGlossaryRegex).Match(url);

                if (!Help.Success)
                {
                    return(null);
                }

                string term = Help.Groups[1].Value;
                if (term.IsNullOrEmpty())
                {
                    return(null);
                }

                return(GetHelpGlossaryItem(ct, url, term));
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                LogTo.Error($"Failed to FetchHtml for url {url} with exception {ex}");
                throw;
            }

            return(null);
        }
Exemple #2
0
        public RemoteTask <List <OxfordDictionary> > GetAvailableDictionaries(RemoteCancellationToken ct)
        {
            OxfordDictClient.SetAuthentication(Config.AppId,
                                               Config.AppKey);

            return(OxfordDictClient.GetAvailableDictionaries(ct.Token()));
        }
        public async Task <EntryResult> LookupWordEntryAsync(RemoteCancellationToken ct,
                                                             string word,
                                                             IDictionaryService dict)
        {
            var lemmas = await dict.LookupLemma(
                ct,
                word);

            if (lemmas?.Results == null ||
                lemmas.Results.Any() == false ||
                lemmas.Results[0].LexicalEntries.Any() == false ||
                lemmas.Results[0].LexicalEntries[0].InflectionOf.Any() == false)
            {
                return(null);
            }

            word = lemmas.Results[0].LexicalEntries[0].InflectionOf[0].Text;

            if (string.IsNullOrWhiteSpace(word))
            {
                return(null);
            }

            return(await dict.LookupEntry(
                       ct,
                       word));
        }
Exemple #4
0
        private async Task <PopupContent> GetWikipediaExtractAsync(RemoteCancellationToken ct, string title, string language)
        {
            string url      = string.Format(ArticleExtractUrl, language, title);
            string response = await GetAsync(ct.Token(), url);

            var extract = response?.Deserialize <WikiExtract>();

            return(CreatePopupHtml(extract));
        }
Exemple #5
0
        public RemoteTask <LemmatronResult> LookupLemma(RemoteCancellationToken ct,
                                                        string word,
                                                        string language = "en-gb")
        {
            if (DictionaryConst.AllMonolingualLanguages.Contains(language) == false)
            {
                LogTo.Warning("Invalid language requested: {Language}", language);
                // ReSharper disable once LocalizableElement
                throw new ArgumentException($"Invalid language requested: {language}", nameof(language));
            }

            OxfordDictClient.SetAuthentication(Config.AppId,
                                               Config.AppKey);

            return(OxfordDictClient.LookupLemma(ct.Token(),
                                                word,
                                                language));
        }
        public RemoteTask <PopupContent> FetchHtml(RemoteCancellationToken ct, string href)
        {
            try
            {
                if (href.IsNullOrEmpty() || !new Regex(DictRegex).Match(href).Success)
                {
                    return(null);
                }

                return(GetDictionaryEntry(ct.Token(), href));
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                LogTo.Error($"Failed to FetchHtml for href {href} with exception {ex}");
                throw;
            }

            return(null);
        }
Exemple #7
0
        public RemoteTask <PopupContent> FetchHtml(RemoteCancellationToken ct, string url)
        {
            try
            {
                if (url.IsNullOrEmpty())
                {
                    return(null);
                }

                var regexArr = new string[] { UrlUtils.GuruRegex, UrlUtils.HelpRegex, UrlUtils.MemopediaRegex };
                if (!regexArr.Any(x => new Regex(x).Match(url).Success))
                {
                    return(null);
                }

                return(GetArticleExtract(ct, url));
            }
            catch (Exception ex)
            {
                LogTo.Error($"Failed to FetchHtml for url {url} with exception {ex}");
                throw;
            }
        }
Exemple #8
0
        public RemoteTask <PopupContent> FetchHtml(RemoteCancellationToken ct, string url)
        {
            try
            {
                if (!url.IsDesktopWikipediaUrl())
                {
                    return(null);
                }

                string title    = url.ParseArticleTitle();
                string language = url.ParseArticleLanguage();
                return(string.IsNullOrEmpty(title)
          ? null
          : GetWikipediaExtractAsync(ct, title, language));
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                LogTo.Error($"Failed to FetchHtml for url {url} with exception {ex}");
                throw;
            }

            return(null);
        }
Exemple #9
0
        private async Task <PopupContent> GetHelpGlossaryItem(RemoteCancellationToken ct, string url, string term)
        {
            string response = await GetAsync(ct.Token(), url);

            return(CreateHelpGlossaryContent(response, url, term));
        }
Exemple #10
0
        private async Task <PopupContent> GetArticleExtract(RemoteCancellationToken ct, string url)
        {
            string response = await GetAsync(ct.Token(), url).ConfigureAwait(false);

            return(CreatePopupContent(response, url));
        }
Exemple #11
0
        private async Task SearchForCards(List <string> words, RemoteCancellationToken ct)
        {
            if (words.IsNull() || !words.Any())
            {
                return;
            }

            var results = new Dictionary <string, int>();

            try
            {
                foreach (var word in words)
                {
                    if (word.IsNullOrEmpty() || ct.IsNull())
                    {
                        return;
                    }

                    if (CardIndex.IsNull())
                    {
                        return;
                    }

                    await foreach (Result res in CardIndex.Search(word, ct.Token()))
                    {
                        string id = res.DocumentReference;
                        if (results.ContainsKey(id))
                        {
                            results[id]++;
                        }
                        else
                        {
                            results[id] = 1;
                        }
                    }
                }
            }
            catch (TaskCanceledException) { }

            if (results.IsNull() || !results.Any())
            {
                return;
            }

            var ordered = results
                          .OrderByDescending(x => x.Value)
                          .Take(Config.MaxSearchResults);

            var cards = new List <Card>();

            foreach (var pair in ordered)
            {
                cards.Add(Cards[pair.Key]);
            }

            if (CurrentWdw.IsNull() || CurrentWdw.IsClosed)
            {
                OpenCardWdw(cards);
            }
            else
            {
                UpdateCardWdw(cards);
            }
        }