Page 1 of 1

VLC Extension: SNPviaHTTP notifier

Posted: 30 Mar 2012 22:24
by M^3
I have hacked together my first Lua script ever using examples found here in this forum and on http://lua-users.org/ and it is a bit of a mess of course.
In any case it works somewhat but it hangs the VLC GUI after a track or two. It's purpose it to send track info to Snarl http://snarl.fullphat.net/ via SNP (Snarl network protocol) over http. Maybe someone here can look at it and tell me where I went wrong after you get done laughing at my awful code. :oops:

Anyway, here it is -

Code: Select all

-- Script descriptor, called when the extensions are scanned function descriptor() return { title = "SNP via HTTP" ; version = "0.1.5 alpha 20120329" ; author = "M^3" ; url = 'http://snarl.fullphat.net/'; shortdesc = "SNPviaHTTP"; description = "<center><b>SNP via HTTP</b></center><br />" .. "Sends played track to Snarl via HTTP request. May cause the GUI of VLC to freeze on occasion.<br />" .. "While I doubt this extension will cause any serious harm I do not promise or warranty this" .. "Use at your own risk or modify to your needs, just give proper credit or blame where due . . ." ; capabilities = { "input-listener", "meta-listener", "playing-listener" } } end -- Activation hook and default settings function activate() -- wget -q -O%tmp%\test.tmp "http://%whereto%/register?id=%appid%&title=%apptitle%&icon=!power-connected" -- example registration from my scipt version -- need to turn that into a LUA http get -- currently I have it hard coded set to default to port 808 gtoken = 0 hport = 808 hhost = "127.0.0.1" userAgentHTTP = "vlcSNPviaHTTP" local retval, token = get( hhost , hport, "/register?id=application/VLC-SNP&title=SNPviaHTTP&icon=!audio-play") if token > 0 then gtoken = token vlc.msg.dbg("[SNPviaHTTP] activate() Activated, token=" .. tostring(token) .. ", Port:" .. tostring(hport)) snpreged = 1 else gtoken = 0 vlc.msg.dbg("[SNPviaHTTP] activate() Activated, no token, return value =" .. tostring(retval)) deactivate() end --start() end function url_encode(str) -- ripped directly from http://lua-users.org/wiki/StringRecipes if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "%%20") end return str end function get(host, hport, path) -- modified from an example by exebetche on videolan forums -- local host, path = parse_url(url) -- example did not have this function, not really needed for this though local header = { "GET "..path.." HTTP/1.1", "Host: "..host, "User-Agent: "..userAgentHTTP, "", "" } local request = table.concat(header, "\r\n") local response = http_req(host, hport, request) --hport should be set in the activate routine later it should be set by loading config vlc.msg.dbg("[SNPviaHTTP] response:" .. tostring(response)) local protocol, pver, retcode, ecodestr, token = ssplit( response ) vlc.msg.dbg("[SNPviaHTTP] got:" .. response .. " Token = " .. tostring(token)) if token > 0 then gtoken = token end return response, token end function http_req(host, port, request) vlc.msg.dbg("[SNPviaHTTP] http_req() - Host: " .. tostring(host) .. " Port: " .. tostring(port) .. " Request: " .. tostring(request)) -- from an example by exebetche on videolan forums local fd = vlc.net.connect_tcp(host, port) if fd >= 0 then vlc.msg.dbg("[SNPviaHTTP] http_req() TCP port opened successfully") local pollfds = {} pollfds[fd] = vlc.net.POLLIN vlc.net.send(fd, request) vlc.net.poll(pollfds) response = vlc.net.recv(fd, 1024) vlc.net.close(fd) vlc.msg.dbg("[SNPviaHTTP] http_req() vlc.net.close(fd)") return response end -- return "" end function ssplit(str) local delim = "/" local maxnb = 5 -- max number of returnable parameters if string.find(str, delim) == nil then return { str } end if maxNb == nil or maxNb < 1 then maxNb = 0 -- No limit end local result = {} local pat = "(.-)" .. delim .. "()" local nb = 0 local lastPos for part, pos in string.gfind(str, pat) do nb = nb + 1 result[nb] = part lastPos = pos if nb == maxNb then break end end -- Handle the last field if nb ~= maxNb then result[nb + 1] = string.sub(str, lastPos) end -- gotta return something for the token, if the global token is set I can just leave it --vlc.msg.dbg("[SNPviaHTTP] ssplit() is returning " .. " - " .. result[1] .. " - " .. result[2] .. " - " .. result[3] .. " - " .. result[4] .. " - " .. tostring(result[5])) if snpreged == 1 then vlc.msg.dbg("[SNPviaHTTP] ssplit() SNP has a token so make sure we don't undo it") return result[1], result[2], result[3], result[4], tonumber(gtoken) else vlc.msg.dbg("[SNPviaHTTP] ssplit() just regestered so return the new token") return result[1], result[2], result[3], result[4], tonumber(result[5]) end end -- Deactivation hook function deactivate() -- wget -q -O%tmp%\test.tmp http://%whereto%/unregister?token=%rtoken% -- Example unregister from script version if gtoken > 0 then local retval, blanktoken = get(hhost, hport, "/unregister?token=" .. gtoken ) vlc.msg.dbg("[SNPviaHTTP] deactivate() return value =" .. tostring(retval)) snpreged = 0 end vlc.msg.dbg("[SNPviaHTTP] Deactivated") end -- Function called when the input (media being read) changes function input_changed() vlc.msg.dbg("[SNPviaHTTP] input_changed ") if vlc.input.is_playing() then --if vlc.input.item():is_preparsed() then --vlc.msg.dbg("[SNPviaHTTP] Media meta data has been preparsed") --end if gtoken > 0 then local item = vlc.input.item() --:name() local meta = item:metas() local iname = item:name() -- local encitem = url_encode( tostring(meta.artist or "blank artist") .. "\n" .. tostring(meta.title or iname) ) vlc.msg.dbg("[SNPviaHTTP] name = " .. tostring(item:name()) ) --vlc.msg.dbg("[SNPviaHTTP] uri = " .. tostring(meta)) vlc.msg.dbg("[SNPviaHTTP] Just before the send ") -- wget -q -O%tmp%\test.tmp "http://%whereto%/notify?token=%rtoken%&id=1&title=my%%20Normal&text=This%%20is%%20a%%20normal%%20notify.&icon=!system-info&timeout=5&priority=-1" -- Example notification from script version local notify = "/notify?token=" .. tostring(gtoken) .. "&id=1&title=VLC%20Media%20Player&text=" .. url_encode( tostring(meta.artist or "blank artist")) .. "%5cn" .. url_encode( tostring(meta.title or iname) ) --vlc.msg.dbg("[SNPviaHTTP] input_changed - notify = " .. notify) local retval, blanktoken = get( hhost , hport, notify) vlc.msg.dbg("[SNPviaHTTP] input_changed - retval = " .. tostring(retval)) else vlc.msg.dbg("[SNPviaHTTP] input_changed - no Global token set") end vlc.msg.dbg("[SNPviaHTTP] Just after the send ") end end function meta_changed() -- vlc.msg.dbg("[SNPviaHTTP] meta_changed " ) end function playing_changed(stat) --vlc.osd.message("Status:" .. tostring(stat)) vlc.msg.dbg("[SNPviaHTTP] Status:" .. tostring(stat)) end --function start() --vlc.msg.dbg("[SNPviaHTTP] Start function") --end
Thanks in advance.

M^3

Re: VLC Extension: SNPviaHTTP notifier

Posted: 01 Apr 2012 13:10
by mederi
Have you checked http://addons.videolan.org ? There is an extension: Share on Twitter. I do not know what it exactly does, but there could be something useful for your script.

Re: VLC Extension: SNPviaHTTP notifier

Posted: 01 Apr 2012 15:11
by M^3
I have looked at a lot of other extensions but my understanding of LUA is still pretty weak. I will check this one out though, thanks!