public void BypassCacheForRecentResults()
        {
            // Setup the cache
            var cachedResults = new TwitterSearchResultCollection();
            cachedResults.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });
            _localCache.Stub(c => c.PageResults).Return(cachedResults);
            _localCache.Stub(c => c.PageNumber).Return(1);

            // Setup the Twitterizer response
            TwitterResponse<TwitterSearchResultCollection> response = new TwitterResponse<TwitterSearchResultCollection>();
            response.ResponseObject = new TwitterSearchResultCollection();
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 2, Text = "This is just another #test" });
            _searchService
                .Expect(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Is.Anything))
                .Return(response);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            var results = client.Search("#test", 1, 10, true); //newSearch means ignore the cache
            Assert.IsFalse(AreEquivalent(cachedResults, results));
            Assert.IsTrue(AreEquivalent(response.ResponseObject, results));

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }
Beispiel #2
0
        public void Tweets_werden_gemappt() {
            var sut = new Mapper();
            IEnumerable<Tweet> result = null;
            sut.Out_Result += x => result = x;

            var twitterSearchResultCollection = new TwitterSearchResultCollection {
                new TwitterSearchResult {
                    FromUserScreenName = "Paul",
                    ProfileImageLocation = "pic#1",
                    CreatedDate = new DateTime(2003, 2, 1, 12, 05, 00),
                    Source = "<a href=''>web</a>"
                }
            };
            sut.In_Map(twitterSearchResultCollection);

            Assert.That(result.Count(), Is.EqualTo(1));
            var tweet = result.First();
            Assert.That(tweet.Name, Is.EqualTo("@Paul, 01.Feb 12:05 via web"));
        }
