Esempio n. 1
0
 public UserFlairTemplate[] GetUserFlairTemplates() // Hacky, there isn't a proper endpoint for this
 {
     var request = Reddit.CreatePost(FlairSelectorUrl);
     var stream = request.GetRequestStream();
     Reddit.WritePostBody(stream, new
     {
         name = Reddit.User.Name,
         r = Name,
         uh = Reddit.User.Modhash
     });
     stream.Close();
     var response = request.GetResponse();
     var data = Reddit.GetResponseString(response.GetResponseStream());
     var document = new HtmlDocument();
     document.LoadHtml(data);
     if (document.DocumentNode.Descendants("div").First().Attributes["error"] != null)
         throw new InvalidOperationException("This subreddit does not allow users to select flair.");
     var templateNodes = document.DocumentNode.Descendants("li");
     var list = new List<UserFlairTemplate>();
     foreach (var node in templateNodes)
     {
         list.Add(new UserFlairTemplate
         {
             CssClass = node.Descendants("span").First().Attributes["class"].Value.Split(' ')[1],
             Text = node.Descendants("span").First().InnerText
         });
     }
     return list.ToArray();
 }
Esempio n. 2
0
        /// <summary>
        /// Replaces the text in this post with the input text.
        /// </summary>
        /// <param name="newText">The text to replace the post's contents</param>
        public void EditText(string newText)
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }
            if (!IsSelfPost)
            {
                throw new Exception("Submission to edit is not a self-post.");
            }

            var request = Reddit.CreatePost(EditUserTextUrl);

            Reddit.WritePostBody(request.GetRequestStream(), new
            {
                api_type = "json",
                text     = newText,
                thing_id = FullName,
                uh       = Reddit.User.Modhash
            });
            var    response = request.GetResponse();
            var    result   = Reddit.GetResponseString(response.GetResponseStream());
            JToken json     = JToken.Parse(result);

            if (json["json"].ToString().Contains("\"errors\": []"))
            {
                SelfText = newText;
            }
            else
            {
                throw new Exception("Error editing text.");
            }
        }
Esempio n. 3
0
        public Comment Reply(string message)
        {
            if (Reddit.User == null)
            {
                throw new AuthenticationException("No user logged in.");
            }
            var request = Reddit.CreatePost(CommentUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                text     = message,
                thing_id = FullName,
                uh       = Reddit.User.Modhash,
                api_type = "json"
                           //r = Subreddit
            });
            stream.Close();
            try
            {
                var response = request.GetResponse();
                var data     = Reddit.GetResponseString(response.GetResponseStream());
                var json     = JObject.Parse(data);
                if (json["json"]["ratelimit"] != null)
                {
                    throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault <double>()));
                }
                return(new Comment(Reddit, json["json"]["data"]["things"][0]));
            }
            catch (WebException ex)
            {
                var error = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
                return(null);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Submits a link post in the current subreddit using the logged-in user
        /// </summary>
        /// <param name="title">The title of the submission</param>
        /// <param name="url">The url of the submission link</param>
        public Post SubmitPost(string title, string url)
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }
            var request = Reddit.CreatePost(SubmitLinkUrl);

            Reddit.WritePostBody(request.GetRequestStream(), new
            {
                api_type  = "json",
                extension = "json",
                kind      = "link",
                sr        = Title,
                title     = title,
                uh        = Reddit.User.Modhash,
                url       = url
            });
            var response = request.GetResponse();
            var result   = Reddit.GetResponseString(response.GetResponseStream());
            var json     = JToken.Parse(result);

            return(new Post(Reddit, json["json"]));
            // TODO: Error
        }
Esempio n. 5
0
 public void AcceptModeratorInvite()
 {
     var request = Reddit.CreatePost(AcceptModeratorInviteUrl);
     Reddit.WritePostBody(request.GetRequestStream(), new
     {
         api_type = "json",
         uh = Reddit.User.Modhash,
         r = Name
     });
     var response = request.GetResponse();
     var result = Reddit.GetResponseString(response.GetResponseStream());
 }
