Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script for printing out an account's tweets
Script for printing out tweets from a given account
Sample usage:
python qikipedia
import twitter
import sys
# Put your credentials in a module
from config import * # noqa
api = twitter.Api(
# 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,, url(tweet)))
def url(tweet):
# username doesn't matter, it will get redirected
return "" %
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 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:
for status in statuses:
yield status
max_id = - 1
if __name__ == "__main__":
username = sys.argv[1]
writer = sys.stdout
# Pass custom filters here
filters = [
write_tweets(username, writer, filters)
Copy link

Could you do a tutorial on how to use this please

Copy link


  1. Install the Python twitter client (pip install python-twitter)
  2. Create a file with your credentials for python-twitter client (see docs for python-twitter)
  3. Run with python $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