Spring is all about cleaning, the saying goes, so why don’t apply the same principle also the the accounts I follow on Twitter? Why? Because I would like to maintain their number under 400 and because I would like to grow my very limited Python skills.
With the help of TweetPony (among the many), the task was pretty straightforward. Final result is a simple script that checks for the people I follow, verifies their last tweet date and alert me if it is older than four months.
Configure the Python environment (Ubuntu 14.04 Trusty)
I don’t want to pollute my system-wide Python installation with libraries and dependencies related to a single project, so I created a virtual environment. Still not a master on that, so forgive my errors:
apt-get install python-pip sudo pip install virtualenv cd %projectdir% virtualenv build_dir source build_dir/bin/activate |
From now ongoing, all the pip commands will be execute inside the (build_dir) virtualdev, and not at system-wide level. Time to install the TweetPony library:
sudo pip install tweetpony |
Once installed, I tried some examples from the GitHub repo, to check if it worked. And yes, it did (even without api key and permission, see later), but a boring console message appeared every time the script made a call to Twitter API, caused probably by the old Python 2.7.6 version or libs I was using:
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning
In order to solve it, I installed some dev libraries required to compile some other Python libraries (again, inside the virtualenv only)
sudo apt-get install libssl-dev sudo apt-get install libfii-dev pip install cryptography pip install pyopenssl ndg-httpsclient pyasn1 pip install urllib3 |
and added these lines of code at the beginning of the main function of the script, before any Twitter API call:
import urllib3.contrib.pyopenssl urllib3.contrib.pyopenssl.inject_into_urllib3() |
They made the trick! But, as I said, probably you may not need all of these.
The script
The script itself it’s pretty simple. I took the basic code to create the TweetPony API object from the repo’s example folder and I was able to get user’s friends_id (the account the user follows). Then, cycling thru each one, I checked the status of that friend, watching for last tweet date. Some cornercases management (like private tweets or no tweets at all) and voila’, I had all I needed.
Regarding authentication, all Twitter’s libraries require a consumer key and consumer secret to work, in addition to an OAuth access_token and access_token_secret. What made me preferred TweetPony to other libs, like tweepy or python-twitter, was that TweetPony doesn’t required anything. Test consumer key and secret are gently embedded into the lib source, while OAuth tokens are created on the fly for you and persisted over a file, .auth_data.json. To use new credentials, simply delete the file and add somewhere, at the beginning of your code, these two lines, with key and secret obtained from Twitter Dev Console:
tweetpony.CONSUMER_KEY = 'xxxx' tweetpony.CONSUMER_SECRET = 'xxxxx' |
Final consideration about Twitter API usage: there is a limit of 180 calls every 15 minutes, so I added a sleep after every check. Slow, but it worked with my 500+ followers :)
Here the complete script (and the gist too):
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 = 'xxxxx' 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() |
Gist to verify if your friends haven’t been tweeting since 4 months – https://t.co/1WT1tTnou1 , and some context http://t.co/3tyvTWpMSt