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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
--[[
Copyright © 2010 VideoLAN and AUTHORS
Authors: Julien 'Lta' BALLET <contact at lta dot io>
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
local config = {}
config["base_url"] = "http://api.icast.io"
config["api_base_url"] = config.base_url.."/1"
local json = nil
local roots = nil
function json_init()
if json ~= nil then return false end
vlc.msg.dbg("JSON parser lazy initialization")
json = require ("dkjson")
-- Use vlc.stream to grab a remote json file, place it in a string,
-- decode it and return the decoded data.
json["parse_url"] = function(url)
local stream = vlc.stream(url)
local string = ""
local line = ""
repeat
line = stream:readline()
string = string..line
until line ~= nil
--print(string)
return json.decode(string)
end
end
-- VLC SD mandatory method, return the name and capabilities of this SD.
function descriptor()
return { title = "iCast Stream Directory", capabilities = {"search"} }
end
-- Utility function to replace nils by an empty string, borrowed from icecast.lua
function dropnil(s)
if s == nil then return "" else return s end
end
function roots_init()
if roots ~= nil then return nil end
roots = {
locals = vlc.sd.add_node({title="Local Streams"}),
cats = vlc.sd.add_node({title="Categories"}),
search = nil
}
end
-- Add this station to the discovered service list.
function station_add(node, station)
if station.streams == nil or station.streams[1] == nil then
return nil
end
item = node:add_subitem({
title = station.name,
path = station.streams[1]["uri"],
meta = {
["Listing Source"] = "icast.io",
["Listing Type"] = "radio",
["Icecast Bitrate"] = dropnil(station.streams[1].bitrate)
}
})
if station.slogan then
item:set_name(station.name..": "..station.slogan)
end
if station.genre_list then
item:set_genre(table.concat(station.genre_list, ", "))
end
if station.language then
item:set_language(station.language)
end
if station.logo.medium then
item:set_arturl(config.base_url..station.logo.medium)
end
end
-- Get an url to iCast's API, parse the returned stations and add them
-- to the list of discovered medias.
function stations_fetch(node, url)
json_init()
vlc.msg.info("Fetching stations from API ("..url..")")
local result = json.parse_url(config.api_base_url..url)
if result.stations == nil then
return nil
end
for _, station in ipairs(result.stations) do
station_add(node, station)
end
end
-- VLC SD API - Search entry point
function search(query)
if roots.search then vlc.sd.remove_node(roots.search) end
roots.search = vlc.sd.add_node({title="Search Results"})
stations_fetch(roots.search, "/stations/search.json?q="..query.."*")
end
-- VLC SD API - Main listing entry point
function main()
roots_init()
stations_fetch(roots.locals, "/stations/local.json")
vlc.msg.dbg("Fetching list of genre from API (/genres.json)")
local result = json.parse_url(config.api_base_url.."/genres.json")
for genre, sub_genres in pairs(result.genres) do
local node = roots.cats:add_subnode({title=genre})
for _, sub_genre in ipairs(sub_genres) do
local sub_node = node:add_subnode({title=sub_genre})
stations_fetch(sub_node, "/stations/genre/"..sub_genre..".json")
end
end
end
|