Skip to content

Instantly share code, notes, and snippets.

@ashander
Last active May 3, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashander/7a55daa5e9d98b88729893c64ff4349d to your computer and use it in GitHub Desktop.
Save ashander/7a55daa5e9d98b88729893c64ff4349d to your computer and use it in GitHub Desktop.
Follow tweets and retweets (etc) on streaming API
name: tweets
channels: !!python/tuple
- defaults
dependencies:
- decorator=4.0.11=py36_0
- ipython=5.1.0=py36_0
- ipython_genutils=0.1.0=py36_0
- openssl=1.0.2k=0
- path.py=10.0=py36_0
- pexpect=4.2.1=py36_0
- pickleshare=0.7.4=py36_0
- pip=9.0.1=py36_1
- prompt_toolkit=1.0.9=py36_0
- ptyprocess=0.5.1=py36_0
- pygments=2.1.3=py36_0
- python=3.6.0=0
- readline=6.2=2
- setuptools=27.2.0=py36_0
- simplegeneric=0.8.1=py36_1
- six=1.10.0=py36_0
- sqlite=3.13.0=0
- tk=8.5.18=0
- traitlets=4.3.1=py36_0
- wcwidth=0.1.7=py36_0
- wheel=0.29.0=py36_0
- xz=5.2.2=1
- zlib=1.2.8=3
- pip:
- ipython-genutils==0.1.0
- oauthlib==2.0.1
- prompt-toolkit==1.0.9
- requests==2.13.0
- requests-oauthlib==0.7.0
- tweepy==3.5.0
prefix: /home/jaime/miniconda3/envs/tweets
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import sys
class RetweetListener(StreamListener):
""" Handles tweets that are received from the stream.
Prints json to stdout
Adapted from:
https://github.com/tweepy/tweepy/blob/master/examples/streaming.py
"""
def __init__(self, user):
self._id_str = user
super().__init__()
@property
def source_id(self):
"""Not used currently"""
return self._id_str
def on_data(self, data):
"""Print retweets"""
try:
tweet, rt = self._process_data(data)
json.dump(tweet, sys.stdout)
print('\n', file=sys.stdout)
return True
except:
return True
def on_error(self, status):
print(status)
def _process_data(self, data):
tweet = json.loads(data)
retweet_of = tweet.pop('retweeted_status')
return tweet, retweet_of['id_str']
if __name__ == '__main__':
"""Follow a user via the streaming API
the follow parameter gets a lot of info:
https://dev.twitter.com/streaming/overview/request-parameters#follow
run like:
python stream_listener.py USER_ID > data.txt
"""
user = sys.argv[1]
with open('.creds.json', 'r') as f:
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you then you will be
# redirected to your app's page. Create an access token under the the
# "Your access token" section.
# Store these, in a file named .creds.json with the format below
# {
# "consumer_key": "xxxxxxxxxxxxxxxxxxxxxx",
# "consumer_secret": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
# "access_token": "yyyyyy-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
# "access_token_secret": "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
# }
creds = json.load(f)
auth = OAuthHandler(creds['consumer_key'], creds['consumer_secret'])
auth.set_access_token(creds['access_token'], creds['access_token_secret'])
l = RetweetListener(user)
stream = Stream(auth, l)
stream.filter(follow=[user])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment