4 Copyright © 2010 VideoLAN and AUTHORS
6 Authors: Julien 'Lta' BALLET <contact at lta dot io>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 config
["base_url"] = "http://api.icast.io"
24 config
["api_base_url"] = config
.base_url
.."/1"
30 if json
~= nil then return false end
32 vlc
.msg
.dbg("JSON parser lazy initialization")
33 json
= require ("dkjson")
35 -- Use vlc.stream to grab a remote json file, place it in a string,
36 -- decode it and return the decoded data.
37 json
["parse_url"] = function(url
)
38 local stream
= vlc
.stream(url
)
43 line
= stream
:readline()
48 return json
.decode(string)
52 -- VLC SD mandatory method, return the name and capabilities of this SD.
54 return { title
= "iCast Stream Directory", capabilities
= {"search"} }
57 -- Utility function to replace nils by an empty string, borrowed from icecast.lua
59 if s
== nil then return "" else return s
end
63 if roots
~= nil then return nil end
66 locals
= vlc
.sd
.add_node({title
="Local Streams"}),
67 cats
= vlc
.sd
.add_node({title
="Categories"}),
72 -- Add this station to the discovered service list.
73 function station_add(node
, station
)
74 if station
.streams
== nil or station
.streams
[1] == nil then
78 item
= node
:add_subitem({
80 path
= station
.streams
[1]["uri"],
82 ["Listing Source"] = "icast.io",
83 ["Listing Type"] = "radio",
84 ["Icecast Bitrate"] = dropnil(station
.streams
[1].bitrate
)
88 if station
.slogan
then
89 item
:set_name(station
.name
..": "..station
.slogan
)
91 if station
.genre_list
then
92 item
:set_genre(table.concat(station
.genre_list
, ", "))
94 if station
.language
then
95 item
:set_language(station
.language
)
97 if station
.logo
.medium
then
98 item
:set_arturl(config
.base_url
..station
.logo
.medium
)
102 -- Get an url to iCast's API, parse the returned stations and add them
103 -- to the list of discovered medias.
104 function stations_fetch(node
, url
)
106 vlc
.msg
.info("Fetching stations from API ("..url
..")")
108 local result
= json
.parse_url(config
.api_base_url
..url
)
110 if result
.stations
== nil then
114 for _
, station
in ipairs(result
.stations
) do
115 station_add(node
, station
)
119 -- VLC SD API - Search entry point
120 function search(query
)
121 if roots
.search
then vlc
.sd
.remove_node(roots
.search
) end
123 roots
.search
= vlc
.sd
.add_node({title
="Search Results"})
125 stations_fetch(roots
.search
, "/stations/search.json?q="..query
.."*")
128 -- VLC SD API - Main listing entry point
132 stations_fetch(roots
.locals
, "/stations/local.json")
134 vlc
.msg
.dbg("Fetching list of genre from API (/genres.json)")
135 local result
= json
.parse_url(config
.api_base_url
.."/genres.json")
136 for genre
, sub_genres
in pairs(result
.genres
) do
137 local node
= roots
.cats
:add_subnode({title
=genre
})
138 for _
, sub_genre
in ipairs(sub_genres
) do
139 local sub_node
= node
:add_subnode({title
=sub_genre
})
140 stations_fetch(sub_node
, "/stations/genre/"..sub_genre
..".json")