Exemplo n.º 1
0
        public List <Tweet> GetTweetsForExistingUser(long userId, long latestTweetId)
        {
            UserTimelineParameters timelineParameters = new UserTimelineParameters
            {
                MaximumNumberOfTweetsToRetrieve = 200,
                IncludeRTS = false,
                SinceId    = latestTweetId
            };

            Task <IEnumerable <ITweet> > userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() =>
            {
                return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters));
            });

            RequestsRemaining--;

            var twitterResponse = userTimlineAsync.Result;
            var latestException = Tweetinvi.ExceptionHandler.GetLastException();

            if (latestException != null)
            {
                throw new InvalidOperationException
                          ($"{latestException.StatusCode} : {latestException.TwitterDescription}");
            }
            else if (twitterResponse == null || !twitterResponse.Any())
            {
                Console.WriteLine($"No new tweets were returned for {userId} since {latestTweetId}.");
                return(null);
            }
            else
            {
                List <Tweet> tweets = new List <Tweet>();

                foreach (var tweet in twitterResponse)
                {
                    var oembedHtml = OEmbedTweet.GetHtml(tweet.Url);

                    RequestsRemaining--;

                    tweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName,
                                         tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id,
                                         tweet.FullText, tweet.Url, oembedHtml));
                }

                return(tweets);
            }
        }
Exemplo n.º 2
0
        public List <Tweet> GetMoreTweets(long userId, long maxTweetId, int tweetsToRetrieve)
        {
            UserTimelineParameters timelineParameters = new UserTimelineParameters
            {
                MaximumNumberOfTweetsToRetrieve = 200,
                IncludeRTS = false,
                MaxId      = maxTweetId
            };

            Task <IEnumerable <ITweet> > userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() =>
            {
                return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters));
            });

            RequestsRemaining--;

            var lastTweets      = userTimlineAsync.Result;
            var latestException = Tweetinvi.ExceptionHandler.GetLastException();

            if (latestException != null)
            {
                throw new InvalidOperationException
                          ($"{latestException.StatusCode} : {latestException.TwitterDescription}");
            }
            else if (lastTweets == null || !lastTweets.Any())
            {
                Console.WriteLine($"No tweets were returned for {userId}.");
                return(null);
            }
            else
            {
                List <Tweet> allTweets = new List <Tweet>();

                foreach (var tweet in lastTweets)
                {
                    var oembedHtml = OEmbedTweet.GetHtml(tweet.Url);

                    RequestsRemaining--;

                    allTweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName,
                                            tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id,
                                            tweet.FullText, tweet.Url, oembedHtml));
                }

                while (lastTweets.ToArray().Length > 0 && allTweets.Count < tweetsToRetrieve)
                {
                    if (RequestsRemaining < 10)
                    {
                        Console.WriteLine("Less than 10 requests remaining in window. 15 minute cooldown starts now.");
                        Thread.Sleep(90 * 1000 * 10); // Wait 900,000 milliseconds, i.e. 15 minutes

                        var newRateLimit = Tweetinvi.RateLimit.GetQueryRateLimit("https://api.twitter.com/1.1/statuses/user_timeline.json");
                        RequestsRemaining = newRateLimit.Remaining;
                        ResetDateTime     = newRateLimit.ResetDateTime;
                    }

                    // Max ID set to lowest tweet ID in our collection (i.e. the oldest tweet) minus 1.
                    // This number serves as a point of reference for requesting tweets older than those
                    // Twitter previously returned.

                    long maxTweetID = allTweets.Min(x => x.TweetId) - 1;

                    timelineParameters = new UserTimelineParameters
                    {
                        MaximumNumberOfTweetsToRetrieve = 200,
                        IncludeRTS = false,
                        MaxId      = maxTweetID
                    };

                    userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() =>
                    {
                        return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters));
                    });

                    RequestsRemaining--;

                    lastTweets = userTimlineAsync.Result;

                    foreach (var tweet in lastTweets)
                    {
                        var oembedHtml = OEmbedTweet.GetHtml(tweet.Url);

                        RequestsRemaining--;

                        allTweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName,
                                                tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id,
                                                tweet.FullText, tweet.Url, oembedHtml));
                    }
                }

                return(allTweets);
            }
        }