-- 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".--localw=weechatSCRIPT_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-- Settingssettings={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 globalsITEM_TEXT=nilfunctionhttp_bi_cb(data,item,window)-- Return the bar item stringifITEM_TEXTthenreturnstring.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'))endreturn''endfunctionhttp_bi_update()-- Function to manually update the bar itemw.bar_item_update(BAR_ITEM_NAME)returnw.WEECHAT_RC_OKendfunctiondebug(buf,str)-- helper function for debugginglocaldebug=falseifdebugandstrthenw.print(buf,SCRIPT_NAME..': '..str)endreturnw.WEECHAT_RC_OKendfunctioninit_config()-- Set defaultsforoption,default_valueinpairs(settings)doifw.config_is_set_plugin(option)==0thenw.config_set_plugin(option,default_value)endend-- read options from weechat into our lua tableforoption,default_valueinpairs(settings)dosettings[option]=w.config_get_plugin(option)endreturnw.WEECHAT_RC_OKendfunctionstart_fetch()-- Get URL using weechat API for URLlocalurl=w.config_get_plugin('url')-- 30 seconds timeoutlocaltimeout=30*1000debug('',url)w.hook_process('url:'..url,timeout,'http_fetch_cb','')returnw.WEECHAT_RC_OKendfunctionhttp_fetch_cb(data,command,return_code,out,err)if#out>0thenout=out:match(w.config_get_plugin('pattern'))ITEM_TEXT=outdebug('',ITEM_TEXT)-- Update bar item since we got new dataw.bar_item_update(BAR_ITEM_NAME)endreturnw.WEECHAT_RC_OKendifw.register(SCRIPT_NAME,SCRIPT_AUTHOR,SCRIPT_VERSION,SCRIPT_LICENSE,SCRIPT_DESC,'','')theninit_config()-- create the bar itemw.bar_item_new(BAR_ITEM_NAME,'http_bi_cb','')-- Initial fetchstart_fetch()-- hook the fetch timerw.hook_timer(w.config_get_plugin('interval')*60*1000,0,0,'start_fetch','')end