]> git.sesse.net Git - vlc/blob - share/lua/sd/jamendo.lua
Lua: Jamendo SD + Demuxer
[vlc] / share / lua / sd / jamendo.lua
1 --[[
2  $Id$
3
4  Copyright © 2010 VideoLAN and AUTHORS
5
6  Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7
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.
12
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.
17
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.
21 --]]
22
23 require "simplexml"
24
25 function descriptor()
26     return { title="Jamendo Selections" }
27 end
28
29 function main()
30     add_top_tracks( "ratingweek_desc", nil, 100 )
31     add_top_albums( "ratingweek_desc", nil, 20 )
32     add_radio_from_id( "9", 20 )
33     add_radio_from_id( "8", 20 )
34     add_radio_from_id( "6", 20 )
35     add_radio_from_id( "5", 20 )
36     add_radio_from_id( "7", 20 )
37     add_radio_from_id( "4", 20 )
38 end
39
40 function add_top_albums( album_order, tag, max_results )
41     local url = "http://api.jamendo.com/get2/id+name+artist_name/album/xml/?order=" .. album_order .. "&n=" .. max_results
42     if tag ~= nil then
43         url = url .. "&tag_idstr=" .. tag
44     end
45     local tree = simplexml.parse_url( url )
46     local node_name = "Top " .. max_results
47     if album_order == "rating_desc" then node_name = node_name .. " most popular albums"
48     elseif album_order == "ratingmonth_desc" then node_name = node_name .. " most popular albums this month"
49     elseif album_order == "ratingweek_desc" then node_name = node_name .. " most popular albums this week"
50     elseif album_order == "releasedate_desc" then node_name = node_name .. " latest released albums"
51     elseif album_order == "downloaded_desc" then node_name = node_name .. " most downloaded albums"
52     elseif album_order == "listened_desc" then node_name = node_name .. " most listened to albums"
53     elseif album_order == "starred_desc" then node_name = node_name .. " most starred albums"
54     elseif album_order == "playlisted_desc" then node_name = node_name .. " most playlisted albums"
55     elseif album_order == "needreviews_desc" then node_name = node_name .. " albums requiring review"
56     end
57     if tag ~= nil then
58         node_name = tag .. " - " .. node_name
59     end
60     local node = vlc.sd.add_node( {title=node_name} )
61     for _, album in ipairs( tree.children ) do
62         simplexml.add_name_maps( album )
63         local album_node = node:add_node( {title=album.children_map["artist_name"][1].children[1] .. " - " .. album.children_map["name"][1].children[1],
64                                            arturl="http://imgjam.com/albums/".. album.children_map["id"][1].children[1] .. "/covers/1.500.jpg"} )
65         local tracks = get_tracks_from_album( album.children_map["id"][1].children[1] )
66         for _, track in ipairs( tracks ) do
67             album_node:add_subitem( {path="http://api.jamendo.com/get2/stream/track/redirect/?id=" .. track.id,
68                                      arturl="http://imgjam.com/albums/".. album.children_map["id"][1].children[1] .. "/covers/1.500.jpg",
69                                      title=track.title,
70                                      artist=album.children_map["artist_name"][1].children[1],
71                                      album=album.children_map["name"][1].children[1],
72                                      genre=track.genre,
73                                      duration=track.duration,
74                                      date=track.date,} ) 
75         end
76     end
77 end
78
79 function add_top_tracks( track_order, tag, max_results )
80     local url = "http://api.jamendo.com/get2/id+name+artist_name+album_name+album_id+duration+album_genre+album_dates/track/xml/track_album+album_artist/?order=" .. track_order .. "&n=" .. max_results
81     if tag ~= nil then
82         url = url .. "&tag_idstr=" .. tag
83     end
84     local tree = simplexml.parse_url( url )
85     local node_name = "Top " .. max_results
86     if track_order == "rating_desc" then node_name = node_name .. " most popular tracks"
87     elseif track_order == "ratingmonth_desc" then node_name = node_name .. " most popular tracks this month"
88     elseif track_order == "ratingweek_desc" then node_name = node_name .. " most popular tracks this week"
89     elseif track_order == "releasedate_desc" then node_name = node_name .. " latest released tracks"
90     elseif track_order == "downloaded_desc" then node_name = node_name .. " most downloaded tracks"
91     elseif track_order == "listened_desc" then node_name = node_name .. " most listened to tracks"
92     elseif track_order == "starred_desc" then node_name = node_name .. " most starred tracks"
93     elseif track_order == "playlisted_desc" then node_name = node_name .. " most playlisted tracks"
94     elseif track_order == "needreviews_desc" then node_name = node_name .. " tracks requiring review"
95     end
96     if tag ~= nil then
97         node_name = tag .. " - " .. node_name
98     end
99     local node = vlc.sd.add_node( {title=node_name} )
100     for _, track in ipairs( tree.children ) do
101         simplexml.add_name_maps( track )
102         node:add_subitem( {path="http://api.jamendo.com/get2/stream/track/redirect/?id=" .. track.children_map["id"][1].children[1],
103                            title=track.children_map["name"][1].children[1],
104                            artist=track.children_map["artist_name"][1].children[1],
105                            album=track.children_map["album_name"][1].children[1],
106                            genre=track.children_map["album_genre"][1].children[1],
107                            date=track.children_map["album_dates"][1].children_map["year"][1].children[1],
108                            arturl="http://imgjam.com/albums/".. track.children_map["album_id"][1].children[1] .. "/covers/1.500.jpg",
109                            duration=track.children_map["duration"][1].children[1]} )
110     end
111 end
112
113 function get_tracks_from_album( album )
114     local tree = simplexml.parse_url( "http://api.jamendo.com/get2/id+name+duration+album_genre+album_dates/track/xml/?album_id=" .. album )
115     local tracks = {}
116     for _, track in ipairs( tree.children ) do
117         simplexml.add_name_maps( track )
118         table.insert( tracks, {title=track.children_map["name"][1].children[1],
119                                genre=track.children_map["album_genre"][1].children[1],
120                                duration=track.children_map["duration"][1].children[1],
121                                id=track.children_map["id"][1].children[1],
122                                date=track.children_map["album_dates"][1].children_map["year"][1].children[1]} )
123     end
124     return tracks
125 end
126
127 function add_radio_from_id( id, max_results )
128     local radio_name
129     if id == "9" then radio_name="Rock"
130     elseif id == "8" then radio_name="Pop / Songwriting"
131     elseif id == "6" then radio_name="Jazz"
132     elseif id == "5" then radio_name="Hip-Hop"
133     elseif id == "7" then radio_name="Lounge"
134     elseif id == "4" then radio_name="Dance / Electro"
135     end
136     vlc.sd.add_item( {path="http://api.jamendo.com/get2/id+name+artist_name+album_name+duration+album_genre+album_dates/track/xml/radio_track_inradioplaylist+track_album+album_artist/?order=random_desc&radio_id=" .. id .. "&n=" .. max_results,
137                       title=radio_name} )
138 end
139