""" | |
Script to identify accounts followed older than 4 months. | |
Source blogpost: http://www.rainbowbreeze.it/identify-your-twitter-followings-older-that-4-months | |
""" | |
import tweetpony | |
from datetime import timedelta, datetime | |
import urllib3.contrib.pyopenssl | |
import time | |
import json | |
import os | |
def authenticate(): | |
try: | |
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: ") | |
api.authenticate(verifier) | |
except tweetpony.APIError as err: | |
print("Oh no! You could not be authenticated. Twitter returned error #%i and said: %s" | |
% (err.code, err.description)) | |
else: | |
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: | |
f.write(json.dumps(auth_data)) | |
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")): | |
authenticate() | |
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".auth_data.json"), 'r') as f: | |
auth_data = json.loads(f.read()) | |
try: | |
api = tweetpony.API(tweetpony.CONSUMER_KEY, tweetpony.CONSUMER_SECRET, auth_data['access_token'], | |
auth_data['access_token_secret']) | |
except tweetpony.APIError as err: | |
print("Oh no! You could not be authenticated. Twitter returned error #%i and said: %s" % (err.code, err.description)) | |
else: | |
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 = datetime.now() - timedelta(weeks=16) | |
try: | |
# 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.name + ' (' + 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: | |
inactive_friends.append(friend_id) | |
print(log_begin + 'never twitted') | |
# Finally a proper user :) | |
else: | |
status_date = friend.status.created_at | |
if status_date < four_months_ago: | |
inactive_friends.append(friend_id) | |
print(log_begin + 'may be old, tweeted last time on ' + unicode(status_date) + ' - Check https://twitter.com/' + friend.screen_name) | |
else: | |
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 | |
time.sleep(5) | |
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 | |
urllib3.contrib.pyopenssl.inject_into_urllib3() | |
# Custom Twitter API keys, change with yours! | |
tweetpony.CONSUMER_KEY = 'xxxxxxx' | |
tweetpony.CONSUMER_SECRET = 'xxxxxx' | |
api = get_api() | |
if not api: | |
return | |
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__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment