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

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com



自怼圈/年番新

DU21.4
关于 ~ DebugUself with DAMA ;-)


关注公众号, 持续获得相关各种嗯哼:
zoomquiet


粤ICP备18025058号-1
公安备案号: 44049002000656 ...::