]> git.sesse.net Git - vlc/blob - share/lua/sd/librivox.lua
librivox: some cleanup and add the book author as artist of the item.
[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 -- Transform a duration 'mm:ss' or 'hh::mm::ss' into an integer
30 function string_2_duration(str)
31     local index = string.find( str, ':' )
32     if( index == nil ) then return str
33     else
34         local index2 = string.find( str, ':', index + 1 )
35         if( index2 == nil ) then
36             return string.sub( str, 0, index - 1 ) * 60 + string.sub( str, index + 1 )
37         else
38             return string.sub( str, 0, index - 1 ) * 3600 + string.sub( str, index + 1, index2 - 1 ) * 60 + string.sub( str, index2 + 1 )
39         end
40     end
41 end
42
43 function main()
44     local podcast = simplexml.parse_url( 'http://librivox.org/podcast.xml' )
45     simplexml.add_name_maps( podcast )
46
47     local channel = podcast.children_map['channel'][1]
48     local arturl = ''
49     local books = {}
50
51     for _, item in ipairs( channel.children ) do
52         if( item.name == 'item' ) then
53             simplexml.add_name_maps( item )
54
55             -- If the book title is unknown, create a node for it in the sd
56             local book_title = item.children_map['itunes:subtitle'][1].children[1]
57             if( books[book_title] == nil ) then
58                 books[book_title] = vlc.sd.add_node( { title = book_title,
59                                                        arturl = arturl } )
60             end
61
62             -- Add the new chapter to the book
63             books[book_title]:add_subitem( { path = item.children_map['link'][1].children[1],
64                                              title = item.children_map['title'][1].children[1],
65                                              album = item.children_map['itunes:subtitle'][1].children[1],
66                                              artist = item.children_map['itunes:author'][1].children[1],
67                                              duration = string_2_duration( item.children_map['itunes:duration'][1].children[1] ),
68                                              arturl = arturl } )
69
70         elseif( item.name == 'itunes:image' ) then
71             arturl = item.attributes['href']
72         end
73     end
74 end
75