def main(): #listen = SListener(api, 'myprefix') #stream = tweepy.Stream(auth, listen) auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) stream = Stream(auth, TweetListener()) stream.filter(track=['nba']) api = tweepy.API(auth) auth.set_access_token(access_token, access_secret) twitterStream = Stream(auth, TweetListener()) #hadoop fs -put twitterStream /tweepy/hdfs.txt #os.system('echo "%s" | hadoop fs -put twitterStream /tweepy/hdfs.txt' %(json.dump(twitterStream))) print("Streaming started...") try: stream.filter(track=track) except: print("error!") stream.disconnect()
def twitterapi(CONSUMER_KEY, CONSUMER_SECRET, token=None, force=False, *args, **kwargs): """ return twitter API object - with or without access token. """ global auth if not auth: auth = tweepy.auth.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) if token: auth.set_access_token(token.key, token.secret) tweetapi = tweepy.API(auth, *args, **kwargs) if not tweetapi: raise Exception("no api returned - %s" % str(auth)) logging.warn("twitter api returned: %s" % str(tweetapi)) return tweetapi
def start_stream(): while True: try: auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET) api = tweepy.API(auth) stream = tweepy.streaming.Stream(auth, MyStreamListener(api)) stream.filter(track=q, async=True) except: continue
def get(self): auth = tweepy.auth.OAuthHandler(self._v('consumer_key'), self._v('consumer_secret')) auth.set_access_token(self._v('access_token'), self._v('access_secret')) return auth
def __init__(self, cfg): ks = cfg.get_auth() auth = tweepy.OAuthHandler(ks["consumer_key"], ks["consumer_secret"]) auth.set_access_token(ks["token"], ks["token_secret"]) self.api = tweepy.API(auth)
import tweepy from tweepy import auth consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXX" consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXX" access_token = "XXXXXXXXXXXXXXXXXXXXXXXX" access_secret = "XXXXXXXXXXXXXXXXXXXXXXXX" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) api = tweepy.API(auth) tweets = api.user_timeline(id="dhruvseth2006", count=20) for tweet in tweets: print(tweet.text)
import random from credentials import credentials pws = credentials() #pws object from credentials.py that holds all the credentials # pws = { # "WORDNIK_API_KEY" : "xyz", # "CONSUMER_KEY" : "xyz", # "CONSUMER_SECRET" : "xyz", # "ACCESS_TOKEN" : "xyz", # "ACCESS_TOKEN_SECRET" : "xyz" # } auth = auth.OAuthHandler(pws["CONSUMER_KEY"], pws["CONSUMER_SECRET"]) auth.set_access_token(pws["ACCESS_TOKEN"], pws["ACCESS_TOKEN_SECRET"]) API = tweepy.API(auth) # to debug http requests # h=urllib2.HTTPHandler(debuglevel=1) # opener = urllib2.build_opener(h) # urllib2.install_opener(opener) def getPartOfSpeech(word): endpoint = "http://api.wordnik.com:80/v4/word.json/" + word + "/definitions?limit=1&includeRelated=true&useCanonical=false&includeTags=false&api_key=" + pws[ "WORDNIK_API_KEY"] response = urllib2.urlopen(endpoint) data = json.loads(response.read()) if data != [] and "partOfSpeech" in data[0]: