Skip to content

Instantly share code, notes, and snippets.

@howyi
Created July 11, 2015 07:39
Show Gist options
  • Save howyi/e10fa96ebdcf25846ba9 to your computer and use it in GitHub Desktop.
Save howyi/e10fa96ebdcf25846ba9 to your computer and use it in GitHub Desktop.
Search tweets(userstream) with tweepy
# Python 3.4.3
# tweepy 3.3.0
# coding: utf-8
import sys
import webbrowser
import tweepy
from tweepy.streaming import StreamListener
from tweepy import Stream
import shelve
consumer_key = '*****'
consumer_secret = '*****'
keyword = '#aikatsu'
class streamListener(StreamListener): #StreamingAPI
def on_status(self, status):
if not hasattr(status, 'retweeted_status'): #without RT
try:
print (' ' + status.author.name + ':'+ status.text)
except:
print ('error')
def on_error(self, status):
print (status)
if __name__ == "__main__":
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
dic = shelve.open('./key')
if 'access_token' in dic.keys():
access_token = dic['access_token']
access_token_secret = dic['access_token_secret']
else:
webbrowser.open(auth.get_authorization_url())
pin = input('PIN: ').strip()
token = auth.get_access_token(verifier=pin)
print(token[0])
dic['access_token'] = token[0]
dic['access_token_secret'] = token[1]
access_token = token[0]
access_token_secret = token[1]
dic.close()
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth)
l = streamListener()
stream = Stream(auth, l)
stream.filter(track=[keyword])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment