Skip to content

Instantly share code, notes, and snippets.

@elleryq
Last active December 18, 2015 19:00
Show Gist options
  • Save elleryq/5830102 to your computer and use it in GitHub Desktop.
Save elleryq/5830102 to your computer and use it in GitHub Desktop.
Use python-twitter to search specified keyword.
#!/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(auth=OAuth(
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
# Now work with Twitter
results = twitter.search.tweets(q='AKB48')
for result in results['statuses']:
print("{0}:{1}".format(
result['user']['screen_name'].encode(
'utf-8'), result['text'].encode('utf-8')))
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment