elleryq
6/21/2013 - 9:29 AM

Using python_twitter to get trend.

Using python_twitter to get trend.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""My app"""
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.trends.place(_id='1')
    for result in results:
        for trend in result['trends']:
            print(trend['name'].encode('utf-8'))


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