Search code examples
c#twitterlinq-to-twitter

Getting 1000 latest tweets and writing it into a log file


i have been trying to get the latest 1000 tweets. I have read all the documentation on LinqToTwitter. I tried writing my own:

 void GetUserTimeLine(TwitterContext ctx)
     {
        List<String> tweetList = new List<String>();
        String[] breakWords;
        ulong maxID = 0000;
        ulong sinceID = 3396455589; //oldest tweetID
        for (int i = 0; i < 5; i++)
        {
            if (i == 0)
            {
                var statusTweets =
               from tweet in twitterCtx.Status
               where tweet.Type == StatusType.User &&
                     tweet.Count == 200 &&
                     tweet.SinceID == sinceID &&
                     tweet.ScreenName == "nyptweets"
               select tweet;



                tweetList.Add(statusTweets.ToList().ToString());
                breakWords = tweetList[tweetList.Count - 1].Split(' ');
                maxID = Convert.ToUInt64(breakWords[5].ToString());
            }
            else
            {
                var statusTweets =
               from tweet in twitterCtx.Status
               where tweet.Type == StatusType.User &&
                     tweet.Count == 200 &&
                     tweet.SinceID == sinceID &&
                     tweet.MaxID == maxID &&
                     tweet.ScreenName == "nyptweets"
               select tweet;
               tweetList.Add(statusTweets.ToList().ToString());
               breakWords = tweetList[tweetList.Count - 1].Split(' ');
               maxID = Convert.ToUInt64(breakWords[5].ToString());
            }


        }
       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))
            {
                File.WriteAllLines(dwnloadFilePath, tweetList);
             }
          }
       }

Can someone help me with that? The problem is that when I select a single tweet it is not a string therefore when I add it to the List<string> it doesn't work.


Solution

  • Just for the sake of it, you should consider cleaning up your code like so (half the length):

    public class Twitter
    {
        private List<String> tweetList;
        private String[] breakWords;
        private ulong maxID = 0000;
        private ulong sinceID = 3396455589; //oldest tweetID
    
        private void progressTweet(int i)
        {
            var statusTweets =
                   from tweet in twitterCtx.Status
                   where tweet.Type == StatusType.User &&
                         tweet.Count == 200 &&
                         tweet.SinceID == sinceID &&
                         tweet.ScreenName == "nyptweets"
                   select tweet;
    
            if (i == 0)
                tweetList.Add(statusTweets.ToList().ToString());
            else
                tweetList.Add(statusTweets.Where(x => x.MaxID == maxID).ToList().ToString());
    
            breakWords = tweetList[tweetList.Count - 1].Split(' ');
            maxID = Convert.ToUInt64(breakWords[5].ToString());
        }
    
        void GetUserTimeLine(TwitterContext ctx)
        {
            tweetList = new List<String>();
    
            for (int i = 0; i < 5; i++)
                progressTweet(i);
    
            File.WriteAllLines(@"C:\temp\Tweet.log", tweetList);
        }
    }
    

    Noticeable is that you don't use i other than for the if (i == 0) ... else ... which doesn't look quite right to me, other than that another optimization is to not run the LINQ for each iteration but rather select those that you want in advance, so that you can pass on actual tweets to progressTweet.

    As to keep this an actual answer, I don't expect .ToList().ToString() to be right.