]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
405b51b25fc2373a23bcc6291de5baf8b6f449ec
[vlc] / modules / demux / playlist / podcast.c
1 /*****************************************************************************
2  * podcast.c : podcast playlist imports
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33
34 #include "playlist.h"
35 #include <vlc_xml.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int Demux( demux_t *p_demux);
41 static int Control( demux_t *p_demux, int i_query, va_list args );
42 static mtime_t strTimeToMTime( const char *psz );
43
44 /*****************************************************************************
45  * Import_podcast: main import function
46  *****************************************************************************/
47 int Import_podcast( vlc_object_t *p_this )
48 {
49     demux_t *p_demux = (demux_t *)p_this;
50
51     if( !demux_IsForced( p_demux, "podcast" ) )
52         return VLC_EGENERIC;
53
54     p_demux->pf_demux = Demux;
55     p_demux->pf_control = Control;
56     msg_Dbg( p_demux, "using podcast reader" );
57
58     return VLC_SUCCESS;
59 }
60
61 /*****************************************************************************
62  * Deactivate: frees unused data
63  *****************************************************************************/
64 void Close_podcast( vlc_object_t *p_this )
65 {
66     (void)p_this;
67 }
68
69 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
70 static int Demux( demux_t *p_demux )
71 {
72     bool b_item = false;
73     bool b_image = false;
74
75     xml_reader_t *p_xml_reader;
76     char *psz_elname = NULL;
77     char *psz_item_mrl = NULL;
78     char *psz_item_size = NULL;
79     char *psz_item_type = NULL;
80     char *psz_item_name = NULL;
81     char *psz_item_date = NULL;
82     char *psz_item_author = NULL;
83     char *psz_item_category = NULL;
84     char *psz_item_duration = NULL;
85     char *psz_item_keywords = NULL;
86     char *psz_item_subtitle = NULL;
87     char *psz_item_summary = NULL;
88     char *psz_art_url = NULL;
89     const char *node;
90     int i_type;
91     input_item_t *p_input;
92     input_item_node_t *p_subitems = NULL;
93
94     input_item_t *p_current_input = GetCurrentItem(p_demux);
95
96     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
97     if( !p_xml_reader )
98         goto error;
99
100     /* xml */
101     /* check root node */
102     if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
103     {
104         msg_Err( p_demux, "invalid file (no root node)" );
105         goto error;
106     }
107
108     if( strcmp( node, "rss" ) )
109     {
110         msg_Err( p_demux, "invalid root node <%s>", node );
111         goto error;
112     }
113
114     p_subitems = input_item_node_Create( p_current_input );
115
116     while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
117     {
118         switch( i_type )
119         {
120             case XML_READER_STARTELEM:
121             {
122                 free( psz_elname );
123                 psz_elname = strdup( node );
124                 if( unlikely(!node) )
125                     goto error;
126
127                 if( !strcmp( node, "item" ) )
128                     b_item = true;
129                 else if( !strcmp( node, "image" ) )
130                     b_image = true;
131
132                 // Read the attributes
133                 const char *attr;
134                 while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
135                 {
136                     char *psz_value = xml_ReaderValue( p_xml_reader );
137                     if( !psz_value )
138                     {
139                         free( psz_value );
140                         goto error;
141                     }
142
143                     if( !strcmp( node, "enclosure" ) )
144                     {
145                         if( !strcmp( attr, "url" ) )
146                         {
147                             free( psz_item_mrl );
148                             psz_item_mrl = psz_value;
149                         }
150                         else if( !strcmp( attr, "length" ) )
151                         {
152                             free( psz_item_size );
153                             psz_item_size = psz_value;
154                         }
155                         else if( !strcmp( attr, "type" ) )
156                         {
157                             free( psz_item_type );
158                             psz_item_type = psz_value;
159                         }
160                         else
161                         {
162                             msg_Dbg( p_demux,"unhandled attribute %s in element %s",
163                                      attr, node );
164                             free( psz_value );
165                         }
166                     }
167                     else
168                     {
169                         msg_Dbg( p_demux,"unhandled attribute %s in element %s",
170                                  attr, node );
171                         free( psz_value );
172                     }
173                 }
174                 break;
175             }
176
177             case XML_READER_TEXT:
178             {
179                 if(!psz_elname) break;
180
181                 /* item specific meta data */
182                 if( b_item )
183                 {
184                     char **p;
185
186                     if( !strcmp( psz_elname, "title" ) )
187                         p = &psz_item_name;
188                     else if( !strcmp( psz_elname, "itunes:author" ) ||
189                              !strcmp( psz_elname, "author" ) )
190                         /* <author> isn't standard iTunes podcast stuff */
191                         p = &psz_item_author;
192                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
193                              !strcmp( psz_elname, "description" ) )
194                         /* <description> isn't standard iTunes podcast stuff */
195                         p = &psz_item_summary;
196                     else if( !strcmp( psz_elname, "pubDate" ) )
197                         p = &psz_item_date;
198                     else if( !strcmp( psz_elname, "itunes:category" ) )
199                         p = &psz_item_category;
200                     else if( !strcmp( psz_elname, "itunes:duration" ) )
201                         p = &psz_item_duration;
202                     else if( !strcmp( psz_elname, "itunes:keywords" ) )
203                         p = &psz_item_keywords;
204                     else if( !strcmp( psz_elname, "itunes:subtitle" ) )
205                         p = &psz_item_subtitle;
206                     else
207                         break;
208
209                     free( *p );
210                     *p = strdup( node );
211                 }
212                 /* toplevel meta data */
213                 else if( !b_image )
214                 {
215                     if( !strcmp( psz_elname, "title" ) )
216                         input_item_SetName( p_current_input, node );
217 #define ADD_GINFO( info, name ) \
218     else if( !strcmp( psz_elname, name ) ) \
219         input_item_AddInfo( p_current_input, _("Podcast Info"), \
220                             info, "%s", node );
221                     ADD_GINFO( _("Podcast Link"), "link" )
222                     ADD_GINFO( _("Podcast Copyright"), "copyright" )
223                     ADD_GINFO( _("Podcast Category"), "itunes:category" )
224                     ADD_GINFO( _("Podcast Keywords"), "itunes:keywords" )
225                     ADD_GINFO( _("Podcast Subtitle"), "itunes:subtitle" )
226 #undef ADD_GINFO
227                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
228                              !strcmp( psz_elname, "description" ) )
229                     { /* <description> isn't standard iTunes podcast stuff */
230                         input_item_AddInfo( p_current_input,
231                             _( "Podcast Info" ), _( "Podcast Summary" ),
232                             "%s", node );
233                     }
234                 }
235                 else
236                 {
237                     if( !strcmp( psz_elname, "url" ) )
238                     {
239                         free( psz_art_url );
240                         psz_art_url = strdup( node );
241                     }
242                     else
243                         msg_Dbg( p_demux, "unhandled text in element <%s>",
244                                  psz_elname );
245                 }
246                 break;
247             }
248
249             // End element
250             case XML_READER_ENDELEM:
251             {
252                 FREENULL( psz_elname );
253
254                 if( !strcmp( node, "item" ) )
255                 {
256                     if( psz_item_mrl == NULL )
257                     {
258                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
259                         goto error;
260                     }
261
262                     p_input = input_item_New( p_demux, psz_item_mrl, psz_item_name );
263                     FREENULL( psz_item_mrl );
264                     FREENULL( psz_item_name );
265
266                     if( p_input == NULL )
267                         break; /* FIXME: meta data memory leaks? */
268
269                     /* Set the duration if available */
270                     if( psz_item_duration )
271                         input_item_SetDuration( p_input, strTimeToMTime( psz_item_duration ) );
272
273 #define ADD_INFO( info, field ) \
274     if( field ) { \
275         input_item_AddInfo( p_input, _( "Podcast Info" ), (info), "%s", \
276                             (field) ); \
277         FREENULL( field ); }
278                     ADD_INFO( _("Podcast Publication Date"), psz_item_date  );
279                     ADD_INFO( _("Podcast Author"), psz_item_author );
280                     ADD_INFO( _("Podcast Subcategory"), psz_item_category );
281                     ADD_INFO( _("Podcast Duration"), psz_item_duration );
282                     ADD_INFO( _("Podcast Keywords"), psz_item_keywords );
283                     ADD_INFO( _("Podcast Subtitle"), psz_item_subtitle );
284                     ADD_INFO( _("Podcast Summary"), psz_item_summary );
285                     ADD_INFO( _("Podcast Type"), psz_item_type );
286 #undef ADD_INFO
287
288                     /* Add the global art url to this item, if any */
289                     if( psz_art_url )
290                         input_item_SetArtURL( p_input, psz_art_url );
291
292                     if( psz_item_size )
293                     {
294                         input_item_AddInfo( p_input,
295                                                 _( "Podcast Info" ),
296                                                 _( "Podcast Size" ),
297                                                 _("%s bytes"),
298                                                 psz_item_size );
299                         FREENULL( psz_item_size );
300                     }
301                     input_item_node_AppendItem( p_subitems, p_input );
302                     vlc_gc_decref( p_input );
303                     b_item = false;
304                 }
305                 else if( !strcmp( psz_elname, "image" ) )
306                 {
307                     b_image = false;
308                 }
309                 break;
310             }
311         }
312     }
313
314     if( i_type < 0 )
315     {
316         msg_Warn( p_demux, "error while parsing data" );
317     }
318
319     free( psz_art_url );
320     free( psz_elname );
321     xml_ReaderDelete( p_xml_reader );
322
323     input_item_node_PostAndDelete( p_subitems );
324     vlc_gc_decref(p_current_input);
325     return 0; /* Needed for correct operation of go back */
326
327 error:
328     free( psz_item_name );
329     free( psz_item_mrl );
330     free( psz_item_size );
331     free( psz_item_type );
332     free( psz_item_date );
333     free( psz_item_author );
334     free( psz_item_category );
335     free( psz_item_duration );
336     free( psz_item_keywords );
337     free( psz_item_subtitle );
338     free( psz_item_summary );
339     free( psz_art_url );
340     free( psz_elname );
341
342     if( p_xml_reader )
343         xml_ReaderDelete( p_xml_reader );
344     if( p_subitems )
345         input_item_node_Delete( p_subitems );
346
347     vlc_gc_decref(p_current_input);
348     return -1;
349 }
350
351 static int Control( demux_t *p_demux, int i_query, va_list args )
352 {
353     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
354     return VLC_EGENERIC;
355 }
356
357 static mtime_t strTimeToMTime( const char *psz )
358 {
359     int h, m, s;
360     switch( sscanf( psz, "%u:%u:%u", &h, &m, &s ) )
361     {
362     case 3:
363         return (mtime_t)( ( h*60 + m )*60 + s ) * 1000000;
364     case 2:
365         return (mtime_t)( h*60 + m ) * 1000000;
366         break;
367     default:
368         return -1;
369     }
370 }