Created
December 10, 2013 08:38
-
-
Save revox/7887444 to your computer and use it in GitHub Desktop.
Shows current tweet rate via the twitter streaming API using tweepy. Also some fragments for spotting retweets, conversations and inspecting tweet objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# based on https://github.com/tweepy/tweepy/blob/master/examples/streaming.py | |
import tweepy # to get your tweepy version go python -c "import tweepy; print(tweepy.__version__)" | |
import time | |
import json | |
import datetime | |
import pprint as p | |
# == OAuth Authentication (dev.twitter.com to make and app and get auth deets == | |
consumer_key = "<yours>" | |
consumer_secret = "<yours>" | |
access_token = "<yours>" | |
access_token_secret = "<yours>" | |
# OAuth process, using the keys and tokens | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
# Creation of the actual interface, using authentication | |
api = tweepy.API(auth) | |
class StreamListener(tweepy.StreamListener): | |
def __init__(self): | |
self.start_time = time.time() | |
self.count = 0 | |
self.api = api | |
super(tweepy.StreamListener, self).__init__() | |
def on_error(self, status_code): | |
print 'Error: ' + repr(status_code) | |
return False | |
def on_data(self, tweet): | |
self.count += 1 | |
now = time.time() - self.start_time | |
rate = self.count/now | |
if now > 10: | |
print datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"),',',rate | |
self.count = 0 | |
self.start_time = time.time() | |
# conversation? | |
# if tweet['in_reply_to_screen_name'] != None: | |
# print tweet['in_reply_to_screen_name'],tweet['text'] | |
# rewteet? | |
# if tweet['retweeted']: | |
# print tweet['text'],tweet['retweet_count'] | |
# inspect the tweet | |
# print json.dumps(tweet) | |
# tweet = json.loads(tweet) | |
# print p.pprint(tweet) | |
return True | |
def on_timeout(self): | |
return True # Don't kill the stream | |
l = StreamListener() | |
streamer = tweepy.Stream(auth=auth, listener=l) | |
setTerms = ['catching fire'] # list - twitter API is good for 400 terms | |
streamer.filter(track = setTerms) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment