예제 #1
0
        public void Submit(RedditSubmission rs)
        {
            //submit if not already submitted
            if (!LastSubmitted.Any(x => x.Equals(rs)))
            {
                //need to try storing this instead of doing it on every submit. might need to do it anyway though.
                var username = rs.RedditRSSBotData.RedditUser.Username;
                var password = rs.RedditRSSBotData.RedditUser.Password;
                var subreddit = rs.RedditRSSBotData.Subreddit;
                var loginData = Login(username, password);
                if (!string.IsNullOrEmpty(loginData.modhash))
                {
                    var parameters = "";
                    parameters += "kind=link";
                    parameters += "&url=" + HttpUtility.UrlEncode(rs.Url);
                    parameters += "&title=" + HttpUtility.UrlEncode(rs.Title);
                    parameters += "&sr=" + HttpUtility.UrlEncode(subreddit);
                    parameters += "&r=" + HttpUtility.UrlEncode(subreddit);
                    parameters += "&uh=" + HttpUtility.UrlEncode(loginData.modhash);

                    var cookies = new CookieContainer();
                    var cookie = new Cookie("reddit_session", HttpUtility.UrlEncode(loginData.cookie), "/api/submit", "reddit.com");
                    cookie.Secure = false;
                    cookie.Expires = DateTime.MinValue;
                    cookies.Add(cookie);

                    var tryAgainMessageReceieved = true;
                    string response = null;
                    while (tryAgainMessageReceieved)
                    {
                        response = HttpPost(REDDIT_URI, parameters, cookies);
                        if (response.Contains("try again in"))
                        {
                            tryAgainMessageReceieved = true;
                            //this line yet untested
                            //var tryAgainMinutes = int.Parse(response.Substring(response.IndexOf("try again in "), 14));
                            var tryAgainMinutes = 3;
                            Thread.Sleep(tryAgainMinutes * 60 * 1000);
                            OnSendMessage("Trying to submit too much, trying again in " + tryAgainMinutes + " minutes");
                        }
                        else if (response.Contains("that link has already been submitted"))
                        {
                            tryAgainMessageReceieved = false;
                            OnSendMessage("This link has already been submitted: /r/" + subreddit + " Title: " + rs.Title + " url: " + rs.Url);
                        }
                        else
                        {
                            tryAgainMessageReceieved = false;
                            OnSendMessage("Post submitted: /r/" + subreddit + " Title: " + rs.Title + " url: " + rs.Url);
                        }
                    }
                    LastSubmitted.Enqueue(rs);
                    if (LastSubmitted.Count > SubmissionsToStore)
                        LastSubmitted.Dequeue();
                }
                else
                {
                    OnSendMessage("Login failed");
                }
            }
            else
            {
                OnSendMessage("Link already submitted.");
            }
        }
예제 #2
0
 public List<RedditSubmission> Read(int readCount)
 {
     var list = new List<RedditSubmission>();
     try
     {
         SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(RedditRSSBotData.FeedUrl));
         var items = feed.Items.OrderByDescending(x => x.PublishDate).Take(readCount);
         foreach (var item in items)
         {
             var rs = new RedditSubmission();
             rs.Title = item.Title.Text;
             var link = item.Links.FirstOrDefault();
             rs.Url = link != null ? link.Uri.ToString() : null;
             rs.RedditRSSBotData = RedditRSSBotData;
             rs.TimeSubmitted = DateTime.Now;
             list.Add(rs);
         }
     }
     catch (Exception)
     {
         OnSendMessage("Error reading feed: " + RedditRSSBotData.FeedUrl);
     }
     return list;
 }