Skip to content

Instantly share code, notes, and snippets.

/RedditLib.cs Secret

Created October 28, 2012 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2b812a63663927fa09b6 to your computer and use it in GitHub Desktop.
Save anonymous/2b812a63663927fa09b6 to your computer and use it in GitHub Desktop.
r9kbot part 2
using System;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
namespace robot9k
{
public class RedditLib
{
public bool isLoggedIn {get; private set;}
public string Username {get; private set;}
public string modHash;
public string cookie;
public RedditLib ()
{
Console.WriteLine ("RedditLib by r9kbot initiated!");
}
public string GetRedditURL()
{
return "http://www.reddit.com";
}
public string GetAPIURL()
{
return "http://www.reddit.com/api";
}
public bool Login(string Username, string Password)
{
this.Username = Username;
string s = SendAPIRequest("login" + "/" + Username, "user=" + Username + "&passwd=" + Password + "&api_type=json");
JObject o = JObject.Parse(s);
if (o["json"]["errors"].HasValues)
{
Console.WriteLine ("Failed to log in!");
foreach (var v in o["json"]["errors"])
{
Console.WriteLine ("Error:" + v.ToString());
}
return false;
}
modHash = (string)o["json"]["data"]["modhash"];
cookie = (string)o["json"]["data"]["cookie"];
return true;
}
public string Comment(string Comment, string Thing_ID, TYPE_THING typeThing)
{
if (typeThing != TYPE_THING.COMMENT && typeThing != TYPE_THING.LINK)
{
Console.WriteLine("Cannot comment on this!");
}
string type = ((int)typeThing).ToString();
string s = SendAPIRequest("comment", "parent=t" + type + "_" + Thing_ID + "&text=" + Comment + "&uh=" + modHash);
if (s.Contains("contentHTML"))
{
string[] stringSeparators = new string[] {"\"id\": \"t1_"};
string[] stringSeparators2 = new string[] {"\"}}]"};
string[] arr = s.Split(stringSeparators, StringSplitOptions.None);
string[] arr2 = arr[1].Split(stringSeparators2, StringSplitOptions.None);
return arr2[0];
}
else
{
Console.WriteLine ("Could not comment!");
return "";
}
}
public bool BanUser(string Name, string Note, string Subreddit)
{
SendAPIRequest("friend", "name=" + Name + "&r=" + Subreddit + "&type=banned&note=" + Note + "&uh=" + modHash);
return true;
}
public bool UnBanUser(string Name, string Note, string Subreddit)
{
SendAPIRequest("unfriend", "name=" + Name + "&r=" + Subreddit + "&type=banned&note=" + Note + "&uh=" + modHash);
return true;
}
public bool MODDistinguish(string THING_ID, string how, TYPE_THING typeThing)
{
string type = ((int)typeThing).ToString();
SendAPIRequest("distinguish", "id=t" + type + "_" + THING_ID + "&how=" + how + "&uh=" + modHash);
return true;
}
public string GetJSONComments(string subreddit, int limit = 100)
{
return SendRequest("r/" + subreddit + "/comments/.json", "limit=" + limit);
}
public string SendRequest(string URL, string PostData)
{
WebClient client = new WebClient();
return client.DownloadString(GetRedditURL() + "/" + URL);
}
public string SendAPIRequest(string URL, string PostData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(GetAPIURL() + "/" + URL);
if (!String.IsNullOrEmpty(cookie))
{
CookieContainer cookies = new CookieContainer();
cookies.Add(new Uri(GetRedditURL()), new Cookie("reddit_session", cookie));
myRequest.CookieContainer = cookies;
}
myRequest.Method = "POST";
byte[] dataToPost = Encoding.Default.GetBytes(PostData);
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.Timeout = 5000;
myRequest.KeepAlive = false;
myRequest.ContentLength = dataToPost.Length;
myRequest.UserAgent = "r9kbot for /r/obot9k by hzj";
Stream newStream=myRequest.GetRequestStream();
newStream.Write(dataToPost,0,dataToPost.Length);
newStream.Close();
string result = "";
using (WebResponse response = myRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
result = responseReader.ReadToEnd();
}
return result;
}
public enum TYPE_THING
{
COMMENT = 1,
ACCOUNT = 2,
LINK = 3,
MESSAGE = 4,
SUBREDDIT = 5
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment