Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active May 27, 2020 15:37
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 edsu/f3340dca45c3e8cbacdccca3137d5d56 to your computer and use it in GitHub Desktop.
Save edsu/f3340dca45c3e8cbacdccca3137d5d56 to your computer and use it in GitHub Desktop.
Collect tweet JSON for tweets that mention particular users.
#!/usr/bin/env python3
"""
Listen for tweets mentioning particular users.
"""
import json
import twarc
twitter = twarc.Twarc()
users = ['jack', 'yoyoel']
for tweet in twitter.filter(','.join(users)):
# if it's > 140 chars look for the users in the extended_tweet
if 'extended_tweet' in tweet:
user_mentions = tweet['extended_tweet']['entities']['user_mentions']
else:
user_mentions = tweet['entities']['user_mentions']
found = False
for mention in user_mentions:
if mention['screen_name'].lower() in users:
found = True
if found:
print(json.dumps(tweet))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment