This article was translated to English, and was originally published for deviceplus.jp.
Welcome to part 2 of tweeting on Raspberry Pi with “Twython.”
Like last time, we’ll be following along the Raspberry Pi Official Site Document. We’ll be covering posts with random character strings, posts with images, and linking up a streaming API.
We’ll be using the 2017-11-29 release of “Raspbian Stretch with desktop” OS version with a “Raspberry Pi2 Model B” for this article.
For part 1, check out →”Tweet on Raspberry Pi via Twython! (Part 1)”
Posting a Random Tweet
“Tweet random messages” on page 7 uses a “random” Python module and introduces a program for posting random messages.
03from twython import Twython
05from auth import(
28"How's it going?",
"Have you been here before?",
"Get a hair cut!",
We then saved the file under the name “random_tweet.py” in the “/home/pi” directory.
The module called “auth” on line 3 is “auth.py” that we created last time. This module collects “Consumer Key (API Key),” “Consumer Secret (API Secret),” “Access Toke,” and “Access Token Secret ” all in one place. Next, let’s prepare it in the same directory as “random_tweet.py.”
*Replace each value with what you obtained with Twitter Apps.
Lines 2-15 are the same as the Twitter post program (twitter.py) we made at the end of the previous article.
Add “import random” into the first line to be able to use random numbers. On lines 17 to 24, we stored six messages in an array; lets select one of them in line 25.
The output comes out as 「How’s it going?」. That’s the fourth stored character string of “messages” variable (array).
If we look at the actual post, we can confirm that it’s posted as 「How’s it going?」. Although it’s a random post, if the same character string is repeatedly posted, then an error from Twitter is going to come up. When that happens, give it a little bit of time before reposting and try not to post the same character string (For example, including the time and character string). That should solve things.
Posting with an Image!
“Tweet a picture” on the 8th page outlines how to post a tweet with an image.
Before starting a program, let’s prepare a suitable image first. This time, we’ll be adjusting the document and preparing 「image.png」 onto 「 /home/pi/Downloads/ 」.
from twython import Twython
from auth import
message="Hello world - here's a picture!"
Lines 17-18 will be changed. Instead of 「update_status」, 「update_status_with_media」will be posted.
This is what it’ll look like. Somehow, it’s displaying some warning text.
Looking up why this is showing up, it looks like it’s currently not recommended to use the 「update_status_with_media」function.
Although it’s not recommended, it looks like we were able to safely post the tweet. Speaking of which, that got us a little interested in this method. Look at the Twython Official Site Manual; we rewrote the program!
“TwythonStreamer” is a type of class used for streaming API on Twitter.
Streaming message types — Twitter Developers
On lines 9-16, classes called “MyStreamer” are declared. This is for appointing actions when tweets are found.
On the program source above, aside from the body text of the tweet, the date, account name, and separator line are all customized for output.
On lines 18-24, I created an instance for a new “MyStreamer” class, and started tracking only tweets that contained “# raspberrypi”.
If you find the tweet, the output will look like this. Until you stop the program, it’ll continue obtaining these types of tweets. (It looks like it doesn’t differentiate between large or small characters)
Next let’s access Twitter and check out the search results.
You’ll get somewhat of a time log, but I think that roughly confirms that you can obtain in real time.
That wraps up our 2nd part explaining how to use “Twython.” You’ll now be able to post and search on Twitter with relative ease!
I think that “Twython” with Python as a base is easy to introduce to any electronics that use parts. I thought “Twython” would be a good way to bridge the internal processing through the API and the part that’s outputted in a visible form. By posting photos you’ve taken, or by taking action when there’s a tweet about a specific keyword, you can make a wide variety of things coupled with everything you’ve learned now!