/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="sub"></param> /// <see cref="https://github.com/reddit/reddit/wiki/API%3A-flairlist"/> /// <returns></returns> public static FlairListing GetFlair(Session session, string sub, string after, string before) { // var url = "http://www.reddit.com/api/flairlist?r=" + sub + "&limit=1000&uh=" + session.ModHash; var url = "http://www.reddit.com/r/" + sub + "/api/flairlist.json?uh=" + session.ModHash + "&limit=1000"; if (!string.IsNullOrEmpty(after)) url += "&after=" + after; if (!string.IsNullOrEmpty(before)) url += "&before=" + before; var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); // convert to a post listing var list = FlairListing.FromJson(o["users"]); list.Next = o["next"] == null ? string.Empty : o["next"].ToString(); list.Prev = o["prev"] == null ? string.Empty : o["prev"].ToString(); return list; }
public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort, string more) { var url = "http://www.reddit.com/comments/" + id + ".json"; var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } // use some tricky recursive linq to get the comment structure, // understanding this required the use of caffeine. I'm tired. var o = JArray.Parse(json); // skip / ignore the post // o[0] // loop over the top level comments var list = new CommentListing(); var comments = o[1]["data"]["children"]; //.Select(t => t["data"]); list.ModHash = o[1]["data"]["modhash"].ToString(); foreach (var comment in comments) { switch (comment["kind"].ToString()) { case "t1": list.Add(new Comment(comment["data"])); break; case "more": var test = comment["data"]["children"].Values().Select(t => t.ToString()).ToList(); list.More.AddRange(test); break; } } return(list); }
public static PostListing GetPosts(Session session) { var request = new Request { Url = "http://www.reddit.com/r/friends/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return PostListing.FromJson(o); }
public static PostListing Query(Session session, string qry, string after, string before) { var request = new Request { Url = "http://www.reddit.com/search.json?q=" + qry, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return PostListing.FromJson(o); }
/// <summary> /// https://github.com/reddit/reddit/wiki/API:-hide /// </summary> /// <param name="session"></param> /// <param name="id"></param> internal static void Hide(Session session, string id, string modhash) { var request = new Request { Url = "http://www.reddit.com/api/hide", Method = "POST", Content = "uh=" + modhash + "&id=" + id, Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); if (json != "{}") throw new Exception(json); }
public static void Logout(Session session) { var request = new Request { Url = "http://www.reddit.com/logout?uh=" + session.ModHash, Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&top=off" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="post"></param> /// <see cref="https://github.com/reddit/reddit/wiki/API"/> public static void Submit(Session session, Post post, PostKind kind) { if (string.IsNullOrEmpty((kind == PostKind.Link ? post.Url : post.SelfText))) { throw new Exception("No link or self text added to the new post"); } if (string.IsNullOrEmpty(post.SubReddit)) { throw new Exception("No subreddit set"); } if (string.IsNullOrEmpty(post.Title)) { throw new Exception("No title provided"); } var request = new Request { Url = "http://www.reddit.com/api/submit", Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&kind=" + (kind == PostKind.Link ? "link" : "self") + "&url=" + (kind == PostKind.Link ? post.Url : post.SelfText) + "&sr=" + post.SubReddit + "&title=" + post.Title + "&r=" + post.SubReddit + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } var o = JObject.Parse(json); // Capcha // o["jquery"][10][3].ToString() // Error Message // o["jquery"][12][3].ToString() }
public static Session Login(string username, string password) { var request = new Request { Url = "https://ssl.reddit.com/api/login/" + username, Method = "POST", Content = "api_type=json&user="******"&passwd=" + password }; // get the modhash var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } /* // Expected Json * { * "json": { * "errors": [], * "data": { * "modhash": "u4abc21302316ad40013feb16cfccb0b11b786596e5194de14", * "cookie": "1234567,2011-07-12T14:53:59,0200b365fa02c61f9532ab244b214bd481941492" * } * } * } */ // Failure // {"json": {"errors": [["WRONG_PASSWORD", "invalid password"]]}} var o = JObject.Parse(json); // Create a session which can be used on further requests return(new Session { Username = username, Password = password, ModHash = o["json"]["data"]["modhash"].ToString(), // reddit_session=8010059%2C2011-01-26T06%3A16%3A49%2C8e77c04fb74713f923c6ed7f44ae8374300db499; Cookie = "reddit_session=" + System.Web.HttpUtility.UrlEncode(o["json"]["data"]["cookie"].ToString()) }); }
public static void Read(Session session, params string[] id) { var request = new Request { Url = "http://www.reddit.com/api/read_message", Method = "POST", Cookie = session.Cookie, Content = "id=" + string.Join(",", id) + "&uh=" + session.ModHash }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); }
/// <summary> /// Get information about a URL or domain, find all the stories and comments /// associated within. /// </summary> /// <param name="session"></param> /// <param name="url"></param> /// <returns></returns> public static PostListing FromUrl(Session session, string url) { // build the request var request = new Request { Url = "http://www.reddit.com/api/info.json?url=" + url, Method = "GET", Cookie = session.Cookie }; // execute the request var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); // convert to a post listing var o = JObject.Parse(json); return PostListing.FromJson(o); }
public static PostListing Query(Session session, string qry, string after, string before) { var request = new Request { Url = "http://www.reddit.com/search.json?q=" + qry, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(PostListing.FromJson(o)); }
public static CommentListing GetComments(Session session) { var request = new Request { Url = "http://www.reddit.com/r/friends/comments/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(CommentListing.FromJson(o)); }
public static SubListing GetMineModerated(Session session) { var request = new Request { Url = "http://www.reddit.com/reddits/mine/moderator.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(SubListing.FromJson(o)); }
public static MessageListing Sent(Session session) { var request = new Request { Method = "GET", Cookie = session.Cookie, Url = "http://www.reddit.com/message/sent/.json" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(MessageListing.FromJson(o)); }
public static UserListing GetBannedUsers(Session session, string sub) { var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/banned/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(UserListing.FromJson(o)); }
public static LogListing GetModerationLog(Session session, string sub) { var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/log/", Method = "GET", Cookie = session.Cookie }; var html = string.Empty; if (request.Execute(out html) != System.Net.HttpStatusCode.OK) { throw new RedditException(html); } // build JSON? return(LogListing.FromHtml(html)); }
public static PostListing GetDisliked(Session session, string username) { // var request = new Request { Url = "http://www.reddit.com/user/" + username + "/disliked/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(PostListing.FromJson(o)); }
public static User Get(Session session, string username) { // build a request var request = new Request { Url = "http://www.reddit.com/user/" + username + "/about.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } var o = JObject.Parse(json); return(User.FromJson(o["data"])); }
// List submissions from friends /// http://www.reddit.com/r/friends/.json /// public static UserListing List(Session session) { var request = new Request { Url = "https://ssl.reddit.com/prefs/friends.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } var o = JObject.Parse(json); var list = UserListing.FromJson(o); return(list); }
public static Sub Get(Session session, string sub) { // var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); return(Sub.FromJson(o["data"])); }
public static PostListing GetRelated(Session session, string id) { // Make sure we process the request with the type removed, so // we just pass the base-36 ID id = id.Replace("t3_", string.Empty); // build a request var request = new Request { Url = "http://www.reddit.com/related/" + id + ".json", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } return(JsonConvert.DeserializeObject <PostListing>(json)); }
private static void Vote(Session session, string id, string modhash, int direction) { var request = new Request { Url = "http://www.reddit.com/api/vote", Method = "POST", Content = "uh=" + modhash + "&id=" + id + "&dir=" + direction.ToString(), Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } if (json != "{}") { throw new Exception(json); } }
internal static void UnHide(Session session, string id, string modhash) { var request = new Request { Url = "http://www.reddit.com/api/unhide", Method = "POST", Content = "uh=" + modhash + "&id=" + id, Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } if (json != "{}") { throw new Exception(json); } }
public static void GetSendForm(Session session, out string iden, out string captcha) { var request = new Request { Cookie = session.Cookie, Method = "GET", Url = "http://www.reddit.com/message/compose" }; var html = string.Empty; if (request.Execute(out html) != System.Net.HttpStatusCode.OK) { throw new Exception(html); } // use NSOUP to get the iden string and captcha URL var client = NSoup.NSoupClient.Parse(html); iden = client.Select("#compose-message input[name=iden]").Val(); captcha = client.Select("#compose-message img.capimage").Attr("src"); }
public static void Create(Session session, Sub sub) { var subreddit_type = "public"; var language = "en"; var content_options = "any"; var over_18 = false; var default_set = true; var show_media = false; var domain = ""; var request = new Request { Url = "", Method = "POST", Cookie = session.Cookie, Content = "name=" + sub.Name + "&title=" + sub.Title + "&description=" + sub.Description + "&lang=" + language + "&type=" + subreddit_type + "&link_type=" + content_options + "&over_18=" + (over_18 ? "on" : "off") + "&allow_top=" + (default_set ? "on" : "off") + "&show_media=" + (show_media ? "on" : "off") + "&domain=" + domain + "&uh=" + session.ModHash + "&id=#sr-form&api_type=json" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="sub"></param> /// <see cref="https://github.com/reddit/reddit/wiki/API%3A-flairlist"/> /// <returns></returns> public static FlairListing GetFlair(Session session, string sub, string after, string before) { // var url = "http://www.reddit.com/api/flairlist?r=" + sub + "&limit=1000&uh=" + session.ModHash; var url = "http://www.reddit.com/r/" + sub + "/api/flairlist.json?uh=" + session.ModHash + "&limit=1000"; if (!string.IsNullOrEmpty(after)) { url += "&after=" + after; } if (!string.IsNullOrEmpty(before)) { url += "&before=" + before; } var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); // convert to a post listing var list = FlairListing.FromJson(o["users"]); list.Next = o["next"] == null ? string.Empty : o["next"].ToString(); list.Prev = o["prev"] == null ? string.Empty : o["prev"].ToString(); return(list); }
public static void Add(Session session, string username, string id, string modhash) { id = id.StartsWith("t2_") ? id : "t2_" + id; var request = new Request { Url = "http://www.reddit.com/api/friend?note=", Method = "POST", Cookie = session.Cookie, Content = "name=" + username + "&container=" + id + "&type=friend" + "&uh=" + modhash + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); // o["jquery"][20][3][0].ToString() }
public static void Delete(Session session, string sub, string id, string modhash) { var request = new Request { Url = "http://www.reddit.com/api/del", Method = "POST", Cookie = session.Cookie, Content = "id=" + id + "&executed=deleted" + "&r=" + sub + "&uh=" + modhash + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } var o = JObject.Parse(json); }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <returns></returns> public static PostListing List(Session session) { // GET // http://www.reddit.com/.json var request = new Request { Url = "http://www.reddit.com/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } // convert to a post listing var o = JObject.Parse(json); return(PostListing.FromJson(o)); }
/// <summary> /// Get information about a URL or domain, find all the stories and comments /// associated within. /// </summary> /// <param name="session"></param> /// <param name="url"></param> /// <returns></returns> public static PostListing FromUrl(Session session, string url) { // build the request var request = new Request { Url = "http://www.reddit.com/api/info.json?url=" + url, Method = "GET", Cookie = session.Cookie }; // execute the request var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } // convert to a post listing var o = JObject.Parse(json); return(PostListing.FromJson(o)); }
public static PostListing Get(Session session, string id) { // check this is a link (in the correct format) if (!id.StartsWith("t3_")) { throw new RedditException("ID is not of a link/post"); } // build a request var request = new Request { Url = "http://www.reddit.com/by_id/" + id + ".json", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } return(JsonConvert.DeserializeObject <PostListing>(json)); }
public static MessageListing Unread(Session session) { // var request = new Request { Method = "GET", Cookie = session.Cookie, Url = "http://www.reddit.com/message/unread/.json" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); return MessageListing.FromJson(o); }
public static void Send(Session session, Message message, string iden, string captcha) { var request = new Request { Url = "http://www.reddit.com/api/compose", Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&to=" + message.Destination + "&subject=" + message.Subject + "&thing_id=" + "&text=" + message.Body + "&iden=" + iden + "&captcha=" + captcha + "&id=#compose-message" + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { } /* { "jquery": [ [0, 1, "call", ["#compose-message"]], [1, 2, "attr", "find"], [2, 3, "call", [".status"]], [3, 4, "attr", "hide"], [4, 5, "call", []], [5, 6, "attr", "html"], [6, 7, "call", [""]], [7, 8, "attr", "end"], [8, 9, "call", []], [1, 10, "attr", "find"], [10, 11, "call", [".status"]], [11, 12, "attr", "show"], [12, 13, "call", []], [13, 14, "attr", "html"], [14, 15, "call", ["your message has been delivered"]], [15, 16, "attr", "end"], [16, 17, "call", []], [1, 18, "attr", "find"], [18, 19, "call", ["*[name=captcha]"]], [19, 20, "attr", "attr"], [20, 21, "call", ["value", ""]], [21, 22, "attr", "end"], [22, 23, "call", []], [1, 24, "attr", "find"], [24, 25, "call", ["*[name=to]"]], [25, 26, "attr", "attr"], [26, 27, "call", ["value", ""]], [27, 28, "attr", "end"], [28, 29, "call", []], [1, 30, "attr", "find"], [30, 31, "call", ["*[name=text]"]], [31, 32, "attr", "attr"], [32, 33, "call", ["value", ""]], [33, 34, "attr", "end"], [34, 35, "call", []], [1, 36, "attr", "find"], [36, 37, "call", ["*[name=subject]"]], [37, 38, "attr", "attr"], [38, 39, "call", ["value", ""]], [39, 40, "attr", "end"], [40, 41, "call", []] ] } */ }
public static void Read(Session session, params string[] id) { var request = new Request { Url = "http://www.reddit.com/api/read_message", Method = "POST", Cookie = session.Cookie, Content = "id=" + string.Join(",", id) + "&uh=" + session.ModHash }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); }
public static PostListing GetDisliked(Session session, string username) { // var request = new Request { Url = "http://www.reddit.com/user/" + username + "/disliked/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return PostListing.FromJson(o); }
public static void Send(Session session, Message message, string iden, string captcha) { var request = new Request { Url = "http://www.reddit.com/api/compose", Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&to=" + message.Destination + "&subject=" + message.Subject + "&thing_id=" + "&text=" + message.Body + "&iden=" + iden + "&captcha=" + captcha + "&id=#compose-message" + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { } /* * { * "jquery": [ * [0, 1, "call", ["#compose-message"]], * [1, 2, "attr", "find"], * [2, 3, "call", [".status"]], * [3, 4, "attr", "hide"], * [4, 5, "call", []], * [5, 6, "attr", "html"], * [6, 7, "call", [""]], * [7, 8, "attr", "end"], * [8, 9, "call", []], * [1, 10, "attr", "find"], * [10, 11, "call", [".status"]], * [11, 12, "attr", "show"], * [12, 13, "call", []], * [13, 14, "attr", "html"], * [14, 15, "call", ["your message has been delivered"]], * [15, 16, "attr", "end"], * [16, 17, "call", []], * [1, 18, "attr", "find"], * [18, 19, "call", ["*[name=captcha]"]], * [19, 20, "attr", "attr"], * [20, 21, "call", ["value", ""]], * [21, 22, "attr", "end"], * [22, 23, "call", []], * [1, 24, "attr", "find"], * [24, 25, "call", ["*[name=to]"]], * [25, 26, "attr", "attr"], * [26, 27, "call", ["value", ""]], * [27, 28, "attr", "end"], * [28, 29, "call", []], * [1, 30, "attr", "find"], * [30, 31, "call", ["*[name=text]"]], * [31, 32, "attr", "attr"], * [32, 33, "call", ["value", ""]], * [33, 34, "attr", "end"], * [34, 35, "call", []], * [1, 36, "attr", "find"], * [36, 37, "call", ["*[name=subject]"]], * [37, 38, "attr", "attr"], * [38, 39, "call", ["value", ""]], * [39, 40, "attr", "end"], * [40, 41, "call", []] * ] * } */ }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="post"></param> /// <see cref="https://github.com/reddit/reddit/wiki/API"/> public static void Submit(Session session, Post post, PostKind kind) { if (string.IsNullOrEmpty((kind == PostKind.Link ? post.Url : post.SelfText))) throw new Exception("No link or self text added to the new post"); if (string.IsNullOrEmpty(post.SubReddit)) throw new Exception("No subreddit set"); if (string.IsNullOrEmpty(post.Title)) throw new Exception("No title provided"); var request = new Request { Url = "http://www.reddit.com/api/submit", Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&kind=" + (kind == PostKind.Link ? "link" : "self") + "&url=" + (kind == PostKind.Link ? post.Url : post.SelfText) + "&sr=" + post.SubReddit + "&title=" + post.Title + "&r=" + post.SubReddit + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); // Capcha // o["jquery"][10][3].ToString() // Error Message // o["jquery"][12][3].ToString() }
public static UserListing GetModerators(Session session, string sub) { // var request = new Request { Method = "GET", Cookie = session.Cookie, Url = "http://www.reddit.com/r/" + sub + "/about/moderators.json" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); return UserListing.FromJson(o); }
public static LogListing GetModerationLog(Session session, string sub) { var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/log/", Method = "GET", Cookie = session.Cookie }; var html = string.Empty; if (request.Execute(out html) != System.Net.HttpStatusCode.OK) throw new RedditException(html); // build JSON? return LogListing.FromHtml(html); }
public static PostListing GetListing(Session session, string sub, SubSortBy sort, string after, string before) { var url = "http://www.reddit.com/r/" + sub + "/"; switch (sort) { case SubSortBy.Hot: url += ".json?limit=" + Limit; break; case SubSortBy.New: url += "new/.json?sort=new&limit=" + Limit; break; case SubSortBy.Rising: url += "new/.json?sort=rising&limit=" + Limit; break; case SubSortBy.TopAllTime: url += "top/.json?sort=top&t=all&limit=" + Limit; break; case SubSortBy.TopYear: url += "top/.json?sort=top&t=year&limit=" + Limit; break; case SubSortBy.TopMonth: url += "top/.json?sort=top&t=month&limit=" + Limit; break; case SubSortBy.TopWeek: url += "top/.json?sort=top&t=week&limit=" + Limit; break; case SubSortBy.TopToday: url += "top/.json?sort=top&t=day&limit=" + Limit; break; case SubSortBy.TopHour: url += "top/.json?sort=top&t=hour&limit=" + Limit; break; case SubSortBy.ControversalAllTime: url += "controversial/.json?sort=controversial&t=all&limit=" + Limit; break; case SubSortBy.ControversalYear: url += "controversial/.json?sort=controversial&t=year&limit=" + Limit; break; case SubSortBy.ControversalMonth: url += "controversial/.json?sort=controversial&t=month&limit=" + Limit; break; case SubSortBy.ControversalWeek: url += "controversial/.json?sort=controversial&t=week&limit=" + Limit; break; case SubSortBy.ControversalToday: url += "controversial/.json?sort=controversial&t=day&limit=" + Limit; break; case SubSortBy.ControversalHour: url += "controversial/.json?sort=controversial&t=hour&limit=" + Limit; break; } if (!string.IsNullOrEmpty(after)) url += "&after=" + after; if (!string.IsNullOrEmpty(before)) url += "&before=" + before; var request = new Request { Method = "GET", Cookie = session.Cookie, Url = url }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); return PostListing.FromJson(o); }
public static Sub Get(Session session, string sub) { // var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return Sub.FromJson(o["data"]); }
public static void Create(Session session, Sub sub) { var subreddit_type = "public"; var language = "en"; var content_options = "any"; var over_18 = false; var default_set = true; var show_media = false; var domain = ""; var request = new Request { Url = "", Method = "POST", Cookie = session.Cookie, Content = "name=" + sub.Name + "&title=" + sub.Title + "&description=" + sub.Description + "&lang=" + language + "&type=" + subreddit_type + "&link_type=" + content_options + "&over_18=" + (over_18 ? "on" : "off") + "&allow_top=" + (default_set ? "on" : "off") + "&show_media=" + (show_media ? "on" : "off") + "&domain=" + domain + "&uh=" + session.ModHash + "&id=#sr-form&api_type=json" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); }
public static PostListing Get(Session session, string id) { // check this is a link (in the correct format) if (!id.StartsWith("t3_")) throw new RedditException("ID is not of a link/post"); // build a request var request = new Request { Url = "http://www.reddit.com/by_id/" + id + ".json", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); return JsonConvert.DeserializeObject<PostListing>(json); }
public static PostListing GetRelated(Session session, string id) { // Make sure we process the request with the type removed, so // we just pass the base-36 ID id = id.Replace("t3_", string.Empty); // build a request var request = new Request { Url = "http://www.reddit.com/related/" + id + ".json", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); return JsonConvert.DeserializeObject<PostListing>(json); }
public static PostListing GetListing(Session session, string sub, SubSortBy sort, string after, string before) { var url = "http://www.reddit.com/r/" + sub + "/"; switch (sort) { case SubSortBy.Hot: url += ".json?limit=" + Limit; break; case SubSortBy.New: url += "new/.json?sort=new&limit=" + Limit; break; case SubSortBy.Rising: url += "new/.json?sort=rising&limit=" + Limit; break; case SubSortBy.TopAllTime: url += "top/.json?sort=top&t=all&limit=" + Limit; break; case SubSortBy.TopYear: url += "top/.json?sort=top&t=year&limit=" + Limit; break; case SubSortBy.TopMonth: url += "top/.json?sort=top&t=month&limit=" + Limit; break; case SubSortBy.TopWeek: url += "top/.json?sort=top&t=week&limit=" + Limit; break; case SubSortBy.TopToday: url += "top/.json?sort=top&t=day&limit=" + Limit; break; case SubSortBy.TopHour: url += "top/.json?sort=top&t=hour&limit=" + Limit; break; case SubSortBy.ControversalAllTime: url += "controversial/.json?sort=controversial&t=all&limit=" + Limit; break; case SubSortBy.ControversalYear: url += "controversial/.json?sort=controversial&t=year&limit=" + Limit; break; case SubSortBy.ControversalMonth: url += "controversial/.json?sort=controversial&t=month&limit=" + Limit; break; case SubSortBy.ControversalWeek: url += "controversial/.json?sort=controversial&t=week&limit=" + Limit; break; case SubSortBy.ControversalToday: url += "controversial/.json?sort=controversial&t=day&limit=" + Limit; break; case SubSortBy.ControversalHour: url += "controversial/.json?sort=controversial&t=hour&limit=" + Limit; break; } if (!string.IsNullOrEmpty(after)) { url += "&after=" + after; } if (!string.IsNullOrEmpty(before)) { url += "&before=" + before; } var request = new Request { Method = "GET", Cookie = session.Cookie, Url = url }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } var o = JObject.Parse(json); return(PostListing.FromJson(o)); }
public static void UnNsfw(Session session, string sub, string id, string modhash) { var request = new Request { Url = "http://www.reddit.com/api/unmarknsfw", Method = "POST", Cookie = session.Cookie, Content = "id=" + id + "&executed=unmarked" + "&r=" + sub + "&uh=" + modhash + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="id">The FULLNAME (see glossary on the API page) of the thing or comment you are commenting on.</param> /// <param name="markdown">The markdown content of the comment you are posting.</param> public static Comment Submit(Session session, string id, string markdown) { // http://www.reddit.com/api/comment var request = new Request { Url = "http://www.reddit.com/api/comment", Cookie = session.Cookie, Method = "POST", Content = "uh=" + session.ModHash + "&thing_id=" + id + "&text=" + markdown }; // jquery[18][3][0][0] var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); var comment = o["jquery"][18][3][0][0]["data"].ToString(); // really we just want to get the ID of the comment here return new Comment(); }
public static User Get(Session session, string username) { // build a request var request = new Request { Url = "http://www.reddit.com/user/" + username + "/about.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); return User.FromJson(o["data"]); }
private static void Vote(Session session, string id, string modhash, int direction) { var request = new Request { Url = "http://www.reddit.com/api/vote", Method = "POST", Content = "uh=" + modhash + "&id=" + id + "&dir=" + direction.ToString(), Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); if (json != "{}") throw new Exception(json); }
public static PostListing getUnmoderated(Session session, string sub) { // var request = new Request { Url = "http://www.reddit.com/r/" + sub + "/about/unmoderated/.json?limit=100", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); return PostListing.FromJson(o); }
public static Session Login(string username, string password) { var request = new Request { Url = "https://ssl.reddit.com/api/login/" + username, Method = "POST", Content = "api_type=json&user="******"&passwd=" + HttpUtility.UrlEncode(password) }; // get the modhash var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new Exception(json); } /* // Expected Json { "json": { "errors": [], "data": { "modhash": "u4abc21302316ad40013feb16cfccb0b11b786596e5194de14", "cookie": "1234567,2011-07-12T14:53:59,0200b365fa02c61f9532ab244b214bd481941492" } } } */ // Failure // {"json": {"errors": [["WRONG_PASSWORD", "invalid password"]]}} var o = JObject.Parse(json); if (o["json"]["errors"].HasValues) throw new RedditException(json); // Create a session which can be used on further requests return new Session { Username = username, Password = password, ModHash = o["json"]["data"]["modhash"].ToString(), // reddit_session=8010059%2C2011-01-26T06%3A16%3A49%2C8e77c04fb74713f923c6ed7f44ae8374300db499; Cookie = "reddit_session=" + System.Web.HttpUtility.UrlEncode(o["json"]["data"]["cookie"].ToString()) }; }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="sub"></param> /// <param name="after"></param> /// <param name="before"></param> /// <returns></returns> public static SubListing List(Session session, string after = "", string before = "") { var request = new Request { Url = "http://www.reddit.com/reddits/.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return SubListing.FromJson(o); }
public static void BanUser(Session session, string sub, string sub_id, string username, string modhash) { var request = new Request { Url = "http://www.reddit.com/api/friend", Method = "POST", Cookie = session.Cookie, Content = "action=add" + "&container=" + sub_id + "&type=banned" + "&name=" + username + "&id=#banned" + "&r=" + sub + "&uh=" + modhash + "&renderstyle=html" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); }
// List submissions from friends /// http://www.reddit.com/r/friends/.json /// public static UserListing List(Session session) { var request = new Request { Url = "https://ssl.reddit.com/prefs/friends.json", Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new Exception(json); var o = JObject.Parse(json); var list = UserListing.FromJson(o); return list; }
public static void GetSendForm(Session session, out string iden, out string captcha) { var request = new Request { Cookie = session.Cookie, Method = "GET", Url = "http://www.reddit.com/message/compose" }; var html = string.Empty; if (request.Execute(out html) != System.Net.HttpStatusCode.OK) throw new Exception(html); // use NSOUP to get the iden string and captcha URL /// var client = NSoup.NSoupClient.Parse(html); //iden = client.Select("#compose-message input[name=iden]").Val(); //captcha = client.Select("#compose-message img.capimage").Attr("src"); iden = null; captcha = null; }
public static void Logout(Session session) { var request = new Request { Url = "http://www.reddit.com/logout?uh=" + session.ModHash, Method = "POST", Cookie = session.Cookie, Content = "uh=" + session.ModHash + "&top=off" }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); }
public static MessageListing GetMessages(Session session, bool unread) { string url = "http://www.reddit.com/message/"; if (unread) { url += "unread/.json"; } else { url += "inbox/.json"; } var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); var o = JObject.Parse(json); return MessageListing.FromJson(o); }