示例#1
0
        private void ReplyRequest(IAsyncResult ar)
        {
            CommentState   commentState = (CommentState)ar.AsyncState;
            HttpWebRequest request      = commentState.AsyncRequest;
            Stream         stream       = request.EndGetRequestStream(ar);

            Reddit.WritePostBody(stream, new
            {
                text     = (String)commentState.ParameterValue,
                thing_id = FullName,
                uh       = Reddit.User.Modhash,
                api_type = "json"
                           //r = Subreddit
            });
        }
示例#2
0
        public async Task SetUserFlair(string user, string cssClass, string text)
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                css_class = cssClass,
                text      = text,
                uh        = Reddit.User.Modhash,
                r         = Name,
                name      = user
            });
            var response = await client.PostAsync(SetUserFlairUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();
        }
示例#3
0
        public async Task RemoveModerator(string id)
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                api_type = "json",
                uh       = Reddit.User.Modhash,
                r        = Name,
                type     = "moderator",
                id
            });
            var response = await client.PostAsync(LeaveModerationUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();
        }
示例#4
0
        private void DistinguishResponse(IAsyncResult ar)
        {
            CommentState   commentState = (CommentState)ar.AsyncState;
            HttpWebRequest request      = commentState.AsyncRequest;

            commentState.AsyncResponse = (HttpWebResponse)request.EndGetResponse(ar);

            var data = Reddit.GetResponseString(commentState.AsyncResponse.GetResponseStream());
            var json = JObject.Parse(data);

            if (json["jquery"].Count(i => i[0].Value <int>() == 11 && i[1].Value <int>() == 12) == 0)
            {
                throw new Exception("You are not permitted to distinguish this comment.");
            }
        }
示例#5
0
        public void EditText(string newText)
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }

            var          request      = Reddit.CreatePost(EditUserTextUrl);
            CommentState commentState = new CommentState();

            commentState.AsyncRequest   = request;
            commentState.ParameterValue = newText;

            request.BeginGetRequestStream(new AsyncCallback(EditTextRequest), commentState);
        }
示例#6
0
        public async Task Vote(VoteType type)
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                dir = (int)type,
                id  = FullName,
                uh  = Reddit.User.Modhash
            });
            var response = await client.PostAsync(VoteUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            Liked = null;
        }
示例#7
0
        public async Task Delete()
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                img_name = Name,
                uh       = Reddit.User.Modhash,
                r        = SubredditStyle.Subreddit.Name
            });
            var response = await client.PostAsync(DeleteImageUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            SubredditStyle.Images.Remove(this);
        }
示例#8
0
        // an attempt at using Task and System.Net.Http
        public async Task <Comment[]> GetComments()
        {
            var comments = new List <Comment>();
            // create a new HttpClient
            HttpClient client = Reddit.CreateClient();
            string     body   = await client.GetStringAsync(string.Format(GetCommentsUrl, Id));

            var json     = JArray.Parse(body);
            var postJson = json.Last()["data"]["children"];

            foreach (var comment in postJson)
            {
                comments.Add(new Comment(Reddit, comment));
            }
            return(comments.ToArray());
        }
示例#9
0
 protected internal Subreddit(Reddit reddit, JToken json)
     : base(json)
 {
     Reddit = reddit;
     JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
     Name = Url;
     if (Name.StartsWith("/r/"))
     {
         Name = Name.Substring(3);
     }
     if (Name.StartsWith("r/"))
     {
         Name = Name.Substring(2);
     }
     Name = Name.TrimEnd('/');
 }
示例#10
0
        public async Task Downvote()
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                dir = -1,
                id  = FullName,
                uh  = Reddit.User.Modhash
            });
            var response = await client.PostAsync(VoteUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // TODO: check the data for success or failure
            Liked = false;
        }
示例#11
0
        private void EditTextRequest(IAsyncResult ar)
        {
            CommentState   commentState = (CommentState)ar.AsyncState;
            HttpWebRequest request      = commentState.AsyncRequest;
            Stream         stream       = request.EndGetRequestStream(ar);

            Reddit.WritePostBody(stream, new
            {
                api_type = "json",
                text     = (String)commentState.ParameterValue,
                thing_id = FullName,
                uh       = Reddit.User.Modhash
            });

            request.BeginGetResponse(new AsyncCallback(EditTextResponse), commentState);
        }
