Ejemplo n.º 1
0
        /// <summary>
        /// This will obtain the latest helprace post. It'll clean the HTML and truncate the body text down to 256 characters. Can be increased later.
        /// TODO: Add the truncate value to the DB so we can edit on the fly instead of hardcoded.
        /// </summary>
        /// <param name="argument">What kind of helprace post should we scrape?.</param>
        /// <returns>Helprace Entry.</returns>
        public async Task <HelpracePost> GetLatestAsync(string argument)
        {
            string rawHelprace;

            if (this.CheckArguments(argument) == true)
            {
                rawHelprace = await this.QueryHelprace(argument);
            }
            else
            {
                Log.Debug($"Failed to obtain Helprace Post, check returned false/null");
                return(null);
            }

            JObject jObject = JObject.Parse(rawHelprace);
            JToken  jPost   = jObject["topics"]["data"][0];

            HelpracePost post = new HelpracePost()
            {
                UID       = jPost["id"].ToString(),
                Channel   = jPost["channel"].ToString(),
                Title     = HttpUtility.HtmlDecode(jPost["title"].ToString()),
                Body      = Sanitize.RemoveBreaks(jPost["body"].ToString()), // Remove the page breaks and fluff that can come with helprace entries.
                Timestamp = DateTime.Parse(jPost["created"].ToString(), CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal),
                Author    = jPost["author"]["name"].ToString(),
            };

            post.Body = post.Body.Truncate(256);   // Truncate the body text so we don't run into an overflow error for the discord embed later.

            Log.Debug($"[HELPRACE]: {post.Timestamp.ToString(CultureInfo.InvariantCulture)}");
            return(post);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Obtain a defined amount of topics from helprace given the particular argument.
        /// </summary>
        /// <param name="pageNumber">What kind of data do you want back from the helprace?.</param>
        /// <returns>An array of helprace posts.</returns>
        public async Task GetAllTopics(int pageNumber = 1)
        {
            string rawHelprace = await this.httpClient.GetStringAsync(new Uri($"{HelpraceURL}/all_topics?sort_by=created&sort_order=desc&fields=id%2Ctitle%2Cbody%2Ccreated%2Cauthor%2Cvotes%2Cchannel&per_page=100&page={pageNumber}"));

            JObject jObject = JObject.Parse(rawHelprace);
            JArray  jPosts  = jObject["topics"]["data"] as JArray;

            for (int i = 0; i < jPosts.Count; i++)
            {
                HelpracePost post = new HelpracePost()
                {
                    Author    = jPosts[i]["author"]["name"].ToString(),
                    Body      = Sanitize.RemoveBreaks(jPosts[i]["body"].ToString().Truncate(256)),
                    Channel   = jPosts[i]["channel"].ToString(),
                    UID       = jPosts[i]["id"].ToString(),
                    Timestamp = DateTime.Parse(jPosts[i]["created"].ToString(), CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal),
                    Title     = HttpUtility.HtmlDecode(jPosts[i]["title"].ToString()),
                };

                await this.TryStoreNewAsync(post);
            }
        }