Python script for printing out an account's tweets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
- Install the Python twitter client (
pip install python-twitter
) - Create a
config.py
file with your credentials for python-twitter client (see docs for python-twitter) - Run with
python account_tweets.py $USERNAME
- 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
Could you do a tutorial on how to use this please