示例#12
0
        public Comment Comment(string message)
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }
            StateObject postState = new StateObject();
            var         request   = Reddit.CreatePost(CommentUrl);

            postState.Request        = request;
            postState.ParameterValue = message;

            IAsyncResult commentRequestAR  = request.BeginGetRequestStream(new AsyncCallback(CommentRequest), postState);
            IAsyncResult commentResponseAR = request.BeginGetResponse(new AsyncCallback(CommentResponse), postState);

            return(returnComment);
        }
示例#13
0
        public async Task UpdateCss()
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                op = "save",
                stylesheet_content = CSS,
                uh       = Reddit.User.Modhash,
                api_type = "json",
                r        = Subreddit.Name
            });
            var response = await client.PostAsync(UpdateCssUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            var json = JToken.Parse(responseContent);
        }
示例#14
0
        public async Task AddFlairTemplate(string cssClass, FlairType flairType, string text, bool userEditable)
        {
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(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"
            });
            var response = await client.PostAsync(FlairTemplateUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            var json = JToken.Parse(responseContent);
        }
示例#15
0
        private void EditTextResponse(IAsyncResult ar)
        {
            CommentState   commentState = (CommentState)ar.AsyncState;
            HttpWebRequest request      = commentState.AsyncRequest;

            commentState.AsyncResponse = (HttpWebResponse)request.EndGetResponse(ar);

            var    data = Reddit.GetResponseString(commentState.AsyncResponse.GetResponseStream());
            JToken json = JToken.Parse(data);

            if (json["json"].ToString().Contains("\"errors\": []"))
            {
                Body = (String)commentState.ParameterValue;
            }
            else
            {
                throw new Exception("Error editing text.");
            }
        }
示例#16
0
        public async Task Unsubscribe()
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                action = "unsub",
                sr     = FullName,
                uh     = Reddit.User.Modhash
            });
            var response = await client.PostAsync(SubscribeUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Do something with the results or discard them
        }
示例#17
0
        public async Task Reply(string message)
        {
            if (Reddit.User == null)
            {
                throw new Exception("No user logged in.");
            }
            HttpClient    client  = Reddit.CreateClient();
            StringContent content = Reddit.StringForPost(new
            {
                text     = message,
                thing_id = FullName,
                uh       = Reddit.User.Modhash
            });
            var response = await client.PostAsync(CommentUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            var json = JObject.Parse(responseContent);
        }
示例#18
0
        public Comment Reply(string message)
        {
            if (Reddit.User == null)
            {
                // RedditSharp used an AuthenticationException
                // but it's not available for Windows Phone
                throw new Exception("No user logged in.");
            }

            CommentState commentState = new CommentState();
            var          request      = Reddit.CreatePost(CommentUrl);

            commentState.AsyncRequest   = request;
            commentState.ParameterValue = message;

            IAsyncResult replyRequestAR  = request.BeginGetRequestStream(new AsyncCallback(ReplyRequest), commentState);
            IAsyncResult replyResponseAR = request.BeginGetResponse(new AsyncCallback(ReplyResponse), commentState);

            return(returnComment);
        }
示例#19
0
        public Comment(Reddit reddit, JToken json)
            : base(reddit, json)
        {
            var data = json["data"];

            JsonConvert.PopulateObject(data.ToString(), this, reddit.JsonSerializerSettings);
            Reddit = reddit;

            // Parse sub comments
            // TODO: Consider deserializing this properly
            var subComments = new List <Comment>();

            if (data["replies"] != null && data["replies"].Any())
            {
                foreach (var comment in data["replies"]["data"]["children"])
                {
                    subComments.Add(new Comment(reddit, comment));
                }
            }
            Comments = subComments.ToArray();
        }
示例#20
0
 public SubredditSettings(Reddit reddit, Subreddit subreddit)
 {
     Subreddit = subreddit;
     Reddit    = reddit;
     // Default settings, for use when reduced information is given
     AllowAsDefault    = true;
     Domain            = null;
     Sidebar           = string.Empty;
     Language          = "en";
     Title             = Subreddit.DisplayName;
     WikiEditKarma     = 100;
     WikiEditAge       = 10;
     UseDomainCss      = false;
     UseDomainSidebar  = false;
     HeaderHoverText   = string.Empty;
     NSFW              = false;
     PublicDescription = string.Empty;
     WikiEditMode      = WikiEditMode.None;
     SubredditType     = SubredditType.Public;
     ShowThumbnails    = true;
     ContentOptions    = ContentOptions.All;
 }
示例#21
0
        public PrivateMessage(Reddit reddit, JToken json)
            : base(json)
        {
            Reddit = reddit;
            JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
            var data = json["data"];

            if (data["replies"] != null && data["replies"].Any())
            {
                if (data["replies"]["data"] != null)
                {
                    if (data["replies"]["data"]["children"] != null)
                    {
                        var replies = new List <PrivateMessage>();
                        foreach (var reply in data["replies"]["data"]["children"])
                        {
                            replies.Add(new PrivateMessage(reddit, reply));
                        }
                        Replies = replies.ToArray();
                    }
                }
            }
        }
示例#22
0
        private void DistinguishRequest(IAsyncResult ar)
        {
            CommentState    commentState    = (CommentState)ar.AsyncState;
            DistinguishType distinguishType = (DistinguishType)commentState.ParameterValue;
            HttpWebRequest  request         = commentState.AsyncRequest;
            Stream          stream          = request.EndGetRequestStream(ar);

            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
            });

            request.BeginGetResponse(new AsyncCallback(DistinguishResponse), commentState);
        }
