Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sat Philora committed May 17, 2019
1 parent 7ad71e5 commit 6740c0b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions settings_TEMPLATE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# replace the XXX values with those found in your twitter developer console
# rename this file as twitter_keys (i.e. remove the _TEMPLATE from the filename

consumer_key: str = 'XXX API key XXX'
consumer_secret: str = 'XXX API secret key XXX'
access_token_key: str = 'XXX Access token XXX'
access_token_secret: str = 'XXX Access token secret XXX'

cutoff_date: int = 365
83 changes: 83 additions & 0 deletions tweeleter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import twitter
from datetime import datetime, timedelta, timezone
import time
import settings

DATETIME_FORMAT = '%a %b %d %H:%M:%S %z %Y' # e.g. : Tue Nov 20 17:08:43 +0000 2018

api = twitter.Api(consumer_key=settings.consumer_key,
consumer_secret=settings.consumer_secret,
access_token_key=settings.access_token_key,
access_token_secret=settings.access_token_secret)


# defaults to cutting off at 90 days ago
def get_cutoff(days_ago: int=90):
return datetime.now(timezone.utc) - timedelta(days_ago)


def as_date_time(datetime_string: str):
return datetime.strptime(datetime_string, DATETIME_FORMAT)


def status_created_before_cutoff(status):
return as_date_time(status.created_at) < cutoffDate


def delete_statuses(statuses_to_delete):
for status in statuses_to_delete:
time.sleep(1)
try:
delete_status(status.id)
except:
print(f'Could not delete status with ID {status.id}')


def find_latest_tweet(timeline):
return max(timeline, key=lambda x: x.id).id


def find_earliest_tweet(timeline):
return min(timeline, key=lambda x: x.id).id


def delete_status(status_id):
print(f'About to delete status with id: {status_id}')
api.DestroyStatus(status_id)
time.sleep(1)


def get_all_existing_statuses():
timeline = api.GetUserTimeline(count=200)

earliest_tweet_id = find_earliest_tweet(timeline)

while True:
tweets = api.GetUserTimeline(
max_id=earliest_tweet_id, count=200
)
if tweets:
new_earliest = find_earliest_tweet(tweets)

if not tweets or new_earliest == earliest_tweet_id:
break
else:
earliest_tweet_id = new_earliest
timeline += tweets

return timeline


#get all tweets
statuses = get_all_existing_statuses()

# get the earliest date at which tweets should be kept
cutoffDate = get_cutoff(settings.cutoff_date)

# create a collection of the tweets that are before the cutoff date
filtered_statuses = [status for status in statuses if (status_created_before_cutoff(status))]

# delete all the tweets before the cutoff date
delete_statuses(filtered_statuses)


0 comments on commit 6740c0b

Please sign in to comment.