Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created November 7, 2017 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codeinthehole/0e7430d79f3dcd1235c89f9367a49a1b to your computer and use it in GitHub Desktop.
Save codeinthehole/0e7430d79f3dcd1235c89f9367a49a1b to your computer and use it in GitHub Desktop.
Python script for printing out an account's tweets
"""
Script for printing out tweets from a given account
Sample usage:
python account_tweets.py qikipedia
"""
import twitter
import sys
# Put your credentials in a config.py module
from config import * # noqa
api = twitter.Api(
consumer_key=TWITTER_CONSUMER_KEY,
consumer_secret=TWITTER_CONSUMER_SECRET,
access_token_key=TWITTER_ACCESS_TOKEN_KEY,
access_token_secret=TWITTER_ACCESS_TOKEN_SECRET,
)
# Filters
def ignore_retweets(tweet):
if not tweet.text.startswith("RT"):
return tweet
def word_of_the_day(tweet):
if tweet.text.startswith("Word of the day"):
return tweet
# Stop functions
NEVER = lambda _: True
# Core tweet printing
def write_tweets(username, writer, filters=(), stop_fn=NEVER):
"""
Write tweets to the given file-object
Filters are functions that take a tweet and return either a filtered version of the tweet text
or None if the tweet is to be filtered out.
The stop function defines when to stop iterating.
"""
for tweet in filtered_tweets(username, filters=filters, stop_fn=stop_fn):
writer.write("%s - %s - %s\n" % (tweet.text, tweet.id, url(tweet)))
def url(tweet):
# username doesn't matter, it will get redirected
return "https://twitter.com/someusername/status/%s" % tweet.id
def filtered_tweets(username, filters, stop_fn):
for tweet in tweets(username, stop_fn=stop_fn):
filtered_tweet = filter_tweet(tweet, filters)
if filtered_tweet is not None:
yield filtered_tweet
def filter_tweet(tweet, filters):
for f in filters:
tweet = f(tweet)
if tweet is None:
return
return tweet
def tweets(username, stop_fn, batch_size=10):
"""
Return a generator that emits tweet status objects
"""
max_id = None
while True:
# Fetch tweets in batches
statuses = api.GetUserTimeline(screen_name=username, max_id=max_id, count=batch_size)
if len(statuses) == 0:
break
for status in statuses:
yield status
max_id = status.id - 1
if __name__ == "__main__":
username = sys.argv[1]
writer = sys.stdout
# Pass custom filters here
filters = [
ignore_retweets,
word_of_the_day,
]
write_tweets(username, writer, filters)
@xenonbeats
Copy link

Could you do a tutorial on how to use this please

@codeinthehole
Copy link
Author

@xenonbeats

  1. Install the Python twitter client (pip install python-twitter)
  2. Create a config.py file with your credentials for python-twitter client (see docs for python-twitter)
  3. Run with python account_tweets.py $USERNAME
  4. Adapt the filter functions that are passed in if you want to filter out certain tweets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment