]> git.sesse.net Git - vlc/blob - share/lua/sd/librivox.lua
librivox: create a node for each book of the podcast.
[vlc] / share / lua / sd / librivox.lua
1 --[[
2  $Id$
3
4  Copyright © 2010 VideoLAN and AUTHORS
5
6  Authors: Rémi Duraffort  <ivoire 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 = 'librivox' }
27 end
28
29 function string_2_duration(str)
30     local index = string.find( str, ':' )
31     if( index == nil ) then return str
32     else
33         local index2 = string.find( str, ':', index + 1 )
34         if( index2 == nil ) then
35             return string.sub( str, 0, index - 1 ) * 60 + string.sub( str, index + 1 )
36         else
37             return string.sub( str, 0, index - 1 ) * 3600 + string.sub( str, index + 1, index2 - 1 ) * 60 + string.sub( str, index2 + 1 )
38         end
39     end
40 end
41
42 function main()
43     local podcast = simplexml.parse_url( 'http://librivox.org/podcast.xml' )
44
45     simplexml.add_name_maps( podcast )
46     local channel = podcast.children_map['channel'][1]
47     local arturl = ''
48     local books = {}
49
50     for _, item in ipairs( channel.children ) do
51         if( item.name == 'item' )
52         then
53             simplexml.add_name_maps( item )
54             local book_title = item.children_map['itunes:subtitle'][1].children[1]
55             if(books[book_title] == nil) then
56                 books[book_title] = vlc.sd.add_node( { title = book_title } )
57             end
58             books[book_title]:add_subitem( { path = item.children_map['link'][1].children[1],
59                                              title = item.children_map['title'][1].children[1],
60                                              album = item.children_map['itunes:subtitle'][1].children[1],
61                                              duration = string_2_duration( item.children_map['itunes:duration'][1].children[1] ),
62                                              arturl = arturl } )
63         elseif( item.name == 'itunes:image' )
64         then
65             arturl = item.attributes['href']
66         end
67     end
68 end
69