public RedditPosts GetAll()
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

            //instantiate model and the lists inside the model
            RedditPosts model = new RedditPosts();

            model.Posts = new List <RedditPost>();

            //assigning url to the string
            string url = "https://www.reddit.com/r/Nootropics/search?q=flair%3A%22Scientific+Study%22+OR+site%3Ancbi.nlm.nih.gov&restrict_sr=on&sort=new&t=all";

            //instantiating loading HtmlWeb from provided url
            var          htmlWeb  = new HtmlWeb();
            HtmlDocument document = null;

            document = htmlWeb.Load(url);

            //getting desired post titles and links from page
            var anchorTags = document.DocumentNode.Descendants("a")
                             .Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("search-title"));

            //looping through the variable and assigning the desired values to my model
            foreach (var node in anchorTags)
            {
                RedditPost item = new RedditPost();
                item.PostTitle = node.InnerText;
                item.PostUrl   = node.GetAttributeValue("href", null);
                //item is pushed to model list
                model.Posts.Add(item);
            }
            //returning model
            return(model);
        }
Пример #2
0
        public ActionResult Reddit(int name)
        {
            int            postnum = name;
            HttpWebRequest request = WebRequest.CreateHttp
                                         ("https://reddit.com/r/aww/.json");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            StreamReader rd   = new StreamReader(response.GetResponseStream());
            string       data = rd.ReadToEnd();

            //ViewBag.dat = data;

            JObject redditJson = JObject.Parse(data);

            //ViewBag.dat = redditJson["data"]["children"][0];
            List <JToken> posts = redditJson["data"]["children"].ToList();

            //JObject post = JObject.Parse(singlePost);

            List <RedditPosts> output = new List <RedditPosts>();

            //string title = singlePost["title"].ToString();


            for (int i = 0; i < postnum; i++)
            {
                RedditPosts rp = new RedditPosts();
                rp.Title    = posts[i]["data"]["title"].ToString();
                rp.ImageURL = posts[i]["data"]["thumbnail"].ToString();
                rp.LinkURL  = posts[i]["data"]["url"].ToString();
                output.Add(rp);
            }

            return(View(output));
        }
Пример #3
0
        public async Task GetData()
        {
            //ToDo add Akavache Caching
            var apiClient = new RedditApiClient();

            string lastPost = "";

            if (RedditPosts != null && RedditPosts.Count > 0)
            {
                lastPost = RedditPosts.Last().Name;
            }

            //Get reddit posts starting from last post name (name=id)
            var redditPostsDTOs = await apiClient.GetRedditPosts(MAX_LIST_VALUE, lastPost);

            foreach (var redditPostDTO in redditPostsDTOs)
            {
                //Add items to UI list until MAX value is reached. To fetch new items, dismiss posts from UI.
                if (RedditPosts.Count < MAX_LIST_VALUE)
                {
                    RedditPosts.Add(new RedditPostViewModel(redditPostDTO));
                }
            }
        }