File: http_item.lua

package info (click to toggle)
weechat-scripts 20180330-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,072 kB
  • sloc: python: 44,904; perl: 27,389; ruby: 2,101; lisp: 339; tcl: 244; sh: 8; makefile: 7
file content (120 lines) | stat: -rw-r--r-- 3,540 bytes parent folder | download | duplicates (6)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
-- HTTP bar item, using lua patterns to get content
--
-- Copyright 2013 Tor Hveem <xt@bash.no>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--

--
-- Usage: put [http_item] in your status bar items.  (Or any other bar to your liking)
-- "/set weechat.bar.status.items".
--

local w = weechat


SCRIPT_NAME    = "http_item"
SCRIPT_AUTHOR  = "xt <xt@bash.no>"
SCRIPT_VERSION = "1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Bar item with HTTP source, using lua patterns to match content"
BAR_ITEM_NAME  = SCRIPT_NAME

-- Settings
settings = {
    url               = 'http://weechat.org/info/stable/',
    pattern           = '(%d+%.%d+%.%d+)',
    message_prefix    = 'Latest WeeChat: ',
    message_postfix   = '',
    message_color     = 'default',
    interval          = '5', -- poll every 5 minutes
}
-- other globals
ITEM_TEXT = nil

function http_bi_cb(data, item, window)
    -- Return the bar item string
    if ITEM_TEXT then
        return string.format('%s%s%s%s',
                w.config_get_plugin('message_prefix'),
                w.color(w.config_get_plugin('message_color')),
                ITEM_TEXT,
                w.config_get_plugin('message_postfix')
                )
    end
    return ''
end

function http_bi_update()
    -- Function to manually update the bar item
    w.bar_item_update(BAR_ITEM_NAME)
    return w.WEECHAT_RC_OK
end

function debug(buf, str)
    -- helper function for debugging
    local debug = false
    if debug and str then
        w.print(buf, SCRIPT_NAME .. ': ' .. str)
    end
    return w.WEECHAT_RC_OK
end

function init_config()
    -- Set defaults
    for option, default_value in pairs(settings) do
        if w.config_is_set_plugin(option) == 0 then
            w.config_set_plugin(option, default_value)
        end
    end
    -- read options from weechat into our lua table
    for option, default_value in pairs(settings) do
        settings[option] = w.config_get_plugin(option)
    end
    return w.WEECHAT_RC_OK
end

function start_fetch()
    -- Get URL using weechat API for URL
    local url = w.config_get_plugin('url')
    -- 30 seconds timeout
    local timeout = 30*1000
    debug('', url)
    w.hook_process('url:'..url, timeout, 'http_fetch_cb', '')
    return w.WEECHAT_RC_OK
end

function http_fetch_cb(data, command, return_code, out, err)
    if #out > 0 then
        out = out:match(w.config_get_plugin('pattern'))
        ITEM_TEXT = out
        debug('', ITEM_TEXT)
        -- Update bar item since we got new data
        w.bar_item_update(BAR_ITEM_NAME)
    end
    return w.WEECHAT_RC_OK
end

if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '') then
    init_config()

    -- create the bar item
    w.bar_item_new(BAR_ITEM_NAME, 'http_bi_cb', '')

    -- Initial fetch
    start_fetch()

    -- hook the fetch timer
    w.hook_timer( w.config_get_plugin('interval')*60*1000, 0, 0, 'start_fetch', '')
end