Beispiel #1
0
        public async Task <GiphyGif> GetGifAsync(string query)
        {
            if (apiKey is null)
            {
                return(null);
            }

            if (query is null)
            {
                return(null);
            }

            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var rawresp = await HttpWebClient.ReturnStringAsync(new Uri($"https://api.giphy.com/v1/gifs/search?q={query.Replace(" ", "%20")}&api_key={apiKey}")).ConfigureAwait(false);

            var jsonresp = JObject.Parse(rawresp);
            var photo    = (JArray)jsonresp["data"];

            if (photo is not null)
            {
                dynamic item = photo.CryptoRandom();

                return(new GiphyGif
                {
                    ID = item["id"].ToString()
                });
            }

            return(null);
        }
Beispiel #2
0
        public async Task <SubReddit> GetSubRedditAsync(string subRedditName, int amountOfPosts)
        {
            try
            {
                if (rateLimiter.IsRatelimited())
                {
                    return(null);
                }

                var uri      = new Uri("https://www.reddit.com/" + subRedditName + "/.json?limit=" + amountOfPosts);
                var response = await HttpWebClient.ReturnStringAsync(uri);

                if (!string.IsNullOrEmpty(response) || !string.IsNullOrWhiteSpace(response))
                {
                    return(JsonConvert.DeserializeObject <SubReddit>(response));
                }
                else
                {
                    throw new Exception("Empty response from " + uri);
                }
            }
            catch
            {
                return(null);
            }
        }
Beispiel #3
0
        public async Task <Define> GetWordAsync(string word)
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var stringifiedxml = await HttpWebClient.ReturnStringAsync(new Uri($"http://www.stands4.com/services/v2/defs.php?uid={stands4userid}&tokenid={stands4usertoken}&word={word}"));

            var xml = new XmlDocument();

            xml.LoadXml(stringifiedxml);
            XObject xNode   = XDocument.Parse(xml.InnerXml);
            var     jobject = JObject.Parse(JsonConvert.SerializeXNode(xNode));
            dynamic item    = jobject["results"]["result"].First();

            return(new Define
            {
                Word = word,
                Definition = item["definition"].ToString(),
                Example = item["example"].ToString(),
                PartOfSpeech = item["partofspeech"].ToString(),
                Terms = item["term"].ToString()
            });
        }
Beispiel #4
0
        public async Task <YNWTFResponce> AskYNWTF()
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            return(JsonConvert.DeserializeObject <YNWTFResponce>(await HttpWebClient.ReturnStringAsync(new Uri($"https://yesno.wtf/api")).ConfigureAwait(false)));
        }
Beispiel #5
0
        public async Task <IEnumerable <UrbanWord> > GetPhrasesAsync(string phrase)
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var raw = await HttpWebClient.ReturnStringAsync(new Uri(QueryEndPoint + phrase)).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <UrbanWordContainer>(raw).List);
        }
Beispiel #6
0
        public async Task <UrbanWord> GetRandomWordAsync()
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var raw = await HttpWebClient.ReturnStringAsync(RandomEndpoint).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <UrbanWord>(raw));
        }
Beispiel #7
0
        public async Task <WikipediaArticle> GetArticleAsync(string language, string name)
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var article = new WikipediaArticle
            {
                Url = $"https://{language}.wikipedia.org/wiki/{name}"
            };

            {
                var pageInfo = await HttpWebClient.ReturnStringAsync(new Uri($"https://{language}.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles={name}")).ConfigureAwait(false);

                var     jsonresp = JObject.Parse(pageInfo);
                dynamic item     = jsonresp["query"]["pages"].First.First;

                string desc = Convert.ToString(item["extract"]);

                article.Name = item["title"].ToString();

                if (desc.Length >= 1024)
                {
                    var split = desc.Split(". ").ToList();
                    article.Description = string.Join(". ", split.Take(4)) + ".";
                }
                else
                {
                    article.Description = desc;
                }
            }

            {
                var pageImage = await HttpWebClient.ReturnStringAsync(new Uri($"https://{language}.wikipedia.org/w/api.php?format=json&action=query&prop=pageimages&piprop=original&titles={name}")).ConfigureAwait(false);

                var     jsonresp = JObject.Parse(pageImage);
                dynamic item     = jsonresp["query"]["pages"].First.First["original"];

                if (item != null)
                {
                    article.Original = new WikipediaImage
                    {
                        Source = item["source"].ToString(),
                        Width  = Convert.ToInt32(item["width"]),
                        Height = Convert.ToInt32(item["height"])
                    };
                }
            }

            return(article);
        }
