In [1]:
import json
import os.path
import time
import logging
import datetime
import itertools
import requests
import geojson
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import IPython.display
logger = logging.getLogger('notebook')
logger.setLevel(logging.INFO)
In [2]:
# secret keys are stored here
keys = json.load(open(os.path.expanduser("~/.twitter.json")))['notebook-example']
# you can generate keys here:
# https://apps.twitter.com/
In [3]:
# now we can load the access keys
auth = tweepy.OAuthHandler(keys['consumer_key'], keys['consumer_secret'])
auth.set_access_token(keys['access_key'], keys['access_secret'])
api = tweepy.API(auth)
In [4]:
# this should display your name
user = api.me()
user.name
Out[4]:
In [5]:
public_tweets = api.home_timeline(count=2)
for tweet in public_tweets:
print(tweet.created_at, tweet.text)
In [6]:
# display a tweet inline
IPython.display.HTML(api.get_oembed(id=tweet.id, hide_media=False)['html'])
Out[6]:
In [7]:
counter = itertools.count()
tweets = api.search('#CoastSnapNarra', include_entities=True, count=100)
for tweet in tweets:
print(tweet.created_at, tweet.text)
for entity in tweet.entities['media']:
resp = requests.get(entity['media_url'])
# download the image
with open('img-%06d.jpg' % next(counter), 'wb') as f:
f.write(resp.content)
In [12]:
# now let's start listening for events
class PrintListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
print_listener = PrintListener()
stream = Stream(api.auth, print_listener)
stream.filter(track=['flooded'], async=True)
In [13]:
stream.disconnect()
In [ ]:
api.