""" { "contributors": null, "truncated": false, "text": "TeeMinus24's Shirt of the Day is Palpatine/Vader '12. Support the Sith. Change you can't stop. http://t.co/wFh1cCep", "in_reply_to_status_id": null, "id": 175090352598945794, "entities": { "user_mentions": [], "hashtags": [], "urls": [ { "indices": [ 95, 115 ], "url": "http://t.co/wFh1cCep", "expanded_url": "http://fb.me/1isEdQJSq", "display_url": "fb.me/1isEdQJSq" } ] }, "retweeted": false, "coordinates": null, "source": "Facebook", "in_reply_to_screen_name": null, "id_str": "175090352598945794", "retweet_count": 0, "in_reply_to_user_id": null, "favorited": false, "user": { "follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif", "verified": false, "profile_image_url_https": "https://si0.twimg.com/profile_images/1428484273/TeeMinus24_logo_normal.jpg", "profile_sidebar_fill_color": "efefef", "is_translator": false, "id": 281077639, "profile_text_color": "333333", "followers_count": 43, "protected": false, "location": "", "profile_background_color": "131516", "id_str": "281077639", "utc_offset": -18000, "statuses_count": 461, "description": "We are a limited edition t-shirt company. We make tees that are designed for the fan; movies, television shows, video games, sci-fi, web, and tech. We have it!", "friends_count": 52, "profile_link_color": "009999", "profile_image_url": "http://a0.twimg.com/profile_images/1428484273/TeeMinus24_logo_normal.jpg", "notifications": null, "show_all_inline_media": false, "geo_enabled": false, "profile_background_image_url": "http://a0.twimg.com/images/themes/theme14/bg.gif", "screen_name": "TeeMinus24", "lang": "en", "profile_background_tile": true, "favourites_count": 0, "name": "Vincent Genovese", "url": "http://www.teeminus24.com", "created_at": "Tue Apr 12 15:48:23 +0000 2011", "contributors_enabled": false, "time_zone": "Eastern Time (US & Canada)", "profile_sidebar_border_color": "eeeeee", "default_profile": false, "following": null, "listed_count": 1 }, "geo": null, "in_reply_to_user_id_str": null, "possibly_sensitive": false, "created_at": "Thu Mar 01 05:29:27 +0000 2012", "possibly_sensitive_editable": true, "in_reply_to_status_id_str": null, "place": null }""" from tweepy import StreamListener import json, time, sys class SListener(StreamListener): def __init__(self, api = None, fprefix = 'streamer'): self.api = api or API() self.counter = 0 self.fprefix = fprefix self.output = open('../data/' + fprefix + '.' + time.strftime('%Y%m%d-%H%M%S') + '.json', 'w') self.delout = open('../data/delete.txt', 'a') def on_data(self, data): if 'in_reply_to_status' in data: self.on_status(data) elif 'delete' in data: delete = json.loads(data)['delete']['status'] if self.on_delete(delete['id'], delete['user_id']) is False: return False elif 'limit' in data: if self.on_limit(json.loads(data)['limit']['track']) is False: return False elif 'warning' in data: warning = json.loads(data)['warnings'] print warning['message'] return false def on_status(self, status): self.output.write(status + "\n") self.counter += 1 if self.counter >= 20000: self.output.close() self.output = open('../data/' + self.fprefix + '.' + time.strftime('%Y%m%d-%H%M%S') + '.json', 'w') self.counter = 0 return def on_delete(self, status_id, user_id): self.delout.write( str(status_id) + "\n") return def on_limit(self, track): sys.stderr.write(track + "\n") return def on_error(self, status_code): sys.stderr.write('Error: ' + str(status_code) + "\n") return False def on_timeout(self): sys.stderr.write("Timeout, sleeping for 60 seconds...\n") time.sleep(60) return from slistener import SListener import time, tweepy, sys ## auth. ## TK: Edit the username and password fields to authenticate from Twitter. username = '' password = '' auth = tweepy.auth.BasicAuthHandler(username, password) api = tweepy.API(auth) ## Eventually you'll need to use OAuth. Here's the code for it here. ## You can learn more about OAuth here: https://dev.twitter.com/docs/auth/oauth #consumer_key = "" #consumer_secret = "" #access_token = "" #access_token_secret = "" # OAuth process, using the keys and tokens #auth = tweepy.OAuthHandler(consumer_key, consumer_secret) #auth.set_access_token(access_token, access_token_secret) def main( mode = 1 ): track = ['obama', 'egypt'] follow = [] listen = SListener(api, 'test') stream = tweepy.Stream(auth, listen) print "Streaming started on %s users and %s keywords..." % (len(track), len(follow)) try: stream.filter(track = track, follow = follow) #stream.sample() except: print "error!" stream.disconnect() if __name__ == '__main__': main() """{ "id_str":"265631953204686848", "text":"@AllenVaughan In all fairness that \"talented\" team was 5-6...", "in_reply_to_user_id_str":"166318986", ... "entities":{ "urls":[], "hashtags":[], "user_mentions":[ { "id_str":"166318986", "indices":[0,13], "name":"Allen Vaughan", "screen_name":"AllenVaughan", "id":166318986 } ]} ... }""" #!/usr/bin/env python import json, sys def main(): for line in sys.stdin: line = line.strip() data = '' try: data = json.loads(line) except ValueError as detail: sys.stderr.write(detail.__str__() + "\n") continue if 'entities' in data and len(data['entities']['user_mentions']) > 0: user = data['user'] user_mentions = data['entities']['user_mentions'] for u2 in user_mentions: print "\t".join([ user['id_str'], u2['id_str'], "1" ]) if __name__ == '__main__': main() ## Eventually you'll need to use OAuth. Here's the code for it here.` ## You can learn more about OAuth here: https://dev.twitter.com/docs/auth/oauth` consumer_key = "YOUR CONSUMER KEY HERE" consumer_secret = "YOUR CONSUMER SECRET HERE" access_token = "YOUR TOKEN HERE" access_token_secret = "YOUR TOKEN SECRET HERE" # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)