How do I pull a user's 201st-400th Status then 401-600, etc.??? Can I?

May 17, 2012 at 2:33 PM

I have copied code from one of the demo programs and successfully pulled the first 200 Tweets, but for the life of me, I cannot figure out how to keep "paging" through the Statuses.

I have found posts on retrieving "statuses in bulk" but nothing that seems to fit what I am attempting.  Below is a code snippet: 

 

void UserStatusQuery(StreamWriter myLog)
        {
            const string strAlcoa = "alcoa";
            string nextCursor = "-1";

            try
            {
                    var statusTweets =
                     from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.User
                           && tweet.ScreenName == "xyz321"
                           && tweet.Count == 200
                           && tweet.ExcludeReplies == true
                     select tweet;

                    PrintTweetsResults(statusTweets, myLog);
                
            }
            catch (Exception ex)
            {
                // do nothing right now...
                string msg = ex.Message;
            }
        }

 

Above is the code I modified from the demo program that gets the first 200.  One of the things I've tried is to put in code below the call to "PrintTweetsResults" to get the lowest tweet.StatusID and then execute code similar to:

statusTweets =
             from tweet in twitterCtx.Status
           where tweet.Type == StatusType.User
                    && tweet.ScreenName == "xyz321"
                    && tweet.Count == 200
                    && tweet.MaxID == lowestStatusID
                    && tweet.ExcludeReplies == true
           select tweet;

 

"&& tweet.MaxID == 

Coordinator
May 20, 2012 at 11:04 PM

Hi,

Here's the proper way to page through results:

        /// <summary>
        /// shows how to query user status
        /// </summary>
        /// <param name="twitterCtx">TwitterContext</param>
        private static void UserStatusQueryDemo(TwitterContext twitterCtx)
        {
            int maxStatuses = 30;
            int lastStatusCount = 0;
            ulong sinceID = 182583822993465346; // last tweet processed on previous query
            ulong maxID;
            int count = 10;
            var statusList = new List<Status>();

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

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

            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();

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

                lastStatusCount = userStatusResponse.Count;
            }
            while (lastStatusCount != 0 && statusList.Count < maxStatuses);

            for (int i = 0; i < statusList.Count; i++)
            {
                Status status = statusList[i];

                Console.WriteLine("{0, 4}. [{1}] User: {2}\nStatus: {3}",
                    i + 1, status.StatusID, status.User.Name, status.Text);
            }
        }

The following documentation explains how paging through timelines works:

https://dev.twitter.com/docs/working-with-timelines

Joe

 

Nov 12, 2012 at 1:15 AM

Hi, where can I get the sinceID from?

Coordinator
Nov 12, 2012 at 2:15 AM

For the very first query, you can initialize it to the lowest possible StatusID that you need. After a query, set it to the highest numbered StatusID that the query returned and use that on the next query.

@JoeMayo

Nov 14, 2012 at 1:22 AM

Hi Joe,

Your work is really impressive. i have also read http://geekswithblogs.net/WinAZ/Default.aspx. However i tried your solutions but met with some difficulties. I am trying to develop a console application to retrieve all the tweets from the screen_name "abc" (hopefully from one-time execution of my prog). What i know now is that abc has a total of estimated 1900 tweets.(max of latest 3200 is enough for me) I would like to retrieve all of this 1900 tweets and write them to notepad. I do not know the id of the oldest post. Could you guide me to achieve what i want? This what i have tried but it always return me either 301 to 400th post or return me "Sequence contains no elements" exception. The sinceID i put is the 400th tweets id.

 void UserStatusQueryDemo(TwitterContext twitterCtx)
        {
            int maxStatuses = 3200;
            int lastStatusCount = 0;
            ulong sinceID = 224798808125607937; // last tweet processed on previous query
            ulong maxID;
            int count = 10;
            var statusList = new List();

            // only count
            var userStatusResponse =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.User &&
                       tweet.ScreenName == "abc" &&
                       tweet.Count == count
                 select tweet)
                .ToList();

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

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

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

                lastStatusCount = userStatusResponse.Count;
            }
            while (lastStatusCount != 0 && statusList.Count < maxStatuses);

            for (int i = 0; i < statusList.Count; i++)
            {
                Status status = statusList[i];

                Console.WriteLine("{0, 4}. [{1}] User: {2}\nStatus: {3}",
                    i + 1, status.StatusID, status.User.Name, status.Text);
            }
        

what should i fill in for these variables? int maxStatuses ,int lastStatusCount , ulong sinceID , int count

I would appreciate if you can help me and explain these 4 variables to me.

Thanks, 

10e5x