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 :)
Continue reading “Identify your Twitter followings older that 4 months”