public void ResetLastAccessed(List<Objects.Option> options, int defaultPollingInterval)
 {
     if (options.Count(x => x.Active) > 0)
     {
         DateTime currentDate = DateTime.Now.ToUniversalTime();
         DateTime defaultLastCheckedDate = currentDate.AddMilliseconds(-defaultPollingInterval);
         foreach (Objects.Option option in options.Where(x => x.Active))
         {
             switch ((TwitterOptionId)option.OptionId)
             {
                 case TwitterOptionId.TweetCount:
                     option.LastAccessed = currentDate.AddMinutes(-option.Numerics[0]);
                     break;
                 default:
                     option.LastAccessed = defaultLastCheckedDate;
                     break;
             }
         }
     }
 }
Example #2
0
        public List<Objects.Notification> TestForNotifications(List<Objects.Option> options)
        {
            var notifications = new List<Objects.Notification>(options.Count);

            //if (options.Count(x => x.Active) > 0)
            {
                var auth = new LinqToTwitter.SingleUserAuthorizer
                {
                    Credentials = new LinqToTwitter.SingleUserInMemoryCredentials
                    {
                        ConsumerKey = System.Configuration.ConfigurationManager.AppSettings.Get("consumerKey"),
                        ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings.Get("consumerSecret"),
                        //AccessToken = "accessToken",
                        //AccessTokenSecret = "accessTokenSecret"
                        TwitterAccessToken = System.Configuration.ConfigurationManager.AppSettings.Get("accessToken"),
                        TwitterAccessTokenSecret = System.Configuration.ConfigurationManager.AppSettings.Get("accessTokenSecret")
                    }
                };

                auth.Authorize();

                using (var ctx = new LinqToTwitter.TwitterContext(auth))
                {

                    //try
                    //{
                    //    //Account account = accounts.SingleOrDefault();
                    //    Account account = ctx.Account.Single(acct => acct.Type == AccountType.VerifyCredentials && acct.SkipStatus == true);
                    //    //var account = twitterCtx.Account
                    //    //    .Where(t => t.Type == AccountType.VerifyCredentials)
                    //    //    .FirstOrDefault(t => t.SkipStatus == true);
                    //    User user = account.User;
                    //    Status tweet = user.Status ?? new Status();
                    //    Console.WriteLine("User (#" + user.Identifier.ID
                    //                        + "): " + user.Identifier.ScreenName
                    //                        + "\nTweet: " + tweet.Text
                    //                        + "\nTweet ID: " + tweet.StatusID + "\n");

                    //    Console.WriteLine("Account credentials are verified.");
                    //}
                    //catch (System.Net.WebException wex)
                    //{
                    //    Console.WriteLine("Twitter did not recognize the credentials. Response from Twitter: " + wex.Message);
                    //}

                    var testMessageNotification = new FritzNotifier.Objects.Notification(this.NotificationApplication, 0, "Test sender" + " sent message " + "message 1", "New message from " + "Test sender", DateTime.Now);
                    var testSimpleMessage = new FritzNotifier.Objects.Notification(this.NotificationApplication, 0, "simple notification", null, DateTime.Now);

                    notifications.Add(testMessageNotification);
                    notifications.Add(testSimpleMessage);
                    return notifications;

                    try
                    {
                        DateTime currentDate = DateTime.Now;
                        foreach (Objects.Option option in options.Where(x => x.Active))
                        {
                            switch ((TwitterOptionId)option.OptionId)
                            {
                                case TwitterOptionId.TweetCount:
                                    // if enough time has passed since we last accessed this
                                    if ((currentDate - option.LastAccessed).TotalMinutes > option.Numerics[0])
                                    {
                                    int tweetCount =
                                        (from tweet in ctx.Status
                                         where tweet.Type == StatusType.Home &&
                                         tweet.CreatedAt > option.LastAccessed
                                         select tweet).Count();

                                    var newTweetCountNotification = new FritzNotifier.Objects.Notification(this.NotificationApplication, 0, tweetCount.ToString() + " new tweets.", tweetCount.ToString() + " new tweets.", currentDate);
                                        option.LastAccessed = currentDate;
                                        notifications.Add(newTweetCountNotification);
                                    }
                                    break;
                                case TwitterOptionId.DirectMessage:
                                    var directMsgs =
                                        (from dm in ctx.DirectMessage
                                         where dm.Type == DirectMessageType.SentTo &&
                                         dm.CreatedAt > option.LastAccessed
                                         select dm).ToList();
                                    foreach (var directMsg in directMsgs)
                                    {
                                        // handle appropriately
                                        var newDirectMessageNotification = new FritzNotifier.Objects.Notification(this.NotificationApplication, 0, directMsg.Sender.Name + " sent message " + directMsg.Text, "New message from " + directMsg.Sender.Name, currentDate);
                                        notifications.Add(newDirectMessageNotification);
                                        option.LastAccessed = currentDate;
                                    }

                                    break;
                            }
                        }
                    }
                    catch (System.Net.WebException wex)
                    {
                        Console.WriteLine("Twitter did not recognize the credentials. Response from Twitter: " + wex.Message);
                    }
                }
            }

            return notifications;
        }
Example #3
0
        private List<Status> GetAllTweets(ulong sinceId = 0)
        {
            var allTweets = new List<Status>();

            using (var twitter = new TwitterContext(Auth))
            {
                foreach (var u in Users)
                {
                    ulong localSince = sinceId;
                    int lastCount = 199;
                    var oldestId = ulong.MaxValue;
                    while (lastCount > 1)
                    {
                        IQueryable<Status> statusTweets =
                            twitter.Status.Where(tweet => tweet.Type == StatusType.User
                                                          && tweet.ScreenName == u
                                                          && tweet.IncludeMyRetweet == true
                                                          && tweet.ExcludeReplies == false
                                                          && tweet.Count == 199);

                        if (oldestId != ulong.MaxValue && sinceId == 0)
                            statusTweets = statusTweets.Where(t => t.MaxID == oldestId);

                        if (sinceId != 0)
                            statusTweets = statusTweets.Where(t => t.SinceID == localSince);

                        if (statusTweets != null)
                        {
                            var returned = statusTweets.ToList();

                            if (!returned.Any())
                                break;

                            lastCount = returned.Count();
                            localSince = returned.Max(t => ulong.Parse(t.StatusID));
                            oldestId = returned.Min(t => ulong.Parse(t.StatusID));
                            returned.RemoveAt(returned.Count - 1);
                            allTweets.AddRange(returned);
                        }
                        else
                        {
                            lastCount = 0;
                        }
                    }
                }
            }

            return allTweets.Where(t => t.CreatedAt > new DateTime(2015,01,01)).OrderByDescending(s =>s.StatusID).ToList();
        }