__del__( self )

Eaten by the Python.

A Python Script Controlled via Twitter

| Comments

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
from urllib import urlopen
from bs4 import BeautifulSoup # module for HTML parsing

def get_tweets(username):
    """ Gets the texts and links of username's lattest tweets"""

    url = urlopen( "https://twitter.com//" + username)
    page = BeautifulSoup( url )
    url.close()

    texts = [p.text for p in page.findAll("p")
             if ("class" in p.attrs) and
             ("ProfileTweet-text" in p.attrs["class"])]

    links = [a.attrs["href"] for a in page.findAll("a")
             if ("class" in a.attrs) and
             ("ProfileTweet-timestamp" in a.attrs["class"])]

    return zip(texts, links)

Let us try it on John D. Cook:

1
>>> print(get_tweets("JohnDCook")[-1]) # John's lattest tweet
1
2
(u"Data cleaning code cannot be clean. It's a sort of sin eater.",
  '/StatFact/status/492753200190341120')

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
import time
import subprocess

old_tweets = [] # tweets that have already been read
while True:
    tweets = [tweet for tweet in get_tweets("Zulko___")
              if tweet not in old_tweets]
    for (text, link) in tweets:
        if text.startswith("cmd: "):
            subprocess.Popen(text[5:], shell="True")
    old_tweets += tweets
    time.sleep(20) # wait 20 seconds

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
from twittcher import TweetSender, SearchWatcher
sender = TweetSender(smtp="smtp.gmail.com", port=587, # use gmail smtp
                     login="tintin.zulko@gmail.com", # gmail login
                     password="fibo112358", # be nice, don't try.
                     to_addrs="tintin.zulko@gmail.com", # where to send
                     sender_id = "chocolate milk") # appears in 'Subject'
bot = SearchWatcher("chocolate milk", action=sender.send)
bot.watch_every(20) # check every 20s

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).

Comments