Beispiel #3
0
 public void In_Map(TwitterSearchResultCollection twitterSearchResultCollection) {   
     Out_Result(twitterSearchResultCollection.Select(CreateTweet));
 }
        public static CommandResult SearchTweets(decimal[] ids)
        {
            if (!IsInitiated)
            {
                return(CommandResult.NotInitiated);
            }

            foreach (var id in ids)
            {
                if (id == 0)
                {
                    continue;
                }

                var UserToProcess = (new TwitterContext()).Users.FirstOrDefault(u => u.Id == id);

                if (UserToProcess == null)
                {
                    Form.AppendLineToOutput(string.Format("User with id {0} does not exist in the database.", id), Color.Olive);
                    continue;
                }
                //else continue and search for tweets to the user and mentions of the user

                string  screenname = UserToProcess.ScreenName;
                decimal idNumber   = UserToProcess.Id;

                TwitterContext db = null;
                try
                {
                    TwitterResponse <TwitterSearchResultCollection> searchResult;
                    int numTweets       = 0;
                    int duplicatesFound = 0;

                    Form.AppendLineToOutput(string.Format("Attempt to search for tweets to and mentioning {0} in the last 4 months", screenname), Color.Olive);

                    //continue from SearchResult if there is tweet, otherwise use user's latest tweet id
                    //decimal latestTweetId = GetEarliestTweetIdFromSearchResult(id);
                    //if(latestTweetId == 0)
                    //{
                    //    latestTweetId = GetLatestTweetId(id);
                    //}

                    //decimal earliestTweetId = GetEarliestTweetId(id);


                    decimal latestTweetId = 701646604403478528;
                    //701646604403478528 : id of a tweet dated 21 Feb 2016 9:56 p.m.

                    decimal earliestTweetId = 656323760102879232;
                    //656323760102879232 : id of a tweet dated 19 Oct 2015 9:19 p.m.

                    do
                    {
                        searchResult = TwitterSearch.Search(
                            Tokens,
                            string.Format("to%3A{0}%20%40{0}", screenname),
                            new SearchOptions
                        {
                            MaxId      = latestTweetId - 1,
                            SinceId    = earliestTweetId,
                            ResultType = SearchOptionsResultType.Recent,
                            Count      = 100
                        });

                        if (searchResult.Result == RequestResult.Success)
                        {
                            TwitterSearchResultCollection tweets = searchResult.ResponseObject;

                            if (tweets == null || tweets.Count == 0)
                            {
                                break;
                            }
                            else
                            {
                                //refresh context
                                db = new TwitterContext();
                                db.Configuration.AutoDetectChangesEnabled = false;

                                decimal earliestIdFromSearch = latestTweetId;

                                foreach (TwitterStatus tweet in tweets)
                                {
                                    SearchResult sr = new SearchResult();

                                    sr.ForAirlineId         = idNumber;
                                    sr.ForAirlineScreenname = screenname;

                                    sr.TweetId          = tweet.Id;
                                    sr.CreatedDate      = tweet.CreatedDate;
                                    sr.PosterUserId     = tweet.User.Id;
                                    sr.PosterScreenname = tweet.User.ScreenName;
                                    sr.TweetText        = tweet.Text;

                                    sr.IsTweetToAirline   = (tweet.InReplyToUserId == idNumber);
                                    sr.IsRetweetOfAirline = (tweet.RetweetedStatus != null && tweet.RetweetedStatus.User.Id == idNumber);
                                    sr.IsMentionAirline   = (!sr.IsTweetToAirline && !sr.IsRetweetOfAirline);

                                    if (tweet.Id < earliestIdFromSearch)
                                    {
                                        earliestIdFromSearch = tweet.Id;
                                    }

                                    numTweets++;

                                    db.SearchResults.Add(sr);
                                }

                                try
                                {
                                    db.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    Form.AppendLineToOutput(ex.Message, Color.Olive);
                                    Form.AppendLineToOutput(ex.StackTrace, Color.Olive);
                                    return(CommandResult.Failure);
                                }

                                latestTweetId = earliestIdFromSearch;
                            }

                            Form.AppendLineToOutput(string.Format("{0} tweets processed for {1} (id:{2}). {3} duplicate {4} not saved.", numTweets, screenname, idNumber, duplicatesFound, duplicatesFound == 1 ? "tweet" : "tweets"), Color.Olive);
                        }
                        else if (searchResult.Result == RequestResult.RateLimited)
                        {
                            WaitForRateLimitReset(searchResult);

                            Form.AppendLineToOutput("Resuming search tweets command", Color.Olive);
                            continue;
                        }
                        else
                        {
                            HandleTwitterizerError <TwitterSearchResultCollection>(searchResult);

                            //log that this user should be retried later
                            File.AppendAllText(@".\users-to-retry.txt", idNumber.ToString() + Environment.NewLine);

                            break; //give up with current user for now
                        }
                    } while (searchResult.ResponseObject != null && searchResult.ResponseObject.Count > 0);
                }
                catch (Exception e)
                {
                    Exception current = e;
                    Form.AppendLineToOutput(string.Format("Unexpected exception : {0}", e.Message), Color.Olive);

                    while (current.InnerException != null)
                    {
                        Form.AppendLineToOutput(string.Format("Inner exception : {0}", current.InnerException.Message), Color.Olive);
                        current = current.InnerException;
                    }

                    //log that this user should be retried later
                    File.AppendAllText(@".\users-to-retry.txt", idNumber.ToString() + Environment.NewLine);

                    continue; //give up with current user
                }
                finally
                {
                    if (db != null)
                    {
                        db.Dispose();
                    }
                }
            }

            return(CommandResult.Success);
        }
        public void RetrieveTweetsFromCache()
        {
            var cachedResults = new TwitterSearchResultCollection();
            cachedResults.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });

            _searchService.AssertWasNotCalled(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Is.Anything));

            _localCache.Expect(c => c.PageResults).Return(cachedResults);
            _localCache.Expect(c => c.PageNumber).Return(1);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            var results = client.Search("#test", 1, 10, false);
            Assert.IsTrue(AreEquivalent(cachedResults, results));

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }