Skip to content

Instantly share code, notes, and snippets.

@miglen
Last active April 18, 2018 13:38
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 miglen/56ee2bc8a4e4708fd3768e76eedad599 to your computer and use it in GitHub Desktop.
Save miglen/56ee2bc8a4e4708fd3768e76eedad599 to your computer and use it in GitHub Desktop.
Ephemeral tweets - python implementation
from datetime import datetime, timedelta
import os
import twitter
'''
Ephermal twitter tweets written in python
Inspired by bit.ly/2JXNkpI
Relies on: github.com/bear/python-twitter
Runs easily as AWS Lambda: amzn.to/2oif6Wv
You may set environment variables to control
the list of IDs you want ot keep as well as
max hours to keep tweets.
'''
# Set environment variables
access_token_key = os.getenv("TWITTER_ACCESS_TOKEN")
access_token_secret = os.getenv("TWITTER_ACCESS_TOKEN_SECRET")
consumer_key = os.getenv("TWITTER_CONSUMER_KEY")
consumer_secret = os.getenv("TWITTER_CONSUMER_SECRET")
keep_ids = os.getenv("TWITTER_KEEP_TWEETS").split(',')
# Get tweet age or set default to two weeks
if os.environ.get('TWITTER_MAX_TWEET_AGE') is None:
tweet_age = 336 # 336 hours is 14 days
else:
tweet_age = os.getenv("TWITTER_MAX_TWEET_AGE")
delta = datetime.today() - timedelta(hours=tweet_age)
# Get the list of most recent 200 statuses
statuses = api.GetUserTimeline(screen_name=api.VerifyCredentials().screen_name, count=200)
for t in statuses:
time_struct = time.strptime(t.created_at, "%a %b %d %H:%M:%S +0000 %Y")
tweet_date = datetime.fromtimestamp(time.mktime(time_struct))
if tweet_date >= delta and t.id not in keep_ids:
print("Deleting ID: {} - {}".format(t.id, t.text))
api.DestroyStatus(t.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment