Wednesday, April 15, 2015

CODE!  Finally, I know.

So.  I started this blog ("blog") with the intention of using it to post and discuss my various coding projects.  But!  As school has the tendency to do, I have much to busy with school projects to do much, or any, of my own coding.  Maybe I'll try merging the two...

public static IEnumerable<ITweet> TEST_GetUserTimeLine(string user)
{
     IEnumerable<ITweet> userTimelineTweets = null;
     TwitterCredentials.ExecuteOperationWithCredentials(Instance.credentials, () =>
     {
          int page = 0;
          const int maxId = 3200;
          string query = String.Format("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&count={1}&page={2}", user, maxId, page);
          string result = TwitterAccessor.ExecuteJsonGETQuery(query);
          IJsonObjectConverter searchQueryHelper = TweetinviContainer.Resolve<IJsonObjectConverter>();
          List<ITweetDTO> tweetDTOs = searchQueryHelper.DeserializeObject<List<ITweetDTO>>(result);
 
          if (tweetDTOs != null && tweetDTOs.Count() != 0)
          {
              userTimelineTweets = Tweet.TweetFactory.GenerateTweetsFromDTO(tweetDTOs);
              for (int y = 1; y < 16; y++)
              {
                   query = String.Format("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&count={1}&page={2}", user, maxId, y);
                   result = TwitterAccessor.ExecuteJsonGETQuery(query);
                   tweetDTOs = searchQueryHelper.DeserializeObject<List<ITweetDTO>>(result);
                   IEnumerable<ITweet> tweeters = Tweet.TweetFactory.GenerateTweetsFromDTO(tweetDTOs);
                   if (tweeters != null && tweeters.Count() != 0)
                   {
                        userTimelineTweets = userTimelineTweets.Union(tweeters);
                   }
                   else
                   {
                        break;
                   }
               }
          }
     });
     return userTimelineTweets;
}

This semester I am taking a class on mining social media.  We have a lab almost every week, two data set problems that span a month each and then a large final project.  This chunk of code is from that final project.

The final project, at least MY final project, involves mining Twitter for user information.  Part of that is getting as much of a given users tweet history (their timeline) as possible.  Unfortunately that amounts to a max of 3200 tweets which is a limitation of the Twitter API.  The Twitter lib I'm using is Tweetinvi and it has a method called GetUserTimeLine(x) in which x is the amount of tweets you want.  Unfortunately it doesn't seem to work as I get a max of 40 tweets regardless of how many the user has on their timeline.

Now this isn't exactly MY code, and I wish I could remember where I got it to give credit where credit is due, but I did fix it up a little.  With each ExecuteJsonGetQuery call you can get a max of 200 tweets meaning it takes 16 calls to get the full 3200 tweet max.  Originally, the way the code was written it would query the Twitter API 16 times whether you had downloaded all of the users timeline or not.  Since Twitter has a 300 calls per 15 minutes rate limit for downloading timelines, 16 per user means you're wasting calls if it doesn't require that many to get all their tweets.

Kind of a side note:  For my project I've downloaded user information for 20,000+ Twitter users.  Not knowing at the time (and not being smart enough to check) that GetUsertimeLine(x) didn't work properly.  So now I'm having to go back through all 20,000+ users and download their timelines again.

Wish me luck...

No comments:

Post a Comment