private static string GrabPostContent(RedditSharp.Things.Post post) { string output = ""; if (post.Url.Host != "www.reddit.com") { output += $"<p>via <a href='{post.Url.ToString()}'>{post.Url.Host}</a></p>"; } if (post.Thumbnail.IsAbsoluteUri) { } var web = new HtmlWeb(); var doc = web.Load(post.Shortlink); var paragraph = doc.DocumentNode.SelectSingleNode("//div[@data-test-id=\"post-content\"]//div//p"); if (paragraph != null) { foreach (var node in paragraph.ParentNode.Descendants().Where(x => x.NodeType == HtmlNodeType.Element)) { node.Attributes.RemoveAll(); } RemoveComments(paragraph.ParentNode); output += paragraph.ParentNode.InnerHtml; } return(output); }
public async Task GetPostAsync(string subreddit) { //NOTE: NSFW Posts/subs are filtered out from this command. var sub = await MediaHelper.Reddit.GetSubredditAsync(subreddit); if (sub == null) { await ReplyAsync("A subreddit with that name could not be found."); return; } var posts = await sub.GetPosts(RedditSharp.Things.Subreddit.Sort.Hot, 25).OrderByDescending(x => Random.Next()).ToList(); RedditSharp.Things.Post selectedPost = null; //Filter out nsfw posts foreach (var post in posts) { if (post.NSFW) { continue; } selectedPost = post; } if (selectedPost == null) { await ReplyAsync("Unable to retrieve a post."); return; } await ReplyAsync($"{selectedPost.Title}\nhttps://reddit.com{selectedPost.Permalink}"); }
public Dto.Post Map(RedditSharp.Things.Post post) { return(new Dto.Post() { Id = post.Id, Author = post.Author.FullName, Text = post.SelfText, Title = post.Title, Url = post.Url, CreatedAt = post.CreatedUTC.UtcDateTime }); }
private bool PostOnReddit() { try { Tuple <string, string> PostTuple = GeneratePost(); RedditSharp.Things.Post Post = MainLogic.Subreddit.SubmitTextPost(PostTuple.Item1, PostTuple.Item2); PostURL = Post.Shortlink.Replace("http://", "https://"); return(true); } catch { return(false); } }
private static Task DownloadAndSaveTask(RedditSharp.Things.Post post, string path) { return(Task.Run(() => { string postURL = !string.IsNullOrEmpty(Path.GetExtension(post.Url.OriginalString).ToString()) ? Convert.ToString(post.Url) : Convert.ToString(post.Url + ".png"); using (WebClient client = new WebClient()) { var response = client.DownloadData(postURL); using (var stream = new MemoryStream(response)) using (Image image = Image.FromStream(stream)) using (Bitmap bitmap = new Bitmap(image)) { if (bitmap != null) { Guid id = Guid.NewGuid(); bitmap.Save(path + id.ToString() + ".png", ImageFormat.Png); } } } })); }
private static int _savePost(RedditSharp.Things.Post post) { if (IHasIt(post)) { return(1); //f**k it, cause I am too cool and I procedure } string __root = Tools.GetRoot(); OriginalPost op = new OriginalPost(); op.ContextURL = "http://www.reddit.com/" + post.Permalink.ToString(); op.Date = post.Created; op.Description = post.SelfText; op.NLikes = post.Upvotes - post.Downvotes; op.OPName = post.AuthorName; op.OPUserLink = "http://www.reddit.com/user/" + post.AuthorName; op.Site = "www.reddit.com"; op.Title = post.Title; op.FUllURL = post.Url.ToString(); op.IDOnSource = post.Id; op.Save(); IMGURDec _dec; if (post.Url.ToString().IndexOf("imgur") >= 0) { try { //is imgur _dec = IMGURConsumer.GetImages(post.Url.ToString()); } catch (Exception ex) { return(2); } if (_dec.IsAlbum) { op.HasAlbum = true; //ImageDesc[] _images= new ImageDesc[_dec.Titles.Length]; for (int i = 0; i < _dec.Titles.Length; i++) { ImageDesc _image = new ImageDesc(); _image = new ImageDesc(); _image.ContextURL = _dec.ContextURL; _image.Description = _dec.Descs[i]; _image.FileType = _dec.FileTypes[i]; _image.FUllURL = _dec.FullURLs[i]; _image.IMGURL = _dec.FullURLs[i]; _image.LocalIMGURL = __root + @"reddit\" + post.SubredditName + @"\imgur\" + _dec.FileNames[i]; _image.ParentPostID = op.ID; _image.Site = "www.imgur.com"; _image.Title = _dec.Titles[i]; _image.Save(); Tools.DownloadFile(_image.FUllURL, _image.LocalIMGURL); } } else { op.HasAlbum = false; op.Description = _dec.Descs[0]; op.FileType = _dec.FileTypes[0]; op.IMGURL = _dec.FullURLs[0]; op.LocalIMGURL = __root + @"reddit\" + post.SubredditName + @"\imgur\" + _dec.FileNames[0]; } } else { //non-IMGUR string[] _ext2 = Tools.GetFileExtension(post.Url.ToString()); op.HasAlbum = false; op.Description = "NODESC"; op.FileType = _ext2[1]; op.IMGURL = post.Url.ToString(); op.LocalIMGURL = __root + @"reddit\" + post.SubredditName + @"\else\" + _ext2[0] + _ext2[1]; } if (op.IMGURL != null && op.LocalIMGURL != null) { op.ResourceError = Tools.DownloadFile(op.IMGURL, op.LocalIMGURL); } op.Save(); MasterMind.Archive(op); return(0); }
private static bool IHasIt(RedditSharp.Things.Post post) { return(OriginalPost.rddt_IHazIt(post.Id)); }
/// <summary> /// Plots reddit post based on Post Id /// </summary> /// <param name="dimstyle">object containing dim scales based on the bounding box drawn by user</param> /// <param name="id">Reddit Post ID</param> /// <returns></returns> public static string PlotPost(EntityData dimstyle, string id) { string result = "OK"; try { RedditSharp.Things.Post post = (RedditSharp.Things.Post)PostIds.GetPost(id); if (IsLinkedImg(post.Url.AbsoluteUri)) { //Will insert imageif link is a direct link to the image string filename = GetImage(post.Url.AbsoluteUri); DrawEntity.DrawImg(dimstyle.origin, filename, "REDDIT.IMGS", dimstyle.yLength * .05, dimstyle.yLength * .05); } //if it's only a link to imgur page... List <string> imgurLinks = GetImgurImgs(post.Url.AbsoluteUri); for (int i = 0; i < imgurLinks.Count; i++) { Point3d imgLocation = new Point3d(dimstyle.origin.X + (i * (dimstyle.cornerBase.X - dimstyle.origin.X)), dimstyle.origin.Y, 0); double heightNWith = dimstyle.yLength * 0.025; //Basing height and with on the height of the border drawn by user. if width & height are seperated then img will be distorted string image = GetImage(imgurLinks[i]); DrawEntity.DrawImg(imgLocation, image, "REDDIT.IMGS", heightNWith, heightNWith); } RedditSharp.Reddit reddit = new RedditSharp.Reddit(); if (post != null) { //Post generated looks like this with indentation int i = -1; //Multiplier to help to align text in the Y axis. double yTextLocation = dimstyle.origin.Y; double mtextActualHeight = (dimstyle.yLength * .0025); Point2d subHeadingLocation = new Point2d(dimstyle.origin.X, dimstyle.origin.Y); foreach (var comment in post.Comments.Take(50)) { Tuple <double, double> widthHeight; // A*C*T*U*A*L width & height of the mtext created yTextLocation -= (mtextActualHeight - (dimstyle.yLength * .0025)); //adds height padding Point2d commentLocation = new Point2d(dimstyle.origin.X, yTextLocation); widthHeight = DrawEntity.DrawText(commentLocation, dimstyle.commentsSize, dimstyle.textWidth, comment.Body, "REDDIT.COMMENTS", 7); mtextActualHeight = widthHeight.Item2; subHeadingLocation = new Point2d(dimstyle.origin.X, yTextLocation - mtextActualHeight); string subHeading = string.Format("By:{0} | Upvotes: {1} | Submitted: {2}", comment.Author, comment.Upvotes, post.Created.ToString()); DrawEntity.DrawText(subHeadingLocation, dimstyle.subCommentsSize, dimstyle.textWidth, subHeading, "REDDIT.COMMENTS.SUBHEADINGS", 251); i--; foreach (var subcomment in comment.Comments.Take(20)) { yTextLocation -= (mtextActualHeight - (dimstyle.yLength * .0025)); //adds height padding commentLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.025), yTextLocation); widthHeight = DrawEntity.DrawText(commentLocation, (dimstyle.commentsSize * 0.75), dimstyle.textWidth, subcomment.Body, "REDDIT.COMMENTS", 7); mtextActualHeight = widthHeight.Item2; subHeadingLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.025), yTextLocation - mtextActualHeight); subHeading = string.Format("By:{0} | Upvotes: {1} | Submitted: {2}", subcomment.Author, subcomment.Upvotes, subcomment.Created.ToString()); DrawEntity.DrawText(subHeadingLocation, (dimstyle.subCommentsSize * 0.75), dimstyle.textWidth, subHeading, "REDDIT.COMMENTS.SUBHEADINGS", 251); i--; foreach (var subsubcomment in subcomment.Comments.Take(15)) { yTextLocation -= (mtextActualHeight - (dimstyle.yLength * .0025)); //adds height padding commentLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.05), yTextLocation); widthHeight = DrawEntity.DrawText(commentLocation, (dimstyle.commentsSize * 0.5), dimstyle.textWidth, subsubcomment.Body, "REDDIT.COMMENTS", 7); mtextActualHeight = widthHeight.Item2; subHeadingLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.05), yTextLocation - mtextActualHeight); subHeading = string.Format("By:{0} | Upvotes: {1} | Submitted: {2}", subsubcomment.Author, subsubcomment.Upvotes, subsubcomment.Created.ToString()); DrawEntity.DrawText(subHeadingLocation, (dimstyle.subCommentsSize * 0.5), dimstyle.textWidth, subHeading, "REDDIT.COMMENTS.SUBHEADINGS", 251); i--; foreach (var subsubsubcomment in subsubcomment.Comments.Take(10)) { yTextLocation -= (mtextActualHeight - (dimstyle.yLength * .0025)); //adds height padding commentLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.075), yTextLocation); widthHeight = DrawEntity.DrawText(commentLocation, (dimstyle.commentsSize * .25), dimstyle.textWidth, subsubsubcomment.Body, "REDDIT.COMMENTS", 7); mtextActualHeight = widthHeight.Item2; subHeadingLocation = new Point2d(dimstyle.origin.X + ((dimstyle.cornerBase.X - dimstyle.origin.X) * 0.075), yTextLocation - mtextActualHeight); subHeading = string.Format("By:{0} | Upvotes: {1} | Submitted: {2}", subsubsubcomment.Author, subsubsubcomment.Upvotes, subsubsubcomment.Created.ToString()); DrawEntity.DrawText(subHeadingLocation, (dimstyle.subCommentsSize * 0.25), dimstyle.textWidth, subHeading, "REDDIT.COMMENTS.SUBHEADINGS", 251); i--; } } } } Point3d endPt = new Point3d(dimstyle.cornerBase.X - ((dimstyle.cornerBase.X - dimstyle.origin.X) / 2), subHeadingLocation.Y - (subHeadingLocation.Y * 0.0025), 0); DrawEntity.DrawBox(dimstyle.origin, endPt, "REDDIT.BORDER", 20); DrawEntity.DrawDim(dimstyle.origin, new Point3d(endPt.X, dimstyle.origin.Y, 0), "POST ID : " + id, "REDDIT.COMMENTS", dimstyle.subHeadingTxtSize, 1.005); } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message); result = "FAILED"; } return(result); }