import time
import re
import twitter
try:
# UCS-4
HIGHPOINTS = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
# UCS-2
HIGHPOINTS = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
class TwitterPurger():
'''
Purges the a Twitter account of all all tweets and favorites
'''
MAX_CALLS_PER_HOUR = 99
SECONDS_IN_AN_HOUR = 3600
def __init__(self):
self.api_call_count = 0
def increment_or_sleep(self):
'''
Increments the call count or sleeps if the max call count per hour
has been reached
'''
self.api_call_count = self.api_call_count + 1
if self.api_call_count > TwitterPurger.MAX_CALLS_PER_HOUR:
time.sleep(TwitterPurger.SECONDS_IN_AN_HOUR)
self.api_call_count = 0
def delete_everything(self, screen_name, consumer_key,
consumer_secret, access_token, access_token_secret):
'''
Deletes all statuses and favorites from a Twitter account
'''
api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token_key=access_token, access_token_secret=access_token_secret)
var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
self.increment_or_sleep()
while len(var_time_line_statuses) > 0:
for status in var_time_line_statuses:
print('Deleting status {id}: {text}'
.format(id=str(status.id),
text=HIGHPOINTS.sub('_', status.text)))
api.DestroyStatus(status.id)
var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
self.increment_or_sleep()
user_favorites = api.GetFavorites(screen_name=screen_name)
self.increment_or_sleep()
while len(user_favorites) > 0:
for favorite in user_favorites:
print('Deleting favorite {id}: {text}'
.format(id=str(favorite.id),
text=HIGHPOINTS.sub('_', favorite.text)))
api.DestroyFavorite(status=favorite)
user_favorites = api.GetFavorites(screen_name=screen_name)
self.increment_or_sleep()
Windows users will need to run the command "chcp 65001" in their console before running the script from the command prompt.