示例#23
0
        public async Task UpdateSettings()
        {
            HttpClient client = Reddit.CreateClient();
            string     link_type;
            string     type;
            string     wikimode;

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

            case RedditWP.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;
            }
            StringContent content = Reddit.StringForPost(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);
            var response = await client.PostAsync(SiteAdminUrl, content);

            var responseContent = await response.Content.ReadAsStringAsync();
        }
示例#24
0
        public SubredditSettings(Subreddit subreddit, Reddit reddit, JObject json)
            : this(reddit, subreddit)
        {
            var data = json["data"];

            AllowAsDefault    = data["default_set"].ValueOrDefault <bool>();
            Domain            = data["domain"].ValueOrDefault <string>();
            Sidebar           = HttpUtility.HtmlDecode(data["description"].ValueOrDefault <string>() ?? string.Empty);
            Language          = data["language"].ValueOrDefault <string>();
            Title             = data["title"].ValueOrDefault <string>();
            WikiEditKarma     = data["wiki_edit_karma"].ValueOrDefault <int>();
            UseDomainCss      = data["domain_css"].ValueOrDefault <bool>();
            UseDomainSidebar  = data["domain_sidebar"].ValueOrDefault <bool>();
            HeaderHoverText   = data["header_hover_text"].ValueOrDefault <string>();
            NSFW              = data["over_18"].ValueOrDefault <bool>();
            PublicDescription = HttpUtility.HtmlDecode(data["public_description"].ValueOrDefault <string>() ?? string.Empty);
            if (data["wikimode"] != null)
            {
                string wikiMode = data["wikimode"].ValueOrDefault <string>();
                switch (wikiMode)
                {
                case "disabled":
                    WikiEditMode = WikiEditMode.None;
                    break;

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

                case "anyone":
                    WikiEditMode = WikiEditMode.All;
                    break;
                }
            }
            if (data["subreddit_type"] != null)
            {
                string type = data["subreddit_type"].ValueOrDefault <string>();
                switch (type)
                {
                case "public":
                    SubredditType = SubredditType.Public;
                    break;

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

                case "restricted":
                    SubredditType = SubredditType.Restricted;
                    break;
                }
            }
            ShowThumbnails = data["show_media"].ValueOrDefault <bool>();
            WikiEditAge    = data["wiki_edit_age"].ValueOrDefault <int>();
            if (data["content_options"] != null)
            {
                string contentOptions = data["content_options"].ValueOrDefault <string>();
                switch (contentOptions)
                {
                case "any":
                    ContentOptions = ContentOptions.All;
                    break;

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

                case "self":
                    ContentOptions = ContentOptions.SelfOnly;
                    break;
                }
            }
        }
示例#25
0
 private void RemoveSpamResponse(IAsyncResult ar)
 {
     HttpWebRequest  request  = (HttpWebRequest)ar.AsyncState;
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
     var             data     = Reddit.GetResponseString(response.GetResponseStream());
 }
示例#26
0
        public void RemoveSpam()
        {
            var request = Reddit.CreatePost(RemoveUrl);

            request.BeginGetRequestStream(new AsyncCallback(RemoveSpamRequest), request);
        }
示例#27
0
 public Post(Reddit reddit, JToken post)
     : base(reddit, post)
 {
     Reddit = reddit;
     JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings);
 }
示例#28
0
 public CreatedThing(Reddit reddit, JToken json)
     : base(json)
 {
     Reddit = reddit;
     JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
 }
示例#29
0
 public AuthenticatedUser(Reddit reddit, JToken json)
     : base(reddit, json)
 {
     JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
 }
示例#30
0
 public SubredditStyle(Reddit reddit, Subreddit subreddit)
 {
     Reddit    = reddit;
     Subreddit = subreddit;
 }