Let us watch and react to the lattest tweets with Python, the dirty way.
Python modules to interact with Twitter, like tweepy, python-twitter, twitter, or twython, all depend on the Twitter API, which makes them a little complicated to use: you must open a Twitter account, register at dev.twitter.com, open a new application there, and at each connection dance with the OAuth.
If you just want to read the lattest tweets of some Twitter user, instead of using these libraries, you can simply parse the HTML of that user’s Twitter page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Let us try it on John D. Cook:
1
|
|
1 2 |
|
As an application, here is a script that watches my (useless) Twitter page every 20 seconds, and each time I tweet something like cmd: my_command
it executes my_command
in a terminal:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
I can now tweet-control, from my smartphone, any computer that is running this script. If I tweet cmd: firefox
the computer will open firefox, if I tweet cmd: echo "Hello"
it will print Hello in the terminal, etc.
Introducing Twittcher
If you want more, I wrote Twittcher, a small Python module which doesn’t depend on the Twitter API, to make bots that watch search results or user pages and react to the tweets they find.
For instance this script checks the search results for chocolate milk every 20 seconds, and sends all the new tweets (with date, username, and link) to my mail box.
1 2 3 4 5 6 7 8 |
|
Just run that script all day on your computer (or rather on your Raspberry Pi) and you will be updated every time someone drinks chocolate milk and feels the urge to tweet about it (which is very often).