Esempio n. 6
0
        public void SetAsRead()
        {
            var request = Reddit.CreatePost(SetAsReadUrl);

            Reddit.WritePostBody(request.GetRequestStream(), new
            {
                id       = FullName,
                uh       = Reddit.User.Modhash,
                api_type = "json"
            });
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
        }
Esempio n. 7
0
 public void RemoveModerator(string id)
 {
     var request = Reddit.CreatePost(LeaveModerationUrl);
     Reddit.WritePostBody(request.GetRequestStream(), new
     {
         api_type = "json",
         uh = Reddit.User.Modhash,
         r = Name,
         type = "moderator",
         id
     });
     var response = request.GetResponse();
     var result = Reddit.GetResponseString(response.GetResponseStream());
 }
Esempio n. 8
0
 public void ClearFlairTemplates(FlairType flairType)
 {
     var request = Reddit.CreatePost(ClearFlairTemplatesUrl);
     var stream = request.GetRequestStream();
     Reddit.WritePostBody(stream, new
     {
         flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR",
         uh = Reddit.User.Modhash,
         r = Name
     });
     stream.Close();
     var response = request.GetResponse();
     var data = Reddit.GetResponseString(response.GetResponseStream());
 }
Esempio n. 9
0
        /// <summary>
        /// Resets the subreddit's header image to the Reddit logo
        /// </summary>
        public void ResetHeaderImage()
        {
            var request = Reddit.CreatePost(DeleteHeaderImageUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                uh = Reddit.User.Modhash,
                r  = Subreddit.Name
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
        }
Esempio n. 10
0
        public void Approve()
        {
            var request = Reddit.CreatePost(ApproveUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                id = FullName,
                uh = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
        }
Esempio n. 11
0
 public void SetUserFlair(string user, string cssClass, string text)
 {
     var request = Reddit.CreatePost(SetUserFlairUrl);
     var stream = request.GetRequestStream();
     Reddit.WritePostBody(stream, new
     {
         css_class = cssClass,
         text = text,
         uh = Reddit.User.Modhash,
         r = Name,
         name = user
     });
     stream.Close();
     var response = request.GetResponse();
     var data = Reddit.GetResponseString(response.GetResponseStream());
 }
Esempio n. 12
0
        public void Delete()
        {
            var request = Reddit.CreatePost(DeleteImageUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                img_name = Name,
                uh       = Reddit.User.Modhash,
                r        = SubredditStyle.Subreddit.Name
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());

            SubredditStyle.Images.Remove(this);
        }
Esempio n. 13
0
        public void Vote(VoteType type)
        {
            var request = Reddit.CreatePost(VoteUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                dir = (int)type,
                id  = FullName,
                uh  = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());

            Liked = null;
        }
Esempio n. 14
0
 public void Unsubscribe()
 {
     if (Reddit.User == null)
         throw new AuthenticationException("No user logged in.");
     var request = Reddit.CreatePost(SubscribeUrl);
     var stream = request.GetRequestStream();
     Reddit.WritePostBody(stream, new
     {
         action = "unsub",
         sr = FullName,
         uh = Reddit.User.Modhash
     });
     stream.Close();
     var response = request.GetResponse();
     var data = Reddit.GetResponseString(response.GetResponseStream());
     // Discard results
 }
Esempio n. 15
0
        public void Downvote()
        {
            var request = Reddit.CreatePost(VoteUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                dir = -1,
                id  = FullName,
                uh  = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());

            Liked = false;
        }
Esempio n. 16
0
        public void UpdateCss()
        {
            var request = Reddit.CreatePost(UpdateCssUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                op = "save",
                stylesheet_contents = CSS,
                uh       = Reddit.User.Modhash,
                api_type = "json",
                r        = Subreddit.Name
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
            var json     = JToken.Parse(data);
        }
Esempio n. 17
0
        public void Unhide()
        {
            if (Reddit.User == null)
            {
                throw new AuthenticationException("No user logged in.");
            }
            var request = Reddit.CreatePost(UnhideUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                id = FullName,
                uh = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
        }
Esempio n. 18
0
        public void Distinguish(DistinguishType distinguishType)
        {
            if (Reddit.User == null)
            {
                throw new AuthenticationException("No user logged in.");
            }
            var    request = Reddit.CreatePost(DistinguishUrl);
            var    stream  = request.GetRequestStream();
            string how;

            switch (distinguishType)
            {
            case DistinguishType.Admin:
                how = "admin";
                break;

            case DistinguishType.Moderator:
                how = "yes";
                break;

            case DistinguishType.None:
                how = "no";
                break;

            default:
                how = "special";
                break;
            }
            Reddit.WritePostBody(stream, new
            {
                how,
                id = Id,
                uh = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(data);

            if (json["jquery"].Count(i => i[0].Value <int>() == 11 && i[1].Value <int>() == 12) == 0)
            {
                throw new AuthenticationException("You are not permitted to distinguish this comment.");
            }
        }
Esempio n. 19
0
 public void AddFlairTemplate(string cssClass, FlairType flairType, string text, bool userEditable)
 {
     var request = Reddit.CreatePost(FlairTemplateUrl);
     var stream = request.GetRequestStream();
     Reddit.WritePostBody(stream, new
     {
         css_class = cssClass,
         flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR",
         text = text,
         text_editable = userEditable,
         uh = Reddit.User.Modhash,
         r = Name,
         api_type = "json"
     });
     stream.Close();
     var response = request.GetResponse();
     var data = Reddit.GetResponseString(response.GetResponseStream());
     var json = JToken.Parse(data);
 }
Esempio n. 20
0
        public void Reply(string message)
        {
            if (Reddit.User == null)
            {
                throw new AuthenticationException("No user logged in.");
            }
            var request = Reddit.CreatePost(CommentUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                text     = message,
                thing_id = FullName,
                uh       = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(data);
        }
Esempio n. 21
0
        public Comment Comment(string message)
        {
            if (Reddit.User == null)
            {
                throw new AuthenticationException("No user logged in.");
            }
            var request = Reddit.CreatePost(CommentUrl);
            var stream  = request.GetRequestStream();

            Reddit.WritePostBody(stream, new
            {
                text     = message,
                thing_id = FullName,
                uh       = Reddit.User.Modhash
            });
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(data);
            var comment  = json["jquery"].FirstOrDefault(i => i[0].Value <int>() == 18 && i[1].Value <int>() == 19);

            return(new Comment(Reddit, comment[3][0][0]));
        }
Esempio n. 22
0
        public void UpdateSettings()
        {
            var    request = Reddit.CreatePost(SiteAdminUrl);
            var    stream  = request.GetRequestStream();
            string link_type;
            string type;
            string wikimode;

            switch (ContentOptions)
            {
            case RedditSharp.ContentOptions.All:
                link_type = "any";
                break;

            case RedditSharp.ContentOptions.LinkOnly:
                link_type = "link";
                break;

            default:
                link_type = "self";
                break;
            }
            switch (SubredditType)
            {
            case SubredditType.Public:
                type = "public";
                break;

            case SubredditType.Private:
                type = "private";
                break;

            default:
                type = "restricted";
                break;
            }
            switch (WikiEditMode)
            {
            case WikiEditMode.All:
                wikimode = "anyone";
                break;

            case WikiEditMode.Moderators:
                wikimode = "modonly";
                break;

            default:
                wikimode = "disabled";
                break;
            }
            Reddit.WritePostBody(stream, new
            {
                allow_top   = AllowAsDefault,
                description = Sidebar,
                domain      = Domain,
                lang        = Language,
                link_type,
                over_18            = NSFW,
                public_description = PublicDescription,
                show_media         = ShowThumbnails,
                sr    = Subreddit.FullName,
                title = Title,
                type,
                uh              = Reddit.User.Modhash,
                wiki_edit_age   = WikiEditAge,
                wiki_edit_karma = WikiEditKarma,
                wikimode,
                api_type = "json"
            }, "header-title", HeaderHoverText);
            stream.Close();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
        }