Tweeting with Python tweepy on the Raspberry Pi – part 2 pi twitter app series
In late October I started a series on Twitter and Python tweepy. Then it got pushed to one side by HDMIPi. But now I’ve managed to find some “recreational” time to play with tweepy a bit more.
So in this post, which is part 2, I’m going to show you how to post a tweet using a Python program on the Raspberry Pi.
Create Your Twitter App
Create a twitter app on your twitter account, as we did in part 1 here
Give Your App Permission to Write
Then make your app read/write. You won’t be able to tweet unless you give it permission to write. Newly created apps are read-only by default. Click on the settings tab and scroll down until you see this…
…then make sure that Read and Write is selected. Then click “Update this twitter application’s settings“.
Install Tweepy (if you haven’t already)
Here’s how you install tweepy.
Now We’re Ready to Code
A lot of our code from last time can be re-used. But if you’ve created a new app, or changed the permissions on an old one, you might have to create new ‘Access tokens’.
The main new tweepy function we’re using is…
tweet_text is the text we type in at the time of running the program (in quotes)
update_status() is the function to post a tweet in tweepy.
The first 17 lines set up authorisation. Pretty much the same as before, but we’ve added
import sys to help us with the command line input arguments.
19-20 pulls in the command line text (if any) to tweet
22-23 provides a ‘default’ tweet in case no text was entered
25 checks the tweet text isn’t too long
26 sends the tweet
27-28 if the entered tweet is too long it warns you and nothing is sent
#!/usr/bin/env python2.7 # tweet.py by Alex Eames https://raspi.tv/?p=5908 import tweepy import sys # Consumer keys and access tokens, used for OAuth consumer_key = 'type in your consumer key here' consumer_secret = 'type in your consumer secret here' access_token = 'type in your access token here' access_token_secret = 'type in your access token secret here' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) if len(sys.argv) >= 2: tweet_text = sys.argv else: tweet_text = "Still messing about with tweepy and twitter API. :)" if len(tweet_text) <= 140: api.update_status(status=tweet_text) else: print "tweet not sent. Too long. 140 chars Max."
I called the program tweet.py and made it executable with
chmod +x tweet.py
then ran it with...
./tweet.py "This is a test tweet using a Python program. :) ===>"
If you run it this way, the
sys.argv will work correctly.
If you prefer to run it as...
python tweet.py, you'll need to change
sys.argv in line 19.
Note that I put the whole tweet in quotes. If you don't do that, Python treats each word as a separate command line 'argument'. Also note, that if you use single quotes and your tweet text includes an apostrophe, it'll get all messed up. (i.e. your tweet will be shortened).
Trying without a tweet...
...gives you this...
Next time we'll do something more advanced and interesting with the 'default' settings, so something else will happen if we type no tweet text. We'll take some information from somewhere and tweet about it. Click here to go there now.
[…] Tweeting with Python tweepy on the Raspberry Pi, from RasPi.TV: […]
I get lots of Followers at @dewhill who are no longer following me as of a week later & Followers who Follow/Unfollow several times within a 2-week period. Is this as a result of your service? I am blind, use a screen reader & have a hard time with Twitter online, so I use Tweetymail. I really don’t appreciate people following me just so they can get their ads in my In Box or in hopes that I will follow them back w/o check if they’re still following me. I suspect that some of this is as a result of those services who sell followers, but am wondering if your site is also part of the problem.
sorry about your twitter problems. We’re not responsible for tweepy here. I’ve never even looked at how to follow or unfollow someone with tweepy (although I think it CAN be done). Most likely the issue you’re having is with spammy service providers.
I’m just trying to teach people how to program here. Hope you get your issues sorted out.
[…] so that never proved to be a challenge. Tweeting was a bit more difficult. I found a great article (here) describing what it takes to get a tweet from the pi to the internet using python. Once I got that […]
Wondering if someone can help- I’m getting a InsecurePlatformWarning when I try to run this and I have tried everything to fix it and nothing has worked. Pls help
Just ignore it. It will still work.
I appreciate that this is nearly a year too late for you, but – having looked at http://doh.so/SSLInsecurePlatformError (or http://stackoverflow.com/questions/29099404/ssl-insecureplatform-error-when-using-requests-package if you – quite properly :)! – mistrust short links) – I got rid of it with:
apt-get install python-dev libffi-dev libssl-dev
pip install ‘requests[security]’
pip install -U tweepy
on a python 2.7 setup. I’m assuming that you’ve already pipstalled tweepy, and that the last -U tweep thing is necessary. It may not be (I’m no pipspert, but actually a pipnoramus).
I originally tried the requests security thing first, to fix tweepy, but a compile failed and indicated that apt-getting libffi-dev would probably be a good idea. If you already have it then that first apt-get may also be unnecessary.