Search code examples
c#twitterconsole-applicationlinq-to-twitter

Getting all latest tweets c# console application


How can i at least get 3200 latest tweets from a public timeline? this is what i have done currently but it only returns me latest 200 tweets.

void GetUserTimeLine(TwitterContext ctx) {

            var statusTweets =
               from tweet in twitterCtx.Status
              where tweet.Type == StatusType.User &&
                    tweet.Count == 3200 &&
                    tweet.ScreenName == "abc" 
             select tweet;

            //PrintTweetsResults(statusTweets);
            foreach (var tweet in statusTweets)
            {
                Console.WriteLine(
                    "(" + tweet.StatusID + ")" +
                    "[" + tweet.User.ID + "]" +
                    tweet.User.Name + ", " +
                    tweet.Text + ", " +
                    tweet.CreatedAt);
            }

        // DEFINE FILE PATH NAME
            string dwnloadFilePath = @"C:\temp\Tweet.log";

        // CREATE AN EMPTY TEXT FILE
        FileStream fs1 = null;
        if (!File.Exists(dwnloadFilePath))
        {
            using (fs1 = File.Create(dwnloadFilePath)) ;
        }

        // WRITE DATA INTO TEXT FILE
        if (File.Exists(dwnloadFilePath))
        {
            using (StreamWriter sw = new StreamWriter(dwnloadFilePath))
            {

                statusTweets.ToList().ForEach(
            tweet => sw.Write(
            "{3}, Tweet ID: {2}, Tweet: {1}\n",
            tweet.User.Name, tweet.Text, tweet.StatusID, tweet.CreatedAt));

            }
        }
        Console.ReadLine();
    }

Can someone please enlighten me?

Thanks, 10e5x


Solution

  • While you can fetch any of the last 3200 tweets in the time line, you can only fetch them in batches of 200 (see the twitter api for more details.)

    You need to requery once you have processed the first 200 entries specifying the max id value as well as the count, where the max id is the lowest id from the set of tweets you have already processed.