Random Twitter stuff – Get recent tweets of a user

by Pradeep Prabakar

So, recently, we have been working on a project to profile a twitter user. We needed to get the tweets of a particular set of users to test our profiling approach. The best thing about working with twitter is its pretty awesome API that is so comprehensive. I wrote a simple script to extract the recent tweets for a list of users. The code is written in Python and easy to follow.

<pre>
## Retrieve a user's tweets
## Sample usage -- python get_tweets.py user_name_list.txt output.csv

import urllib2
import sys
import csv
import urllib
import simplejson
import traceback

COUNT=100

URL_LOCATION="https://api.twitter.com/1/statuses/user_timeline.json?"
params = {'include_entities':'true',
          'include_rts':'true',
           'screen_name':'',
           'count':COUNT
           }

def check_unicode(text):
    if isinstance(text,unicode):
        text = text.encode("UTF-8","ignore")
        return text
    elif isinstance(text,str):
        text= unicode(text,"UTF-8").encode("UTF-8","ignore")
    return text    

csv_writer = csv.writer(open(sys.argv[2],"wb"))
csv_writer.writerow(["User","tweet"])
user_list = open(sys.argv[1],"rb")
cnt =0
for each_user in user_list:
    print cnt
    params['screen_name']=each_user
    encoded_url = urllib.urlencode(params)
    API_CALL = URL_LOCATION + encoded_url
    print API_CALL
    try:
        data = urllib2.urlopen(API_CALL).read()
        json_data = simplejson.loads(data)
        for each_timeline in json_data:
            csv_writer.writerow([each_user,check_unicode(each_timeline['text'])])            
    except:
        # Some Crap Failed !!!
        print traceback.format_exc()
    cnt+=1

</pre>
The Input file should be a file with user name in each line.

Sample I/P file:

some_user_name_1
some_user_name_2
some_user_name_3

Thanks to the awesome developers at Twitter, This hack was done in less than 10 minutes 🙂

Advertisements