elleryq
6/21/2013 - 8:56 AM

Using python_twitter to get home timeline.

Using python_twitter to get home timeline.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from twitter import oauth_dance
from twitter import read_token_file
from twitter import OAuth
from twitter import Twitter

# You need to create application in dev.twitter.com.
# Then you'll get the CONSUMER_KEY and CONSUMER_SECRET
CONSUMER_KEY = ''
CONSUMER_SECRET = ''


def main(arg):
    MY_TWITTER_CREDS = os.path.expanduser('~/.config/my_app_credentials')
    if not os.path.exists(MY_TWITTER_CREDS):
        oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
                    MY_TWITTER_CREDS)

    oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)

    twitter = Twitter(domain='api.twitter.com', api_version='1.1', auth=OAuth(
        oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))

    # Now work with Twitter
    results = twitter.statuses.home_timeline()
    for result in results:
        print("{0}:{1}".format(
            result['user']['screen_name'].encode(
                'utf-8'), result['text'].encode('utf-8')))


if __name__ == "__main__":
    main(sys.argv[1:])