Wednesday, February 24, 2016

Tweeting from your Pi - Fixing issues with Tweepy

Edit (10/21/2016) - Debian Jesse took care of most of these problems, so you may not need to do all of them. I ran through on a fresh Jesse install just to see what happened, no errors but there was a lot of "already installed" messages returned. YMMV!

I've set up one of my Raspberry Pis to do some various functions around the house - monitoring a weather radio, checking temperatures - that sort of thing. I have it send alerts via email, but wanted a more publicly accessible way of getting this data in the event that I don't have my phone with me, or whatever. You know - more redundancy is better!

Twitter is a pretty good way to satisfy that requirement, and they provide a pretty decently documented (and well hashed by programmers) API. It's pretty easy to push messages from the command line with a few lines of python.

I started by following this guide to create the application in twitter and get the necessary python libraries on my Pi. It's using Tweepy, which seems to be the de-facto way of sending data to twitter via python. For the most part, the guide works, but I did run into some issues.

First was this error while actually installing the Tweepy library:

TypeError: parse_requirements() got an unexpected keyword argument 'session'

Searching for information, it seems that pip needs to be updated. Now, you're saying to yourself, "I just installed this! Why does it need to be updated?"

The version in the repository is an old version, which is the reason for the needed upgrade. See this post on github for a description of the problem. It's easy enough to correct, just run the upgrade command on pip:

sudo pip install --upgrade pip

Great. You're now ready to tweet. You've put your keys in the programs from the guide, you execute the program, and:

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:120: 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


Well, crap. The tweet worked, but this warning pops up. Certain cryptographic extensions aren't available on the Pi. Easy enough to fix, see this stackoverflow question for more information.

To fix THIS issue, you'll need to install some more stuff. I assume you already have a gcc compiler installed, along with dev tools for it. There are plenty of tutorials out there for that, so again - I'm not going to cover it here. However, once you have that set up, you'll need to install some libraries, and then install the cryptographic extensions:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libffi-dev
sudo apt-get install python-dev

What that just did is install the Foreign Function Interface that allows Python to use compiled code from other languages. This actually has to compile some code, so it will take a few minutes (or longer, depending on your Pi.) It also installs some python development tools so gcc can properly compile python support into the next command.

Now:

sudo pip install requests[security]

That will install all the necessary security extensions. Again, this will take a few minutes. If you get other errors at this point, I would suggest just copying the error directly into a google search. Chances are, someone else has had the same error - in fact, that's how I solved all the problems here.

Now, you can tweet with Tweepy. You shouldn't get any kind of messages on the Pi unless the tweet failed in some way.

This is a fairly top-level overview of what I encountered when installing Tweepy. As always, if you're unsure of what the steps mean, or encounter other errors during the installation process, STOP! Research your particular error before continuing.