Search code examples
luarssconky

What's the best way to display a différent title of a post from TorrentFreak using RSS and Conky?


I'm looking for a way to show every 5 second a different title of one of the post from the RSS feed of TorrentFreak.com

I'm not sure how to do it, or event if it's possible, but I try.

So far I tried to do it in Lua :

-- Check if Conky env
if conky == nil then
    os.exit(1)
end

conky.config = {
    use_xft = true,
    font = 'DejaVu Sans:size=10',
    update_interval = 2, -- update every 2sec
}

function conky_rss()
    local rss_url = "https://torrentfreak.com/feed/"

    function get_all_rss_titles()
        local xml = io.popen('curl -s "'..rss_url..'"'):read('*all')

        local xml2lua = require("xml2lua")
        local handler = require("xmlhandler.tree")
        local parser = xml2lua.parser(handler)
        parser:parse(xml)

        local titles = {}
        for _, item in ipairs(handler.root.RSS.Channel.Item) do
            table.insert(titles, item.Title)
        end

        return titles
    end

    function display_next_title()
        local titles = get_all_rss_titles()

        local index = tonumber(os.date('%S')) % #titles + 1
        local title = titles[index]

        return title
    end

    return display_next_title()
end

And I'm calling this script like that in Conky.text :

${color1} -> ${color0}${lua /home/Cthulhu/.conky/My_Script/Script_Sources/conky-rss.lua}

It didn't show anything yet ... Any ideas to make it work or to improve it ?


Solution

  • The Lua file shouldn't be referred to in the lua object. The object should be simply {lua conky_rss}.

    The Lua file should be referred to in a lua_load option in the conky.config section of Conky.text. That option should be lua_load = '/home/Cthulhu/.conky/My_Script/Script_Sources/conky-rss.lua',.