50 lines
1.8 KiB
Python
Executable file
50 lines
1.8 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
"""Deletes old tweets."""
|
|
__author__ = "Tobias Schmidl"
|
|
__copyright__ = "Copyright 2021 Tobias Schmidl"
|
|
__license__ = "MIT"
|
|
__version__ = "0.1"
|
|
|
|
import os
|
|
from datetime import datetime, timedelta
|
|
|
|
import toml
|
|
import tweepy
|
|
from xdg import xdg_config_home
|
|
|
|
# options
|
|
twitter_config = toml.load(os.path.join(xdg_config_home(), "twitter.toml"))
|
|
test_mode = twitter_config["test_mode"] if "test_mode" in twitter_config else False
|
|
verbose = twitter_config["verbose"] if "verbose" in twitter_config else False
|
|
days_to_keep = twitter_config["days_to_keep"] if "days_to_keep" in twitter_config else 30
|
|
tweets_to_save = twitter_config["tweets_to_save"] if "tweets_to_save" in twitter_config else []
|
|
secrets = twitter_config["secrets"]
|
|
|
|
# auth and api
|
|
if __name__ == "__main__":
|
|
auth = tweepy.OAuthHandler(secrets['api_key'], secrets['api_key_secret'])
|
|
auth.set_access_token(secrets['access_token'], secrets['access_token_secret'])
|
|
api = tweepy.API(auth)
|
|
# set cutoff date, use utc to match twitter
|
|
cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep)
|
|
|
|
# get all timeline tweets
|
|
print("Retrieving timeline tweets")
|
|
timeline = tweepy.Cursor(api.user_timeline).items()
|
|
deletion_count = 0 # pylint:disable=C0103
|
|
ignored_count = 0 # pylint:disable=C0103
|
|
|
|
for tweet in timeline:
|
|
# where tweets are not in save list and older than cutoff date
|
|
if tweet.id not in tweets_to_save and tweet.created_at < cutoff_date:
|
|
if verbose:
|
|
print("Deleting %d: [%s] %s" % (tweet.id, tweet.created_at, tweet.text))
|
|
if not test_mode:
|
|
api.destroy_status(tweet.id)
|
|
|
|
deletion_count += 1
|
|
else:
|
|
ignored_count += 1
|
|
|
|
print("Deleted %d tweets, ignored %d" % (deletion_count, ignored_count))
|