4

I wish to return ALL the Tweets I have ever posted on my timeline.

I am using the Linq To Twitter library as so -

var statusTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.User
                      && tweet.UserID == MyUserID
                      && tweet.Count == 200
                select tweet;

                statusTweets.ToList().ForEach(
                    tweet => Console.WriteLine(
                    "Name: {0}, Tweet: {1}\n",
                    tweet.User.Name, tweet.Text));

This works fine and brings back the first 200. However the first 200 seems to be the maximum I can retrieve, as there a way to bring back say 1000? There does not seem to be a next cursor movement option like there is in other parts of this library to move onto next pages etc.

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

1 Answers1

4

You would use a combination of Count, MaxID, and SinceID to page through tweets. This sounds strange, but there's a very good reason for this approach, given that the tweet stream is continually updating. I've written a blog post, Working with Timelines with LINQ to Twitter, that points to the Twitter documentation and describes how LINQ to Twitter does this.

// last tweet processed on previous query set
ulong sinceID = 210024053698867204;

ulong maxID;
const int Count = 10;
var statusList = new List<status>();

var userStatusResponse =
    (from tweet in twitterCtx.Status
     where tweet.Type == StatusType.User &&
       tweet.ScreenName == "JoeMayo" &&
       tweet.SinceID == sinceID &&
       tweet.Count == Count
     select tweet)
    .ToList();

statusList.AddRange(userStatusResponse);

// first tweet processed on current query
maxID = userStatusResponse.Min(
    status => ulong.Parse(status.StatusID)) - 1;

do
{
    // now add sinceID and maxID
    userStatusResponse =
        (from tweet in twitterCtx.Status
         where tweet.Type == StatusType.User &&
               tweet.ScreenName == "JoeMayo" &&
               tweet.Count == Count &&
               tweet.SinceID == sinceID &&
               tweet.MaxID == maxID
         select tweet)
        .ToList();

    if (userStatusResponse.Count > 0)
    {
        // first tweet processed on current query
        maxID = userStatusResponse.Min(
            status => ulong.Parse(status.StatusID)) - 1;

        statusList.AddRange(userStatusResponse); 
    }
}
while (userStatusResponse.Count != 0 && statusList.Count < 30);
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
  • Ok I don't have time to implement this tonight however will try tomorrow, my first question that stands out with your solution is, what if I dont have a sinceID? IE I wish to start from the begging? – Ebikeneser Sep 05 '13 at 16:03
  • There was never a Tweet 0, so you could start with 1 or larger. It's unlikely that the Twitter API will give you tweets that old because, IIRC, they only go back a certain amount of time. – Joe Mayo Sep 05 '13 at 16:18
  • This worked, thanks, this seems like a relatively slow process however, to retrieve the maximum 3200 tweets was taking quite a while, is there a more efficient method/library you would suggest? – Ebikeneser Sep 09 '13 at 09:40
  • Make sure your Count is set to the max value, 200. Also, you can reduce the size of the payload by turning off options like IncludeReteweets, TrimUser, and ExcludeReplies. For historical retrieval, this is the way to go. However, once you have older tweets, you can use User streams or Site Streams to pull in tweets in real-time. – Joe Mayo Sep 09 '13 at 16:30
  • Good answer Joe Mayo. You still aren't getting that massage chair though, and I'm not paying for your fur coat either. – EkoostikMartin Sep 17 '13 at 15:13