Home

Script: wtwitter.py

Send latest tweet to the current buffer. (for WeeChat ≥ 0.3.0, requires: feedparser)
Author: Jimmy Zelinskie, version 0.2, GPL3 — added: 2012-08-17, updated: 2013-05-01
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
    wtwitter.py

    author: Jimmy Zelinskie <jimmyzelinskie@gmail.com>
      desc: Sends your latest tweet to the current buffer
     usage:
       /set plugins.var.python.wtwitter.twitter_handle yourusername
       /wtwitter
   license: GPL3

   history:

       0.2 - rename script to wtwitter.py (twitter is a python module)
       Sebastien Helleu <flashcode@flashtux.org>

       0.1 - initial script
       Jimmy Zelinskie <jimmyzelinskie@gmail.com>

       0.0 - forked from lastfm.py by
       Adam Saponara <saponara TA gmail TOD com>

"""

import weechat
import feedparser

weechat.register("wtwitter", "Jimmy Zelinskie", "0.2", "GPL3", "Sends your latest tweet to the current buffer", "", "")

defaults = {
        "twitter_handle" : "nobody",
        "command" : "/me last tweeted: %s"
}

cmd_hook_process = ""
cmd_buffer       = ""
cmd_stdout       = ""
cmd_stderr       = ""

for k, v in defaults.iteritems():
        if not weechat.config_is_set_plugin(k):
                weechat.config_set_plugin(k, v)

def twitter_cmd(data, buffer, args):
        global cmd_hook_process, cmd_buffer, cmd_stdout, cmd_stderr
        if cmd_hook_process != "":
                weechat.prnt(buffer, "Twitter is already running!")
                return weechat.WEECHAT_RC_OK
        cmd_buffer = buffer
        cmd_stdout = ""
        cmd_stderr = ""
        python2_bin = weechat.info_get("python2_bin", "") or "python"
        cmd_hook_process = weechat.hook_process(
                python2_bin + " -c \"\n"
                "import sys, feedparser\n"
                "feed = None\n"
                "feed = feedparser.parse('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=%(username)s')\n"
                "if not feed or feed.bozo:\n"
                "	print >>sys.stderr, 'Could not fetch Twitter RSS feed.',\n"
                "elif not 'items' in feed or len(feed['items']) < 1:\n"
                "	print >>sys.stderr, 'No tweets found in Twitter RSS feed.',\n"
                "else:\n"
                "	print '@'+feed['items'][0]['title'].replace(u'\u2013', '-').encode('utf-8', 'replace'),\n"
                "\"" % {"username" : weechat.config_get_plugin('twitter_handle')},
                10000, "twitter_cb", "")
        return weechat.WEECHAT_RC_OK

def twitter_cb(data, command, rc, stdout, stderr):
        global cmd_hook_process, cmd_buffer, cmd_stdout, cmd_stderr
        cmd_stdout += stdout
        cmd_stderr += stderr
        if int(rc) >= 0:
                if cmd_stderr != "":
                        weechat.prnt(cmd_buffer, "%s" % cmd_stderr)
                if cmd_stdout != "":
                        weechat.command(cmd_buffer, weechat.config_get_plugin("command") % cmd_stdout)
                cmd_hook_process = ""
        return weechat.WEECHAT_RC_OK

hook = weechat.hook_command(
        "wtwitter",
        "Sends your latest tweet to the current buffer. Before using /wtwitter, set your twitter handle like this:\n\n"
        "    /set plugins.var.python.wtwitter.twitter_handle yourhandle\n\n"
        "You can also customize the command that will be sent to the buffer like this:\n\n"
        "    /set plugins.var.python.wtwitter.command I last tweeted %s\n",
        "", "", "", "twitter_cmd", "")