Beispiel #8
0
        public async Task <GiphyGif> GetGifAsync(string query)
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            query = query.Replace(" ", "%20");

            var rawresp = await HttpWebClient.ReturnStringAsync(new Uri($"https://api.giphy.com/v1/gifs/search?q={query}&api_key=dc6zaTOxFJmzC")).ConfigureAwait(false);

            var jsonresp = JObject.Parse(rawresp);
            var photo    = (JArray)jsonresp["data"];

            dynamic item = photo[SkuldRandom.Next(0, photo.Count)];

            return(new GiphyGif
            {
                ID = item["id"].ToString()
            });
        }
Beispiel #9
0
 public async Task <XKCDComic> GetComicAsync(int comicid)
 {
     if (!XKCDLastPage.HasValue)
     {
         XKCDLastPage = await GetLastPageAsync().ConfigureAwait(false);
     }
     if (!rateLimiter.IsRatelimited())
     {
         if (comicid < XKCDLastPage.Value && comicid > 0)
         {
             return(JsonConvert.DeserializeObject <XKCDComic>(await HttpWebClient.ReturnStringAsync(new Uri($"https://xkcd.com/{comicid}/info.0.json")).ConfigureAwait(false)));
         }
         else
         {
             return(JsonConvert.DeserializeObject <XKCDComic>(await HttpWebClient.ReturnStringAsync(new Uri($"https://xkcd.com/{XKCDLastPage.Value}/info.0.json")).ConfigureAwait(false)));
         }
     }
     else
     {
         return(null);
     }
 }
Beispiel #10
0
        private static async Task <int?> GetLastPageAsync()
        {
            var rawresp = await HttpWebClient.ReturnStringAsync(new Uri("https://xkcd.com/info.0.json")).ConfigureAwait(false);

            if (rawresp.IsNullOrWhiteSpace())
            {
                return(null);
            }

            var     jsonresp = JObject.Parse(rawresp);
            dynamic item     = jsonresp;

            if (item["num"].ToString() is not null)
            {
                int num = Convert.ToInt32(item["num"].ToString());
                return(num);
            }
            else
            {
                return(null);
            }
        }
Beispiel #11
0
        public async Task <WikipediaArticle> GetArticleAsync(string language, string name)
        {
            if (rateLimiter.IsRatelimited())
            {
                return(null);
            }

            var article = new WikipediaArticle
            {
                Url = $"https://{language}.wikipedia.org/wiki/{name}"
            };

            var page = new WikiPage(wikipediaSite, name);

            await page.RefreshAsync(new WikiPageQueryProvider
            {
                Properties =
                {
                    new ExtractsPropertyProvider
                    {
                        MaxCharacters    = 1024,
                        AsPlainText      = true,
                        IntroductionOnly = true
                    }
                }
            });

            var extractGroup = page.GetPropertyGroup <ExtractsPropertyGroup>();

            article.Name = page.Title;
            article.Url  = WikiLink.Parse(wikipediaSite, name).TargetUrl;

            article.Description = extractGroup.Extract;

            if (article.Description.Length >= 1024)
            {
                var split = article.Description.Split(". ").ToList();
                article.Description = string.Join(". ", split.Take(4)) + ".";
            }

            var response = await HttpWebClient.ReturnStringAsync(new System.Uri($"https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&sites={language}wiki&props=claims&titles={name}"));

            var jsonresp  = JObject.Parse(response);
            var container = (JObject)jsonresp["entities"].First.Value <JProperty>().Value;
            var claims    = container["claims"];

            //P18/P154/P242/P109/P1621
            JToken snak = null;

            if (claims["P18"] is not null)
            {
                snak = claims["P18"];
            }
            else if (claims["P154"] is not null)
            {
                snak = claims["P154"];
            }
            else if (claims["P242"] is not null)
            {
                snak = claims["P242"];
            }
            else if (claims["P109"] is not null)
            {
                snak = claims["P109"];
            }
            else if (claims["P1621"] is not null)
            {
                snak = claims["P1621"];
            }

            if (snak is not null)
            {
                var val = snak.First["mainsnak"]["datavalue"]["value"].ToObject <string>();

                val = val.Replace(" ", "_");

                var md5 = val.CreateMD5(true);;

                article.ImageUrl = $"https://upload.wikimedia.org/wikipedia/commons/{md5[0]}/{md5[0]}{md5[1]}/{val}";
            }

            return(article);
        }