public IActionResult Subreddit() { //string output = RD.GetAPIString("aww"); //ViewBag.test = output; RedditPost rp = RD.GetPost(); return View(rp); }
public IActionResult RedditPostData() { RedditDAL dl = new RedditDAL(); RedditPost p = dl.GetRedditPost(); return(View(p)); }
public ActionResult Reddit() { HttpWebRequest request = WebRequest.CreateHttp("https://www.reddit.com/r/nba/.json"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader rd = new StreamReader(response.GetResponseStream()); string data = rd.ReadToEnd(); JObject redditJson = JObject.Parse(data); List <JToken> posts = redditJson["data"]["children"].ToList(); List <RedditPost> output = new List <RedditPost>(); for (int i = 0; i < posts.Count; i++) { RedditPost rp = new RedditPost(); rp.Title = posts[i]["data"]["title"].ToString(); rp.ImageURL = posts[i]["data"]["thumbnail"].ToString(); rp.LinkURL = "http://reddit.com/" + posts[i]["data"]["permalink"].ToString(); output.Add(rp); } return(View(output)); }
public ActionResult Index() { string text = RedditDAL.GetData(); RedditPost rp = new RedditPost(text, 0); return(View(rp)); }
public static void GetPostsAsync() { foreach (var child in Api.Data["data"]["children"]) { var data = child["data"]; var post = new RedditPost { Author = (string)data["author"], Domain = (string)data["domain"], ID = (string)data["id"], Link = (string)$"{Api.DOMAIN}{data["permalink"]}", Name = (string)data["name"], NumComments = (int)data["num_comments"], Score = (int)data["score"], Thumbnail = (string)data["thumbnail"], Title = (string)data["title"], Url = (string)data["url"], Utc = (string)data["utc"], }; if (post.Author != "PhotoShopBattles") { Api.Posts.Add(post); } } }
public void DownvotePost(RedditPost oldpost) { var newpost = RedditPostContext.RedditPosts.FirstOrDefault(p => p.Id == oldpost.Id); newpost.DownVote++; RedditPostContext.SaveChanges(); }
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); }
public void LoadDBRecord() { var redditPost = ETHBotDBContext.RedditPosts.SingleOrDefault(i => i.PostId == Post.Fullname); if (redditPost == null) { ETHBotDBContext.RedditPosts.Add(new RedditPost() { PostId = Post.Fullname, PostTitle = Post.Title, PostedAt = Post.Created, Author = Post.Author, UpvoteCount = Post.UpVotes, DownvoteCount = Post.DownVotes, IsNSFW = Post.NSFW, Permalink = Post.Permalink, SubredditInfo = SubredditInfo, Url = Post.Listing.URL.Substring(0, Math.Min(500, Post.Listing.URL.Length)), IsText = Post.Listing.IsSelf, Content = Post.Listing.IsSelf ? Post.Listing.SelfText : null });; ETHBotDBContext.SaveChanges(); // TODO Check if the primary key is updated on save changes in the object redditPost = ETHBotDBContext.RedditPosts.SingleOrDefault(i => i.PostId == Post.Fullname); } else { PostExisted = true; } DBPost = redditPost; }
public ActionResult DeleteConfirmed(int id) { RedditPost post = db.Posts.Find(id); db.Posts.Remove(post); db.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public static void fetch(string callType, RedditPost post, string url) { var postLink = $"{(string)url}.json"; string req = Request(postLink); var arr = JsonConvert.DeserializeObject <JArray>(req); Api.CommentData = arr; GetCommentsAsync(post); }
private async Task ParseResult(RedditPost post, BlockingCollection <RedditPost> outputCollection) { post.Url = post.Url.Trim('/'); if (post.Domain.Contains(".deviantart.com")) //The domain-field for DeviantArt contains the artists username as well, so we cant switch on it. { post.Image = await _deviantartImages.GetContent(post.Url); } else { switch (post.Domain) { case "i.imgur.com": case "m.imgur.com": case "imgur.com": if (post.Url.Contains("imgur.com/a/") || post.Url.Contains("imgur.com/gallery/")) { var albumId = post.Url.Split('/').Last(); var album = await _imgurAlbums.GetContent(albumId); post.Album = album; } else { var imageId = post.Url.Split('/').Last(); if (imageId.Contains(".")) //The image extension is not a part of the imageId. { imageId = imageId.Split('.').First(); } var image = await _imgurImages.GetContent(imageId); post.Image = image; } break; case "i.redd.it": //Images from i.redd.it can be handled just like generic images since it doesn't have an api that we need to call. Case is redundant, but signals that this domain is handled. default: var ending = $".{post.Url.Split('.').Last()}"; //Extensions in the settings-file include the period, so we re-add it after splitting on it. if (Settings.GetSupportedExtensions().Contains(ending.ToLower())) { post.Image = new GenericImage { Url = post.Url }; } break; } } //Exclude all posts that link to non-supported domains, or where we couldn't create an image/album-entry. if (post.Image != null || post.Album != null) { outputCollection.Add(post); } }
public ActionResult Edit([Bind(Include = "Id,Title,Body,UpVotes,DownVotes,DatePosted,UserId")] RedditPost post) { if (ModelState.IsValid) { db.Entry(post).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View(post)); }
public ActionResult Create([Bind(Include = "Id,Title,Body,UpVotes,DownVotes,DatePosted,Img")] RedditPost redditPost) { if (ModelState.IsValid) { redditPost.UserId = HttpContext.User.Identity.GetUserId(); db.Posts.Add(redditPost); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(redditPost)); }
static public async Task RunIt() { try { string oauthUrl = "https://oauth.reddit.com/r/"; HttpClient hc = new HttpClient(); hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.AccessToken); hc.DefaultRequestHeaders.Add("User-Agent", "UserAgent1"); // All set at this point. Below is something I whipped up as an example; not exactly the best looking code... string subreddits = ConfigurationManager.AppSettings["subreddits"].ToString(); foreach (string subreddit in subreddits.Split(',')) { string fullUrl = oauthUrl + subreddit; var response = await hc.GetAsync(fullUrl); var subredditText = await response.Content.ReadAsStringAsync(); var json = JsonConvert.DeserializeObject <RootObject>(subredditText); foreach (var article in json.data.children) { RedditPost ra = new RedditPost { PostId = article.data.id, Title = article.data.title, Subreddit = article.data.subreddit, NumberOfComments = article.data.num_comments, NumberOfUpvotes = article.data.ups, IsGilded = Utility.NumberOfGildings(article.data.gildings), PostDate = Utility.GetDateTimeFromRedditUnixTimestamp(article.data.created_utc), Url = article.data.url }; if (!dal.PostAlreadyExists(ra.PostId)) { dal.InsertNewPostToDatabase(ra); } else { dal.UpdateExistingPosts(ra); } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public IActionResult AddPost(RedditPost post) { if (ModelState.IsValid) { RedditPostRepository.AddPost(post); return(RedirectToAction("Read")); } else { return(View("Add")); } }
private Event TransformPostToEvent(RedditPost post) { var data = post.Data; var parser = new RedditHtmlParser(data.Selftext_Html); return new Event { CreatedOn = TimeStampToDateTime(data.Created), EventId = data.Id, ImageUrl = parser.ImageUrl, Status = TransformCssClassToStatus(data.Link_Flair_Css_Class, parser), Title = data.Title }; }
public bool ReadPost(RedditPost redditPost) { try { this.iRepository.UpdateRedditPost(redditPost); this.iRepository.Save(); return(true); } catch (Exception) { return(false); } }
public bool DismissPost(RedditPost redditPost) { try { this.iRepository.DeleteRedditPost(redditPost.Id); this.iRepository.Save(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Parse reddit posts from given url. i.e. http://www.reddit.com/r/worldnews.json /// Throws AggregateException as this is task. Should contain WebException with info. /// </summary> /// <param name="url"></param> /// <returns></returns> public static async Task <RedditPostPage> ParsePosts(string url) { RedditPostPage rpp = new RedditPostPage(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Accept = "application/json"; req.Timeout = 3000; try { // get response and handle result using (HttpWebResponse res = (HttpWebResponse)await req.GetResponseAsync()) { // might be unnecessary check if (res.StatusCode == HttpStatusCode.OK) { // stream -> json readers using (StreamReader sr = new StreamReader(res.GetResponseStream())) using (JsonTextReader jtr = new JsonTextReader(sr)) { // load json try { JObject all = JObject.Load(jtr); rpp.Before = all["data"]["before"].ToObject <string>(); // token to previous page rpp.After = all["data"]["after"].ToObject <string>(); // token to next page // post array JArray posts = (JArray)all["data"]["children"]; // parse posts, Todo: could we somehow avoid loop? foreach (JToken post in posts) { RedditPost p = post["data"].ToObject <RedditPost>(); rpp.Posts.Add(p); } } catch (JsonReaderException jre) { Debug.WriteLine(jre.Message); throw new WebException("ParsePosts, invalid data in response from " + url); } } } } } catch (WebException) { // Todo: could remove try as we don't handle exception... throw; } return(rpp); }
// GET: Posts/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } RedditPost post = db.Posts.Find(id); if (post == null) { return(HttpNotFound()); } return(View(post)); }
// GET: RedditPosts/Details/5 public ActionResult Details(string UserId) { if (UserId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } RedditPost redditPost = db.Posts.Find(UserId); if (redditPost == null) { return(HttpNotFound()); } return(View(redditPost)); }
public ActionResult Index() { //HttpWebRequest request = WebRequest.CreateHttp("https://www.reddit.com/r/nba/.json"); //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //StreamReader rd = new StreamReader(response.GetResponseStream()); //string data = rd.ReadToEnd(); //JObject redditJson = JObject.Parse(data); //RedditPost p = new RedditPost(data, 0); RedditPost rp = RedditDAL.GetPost(0); return(View(rp)); }
public async Task <IActionResult> CreateComment(RedditComment comment, string ReturnURL) { RedditPost post = await _context.Posts.FindAsync(comment.postID); post.num_comments += 1; Int64 unixTimestamp = (Int64)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; comment.unixTimestamp = unixTimestamp; await _context.AddAsync(comment); await _context.SaveChangesAsync(); return(LocalRedirect("/reddit/comments/" + comment.postID)); }
private List <RedditPost> JsonToObject(string jsonString) { JObject json = JObject.Parse(jsonString)["data"].ToObject <JObject>(); JArray arr = json["children"].ToObject <JArray>(); List <RedditPost> l = new List <RedditPost>(); for (int i = 0; i < arr.Count; i++) { var jsonString2 = arr[i]["data"].ToString(); RedditPost redditPost = JsonConvert.DeserializeObject <RedditPost>(jsonString2); redditPost.created_string = SecondsToAgoString((Int64)redditPost.created_utc); l.Add(redditPost); } return(l); }
public ActionResult Create([Bind(Include = "Id,Title,Img,Body,UpVotes,DownVotes,DatePosted,UserId")] RedditPost post) { if (ModelState.IsValid) { db.Posts.Add(post); post.UpVotes = 1; post.DownVotes = 0; post.PostedAt = DateTime.Now; post.UserId = User.Identity.GetUserId(); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View(post)); }
public async Task <IActionResult> CreatePost([FromForm] RedditPost post) { System.Console.WriteLine("^^^^^^^^^^>>"); System.Console.WriteLine(post.url); Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; post.created_utc = unixTimestamp; post.id = unixTimestamp.ToString(); post.name = "t3_" + post.id; post.num_comments = 0; post.score = "0"; await _context.AddAsync(post); await _context.SaveChangesAsync(); return(Json(new { success = true })); }
public static void LoadTitles(ListBox myListBox, RedditRootobject response) { if (response != null) { //initialize global variable Global.RedditForum = new RedditForum(); Global.RedditForum.redditPosts = new List <RedditPost>(); foreach (var children in response.data.children) { //global variable RedditPost newRedditPost = new RedditPost(); //id newRedditPost.Id = children.data.id; //title if (children.data.title != null) { newRedditPost.Title = children.data.title; } //preview if (children.data.preview != null) { if (children.data.preview.images[0] != null) { newRedditPost.PreviewPictureLink = children.data.preview.images[0].source.url; } } //global variable Global.RedditForum.redditPosts.Add(newRedditPost); } //load combo myListBox.DataSource = Global.RedditForum.redditPosts; myListBox.ValueMember = "id"; myListBox.DisplayMember = "title"; } else { throw new System.InvalidOperationException("JSON response is NULL"); } }
public void ObjectEqualityOnDatabaseObjectsWorksAsExpected() { using (DatabaseCache <RedditComment> cache = new DatabaseCache <RedditComment>(5)) { for (int i = 0; i < 5; ++i) { RedditComment comment = new RedditComment() { Body = $"info: {i}" }; Assert.IsFalse(cache.Contains(comment)); cache.Add(comment); Assert.IsTrue(cache.Contains(comment)); } Assert.IsTrue(cache.Contains(new RedditComment() { Body = $"info: 0" })); } using (DatabaseCache <RedditPost> postCache = new DatabaseCache <RedditPost>(15)) { for (int i = 0; i < 20; ++i) { RedditPost post = new RedditPost() { Title = $"info: {i}" }; Assert.IsFalse(postCache.Contains(post)); postCache.Add(post); Assert.IsTrue(postCache.Contains(post)); } Assert.IsTrue(postCache.Contains(new RedditPost() { Title = $"info: 10" })); Assert.IsFalse(postCache.Contains(new RedditPost() { Title = $"info: 0" })); } }
public static void GetCommentsAsync(RedditPost post) { int index = Api.Posts.IndexOf(post); RedditPost Post = Api.Posts[index]; foreach (var child in Api.CommentData[1]["data"]["children"]) { var data = child["data"]; bool IsComment = (string)child["kind"] != "more"; if (IsComment) { try { bool IsNotDeleted = (string)data["author"] != "[deleted]"; bool IsNotControversial = (int)data["score"] > 0; if (IsNotDeleted && IsNotControversial) { var comment = new RedditComment { Author = (string)data["author"], Score = (int)data["score"], Utc = (string)data["created_utc"], Body = (string)data["body"], Link = (string)$"{Api.DOMAIN}{data["permalink"]}", Replies = data["replies"] }; Api.Posts[index].Comments.Add(comment); Console.WriteLine($"Title: {comment.Title}"); Console.WriteLine($"Is image? {comment.HasImage}"); } } catch (System.ArgumentNullException) { // If Comment Cannot be parsed, catch error return; } } } }
public async Task <IActionResult> Comments(string id) { RedditPost post = await _context.Posts.FindAsync(id); post.created_string = SecondsToAgoString((Int64)post.created_utc); List <RedditComment> RList = _context.Comments .Where(a => a.postID == id) .ToList(); RList.Reverse(); string current_id = post.id; CommentTree commentTree = new CommentTree(); commentTree.children = new List <CommentTree>(); commentTree.comment = null; List <RedditComment> TopLevelComments = RList.Where(a => a.parentID.ToString() == current_id).ToList(); foreach (var TopLevelComment in TopLevelComments) { CommentTree child_comment_tree = GetCommentTree(RList, TopLevelComment, true); commentTree.children.Add(child_comment_tree); } foreach (var item in RList) { item.createdString = SecondsToAgoString(item.unixTimestamp); } string subreddit = post.subreddit; CommentsPage commentsPage = new CommentsPage() { post = post, commentTree = commentTree }; return(View(commentsPage)); }
/// <summary> /// Create funny post from reddit post /// </summary> /// <param name="rp"></param> /// <returns>Post or null in case of error</returns> internal static async Task <Post> createFunnyPost(RedditPost rp) { // funny post Post fp = new Post { Title = rp.Title, RedditId = rp.Id }; // timestamp fp.Created = UNIX_START.AddSeconds(rp.Created_Utc); // get direct link to picture fp.ImageUrl = await createPictureLink(rp.Url); if (fp.ImageUrl == null) { // no picture => no post return(null); } // fetch comments try { List <RedditComment> comments = await RedditScraper.ParseComments(fp.RedditId); // take 3 first comments to be added into db foreach (RedditComment comment in comments.Take(3)) { fp.Comments.Add(new Comment { Text = comment.Body }); } } catch (WebException we) { // comments are not crucial, so we just forget them in case of error Debug.WriteLine(we.Message); } return(fp); }
public RedditShredder(string url) { XmlDocument doc = new XmlDocument(); doc.Load(url); XmlDocument docc = new XmlDocument(); string deoc = doc.InnerXml.Remove(0,38); docc.LoadXml(deoc); XmlNodeList nodes = docc.FirstChild.ChildNodes; foreach (XmlNode node in nodes) { string builder = ""; if (!node.Name.Equals("entry")) continue; foreach (var temp in node.InnerXml.Split(new string[] { "xmlns=\"http://www.w3.org/2005/Atom\"" }, StringSplitOptions.RemoveEmptyEntries)) builder += temp; XmlDocument reader = new XmlDocument(); RedditPost post = new RedditPost(); reader.LoadXml("<root>" + builder + "</root>"); post.PostTitle = reader.SelectSingleNode("//title").InnerText; post.PostUrl = reader.SelectSingleNode("//link/@href").InnerText; post.AuthorName = reader.SelectSingleNode("//author/name").InnerText; post.AuthorUrl = reader.SelectSingleNode("//author/uri").InnerText; post.PostContent = reader.SelectSingleNode("//content").InnerText; try { reader.LoadXml("<content>"+post.PostContent + "</content>"); } catch(Exception e) { } string innerxml = reader.InnerXml; string[] imageArray = innerxml.Split(new string[] { "<img" }, StringSplitOptions.None); if (imageArray.Length > 1) { string image = imageArray[1]; image = image.Split('>')[0]; image = "<img" + image + ">"; post.Image = image; } else { post.Image = ""; } var yearReg = "(20[0-9][0-9]|20[0-9][0-9])"; //< Allows a number between 2014 and 2029 var monthReg = "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; //< Allows a number between 00 and 12 var dayReg = "([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])"; //< Allows a number between 00 and 31 var hourReg = "([0-1][0-9]|2[0-4])"; //< Allows a number between 00 and 24 var minReg = "([0-5][0-9])"; //< Allows a number between 00 and 59 var reg = new Regex(yearReg+"-"+ dayReg + "-" + dayReg + "T" + hourReg + ':' + minReg + ':' + minReg); if (reader.SelectSingleNode("//updated") != null) { string tempss = reader.SelectSingleNode("//updated").InnerText; string date = reader.SelectSingleNode("//updated").InnerText; string output = reg.Match(date).ToString(); DateTime time = new DateTime(); try { time = DateTime.Parse(output); } catch { reg = new Regex(dayReg + "\\s" + monthReg + "\\s" + yearReg); output = reg.Match(output).ToString(); time = DateTime.Parse(output); } post.PostDate = time; } posts.Add(post); } }