Instantly share code, notes, and snippets.

What would you like to do?
Script to identify accounts followed older than 4 months.
Source blogpost:
import tweetpony
from datetime import timedelta, datetime
import urllib3.contrib.pyopenssl
import time
import json
import os
def authenticate():
api = tweetpony.API(tweetpony.CONSUMER_KEY, tweetpony.CONSUMER_SECRET)
url = api.get_auth_url()
print("Visit this URL to obtain your verification code: %s" % url)
verifier = raw_input("Input your code: ")
except tweetpony.APIError as err:
print("Oh no! You could not be authenticated. Twitter returned error #%i and said: %s"
% (err.code, err.description))
auth_data = {'access_token': api.access_token, 'access_token_secret': api.access_token_secret}
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".auth_data.json"), 'w') as f:
print("Hello, @%s! You have been authenticated. You can now run the other example scripts without having to "
"authenticate every time." % api.user.screen_name)
def get_api():
if not os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".auth_data.json")):
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".auth_data.json"), 'r') as f:
auth_data = json.loads(
api = tweetpony.API(tweetpony.CONSUMER_KEY, tweetpony.CONSUMER_SECRET, auth_data['access_token'],
except tweetpony.APIError as err:
print("Oh no! You could not be authenticated. Twitter returned error #%i and said: %s" % (err.code, err.description))
return api
return False
def find_inactive_friends(api, screen_name):
Prints all the followers of a given user which twitted, last time, more than 4 months ago
:return a list of these accounts
print("Searching for followers that have twitted last time more than 4 months ago for user " + screen_name)
inactive_friends = []
four_months_ago = - timedelta(weeks=16)
# Find all the followers of the given user
all_friends_ids = api.friends_ids(screen_name=screen_name)
total_friends = len(all_friends_ids)
print('Total friends %d' % total_friends)
counter = 0
for friend_id in all_friends_ids:
counter += 1
# Get follower information
friend = api.get_user(user_id=friend_id)
log_progress = ' (%d/%d) ' % (counter, total_friends)
log_begin = log_progress + + ' (' + friend.screen_name + ') '
# Protected tweets, no status property at all
if not hasattr(friend, 'status'):
print(log_begin + 'has private tweets')
# No status, never twitted -> old or fake user
if friend.status is None:
print(log_begin + 'never twitted')
# Finally a proper user :)
status_date = friend.status.created_at
if status_date < four_months_ago:
print(log_begin + 'may be old, tweeted last time on ' + unicode(status_date) + ' - Check' + friend.screen_name)
if 0 == counter % 5:
print(log_progress + 'and counting...')
#print(' ' + log_begin + 'seems OK, last tweet was on ' + unicode(status_date))
# Try to avoid Twitter error #88: Rate limit exceeded
# 180 request every 15 minutes, one each 5 seconds
except tweetpony.APIError as err:
print("Oh no! Twitter returned error #%i and said: %s" % (err.code, err.description))
except Exception as err:
print(' Analysing follower id %s returned error: %s' % (unicode(friend_id), err))
return inactive_friends
def main():
# Removes InsecurePlatformWarning
# Custom Twitter API keys, change with yours!
tweetpony.CONSUMER_KEY = 'xxxxxxx'
tweetpony.CONSUMER_SECRET = 'xxxxxx'
api = get_api()
if not api:
username = raw_input("Username to lookup (leave blank for your own): ").strip()
if username == "":
username = api.user.screen_name
inactive_friends = find_inactive_friends(api, username)
print('Find %d' % len(inactive_friends))
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment