]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
input_item: Remove input_item_AddSubItem2 and send subitem_added event from input_ite...
[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     int i_ret;
75
76     xml_t *p_xml;
77     xml_reader_t *p_xml_reader = NULL;
78     char *psz_elname = NULL;
79     char *psz_item_mrl = NULL;
80     char *psz_item_size = NULL;
81     char *psz_item_type = NULL;
82     char *psz_item_name = NULL;
83     char *psz_item_date = NULL;
84     char *psz_item_author = NULL;
85     char *psz_item_category = NULL;
86     char *psz_item_duration = NULL;
87     char *psz_item_keywords = NULL;
88     char *psz_item_subtitle = NULL;
89     char *psz_item_summary = NULL;
90     char *psz_art_url = NULL;
91     int i_type;
92     input_item_t *p_input;
93     input_item_node_t *p_subitems = NULL;
94
95     input_item_t *p_current_input = GetCurrentItem(p_demux);
96
97     p_xml = xml_Create( p_demux );
98     if( !p_xml )
99         goto error;
100
101     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
102     if( !p_xml_reader )
103         goto error;
104
105     /* xml */
106     /* check root node */
107     if( xml_ReaderRead( p_xml_reader ) != 1 )
108     {
109         msg_Err( p_demux, "invalid file (no root node)" );
110         goto error;
111     }
112
113     while( xml_ReaderNodeType( p_xml_reader ) == XML_READER_NONE )
114     {
115         if( xml_ReaderRead( p_xml_reader ) != 1 )
116         {
117             msg_Err( p_demux, "invalid file (no root node)" );
118             goto error;
119         }
120     }
121
122     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
123         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
124         strcmp( psz_elname, "rss" ) )
125     {
126         msg_Err( p_demux, "invalid root node %i, %s",
127                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
128         goto error;
129     }
130     FREENULL( psz_elname );
131
132     p_subitems = input_item_node_Create( p_current_input );
133
134     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
135     {
136         // Get the node type
137         i_type = xml_ReaderNodeType( p_xml_reader );
138         switch( i_type )
139         {
140             // Error
141             case -1:
142                 goto error;
143
144             case XML_READER_STARTELEM:
145             {
146                 // Read the element name
147                 free( psz_elname );
148                 psz_elname = xml_ReaderName( p_xml_reader );
149                 if( !psz_elname )
150                     goto error;
151
152                 if( !strcmp( psz_elname, "item" ) )
153                 {
154                     b_item = true;
155                 }
156                 else if( !strcmp( psz_elname, "image" ) )
157                 {
158                     b_image = true;
159                 }
160
161                 // Read the attributes
162                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
163                 {
164                     char *psz_name = xml_ReaderName( p_xml_reader );
165                     char *psz_value = xml_ReaderValue( p_xml_reader );
166                     if( !psz_name || !psz_value )
167                     {
168                         free( psz_name );
169                         free( psz_value );
170                         goto error;
171                     }
172
173                     if( !strcmp( psz_elname, "enclosure" ) )
174                     {
175                         if( !strcmp( psz_name, "url" ) )
176                         {
177                             free( psz_item_mrl );
178                             psz_item_mrl = psz_value;
179                         }
180                         else if( !strcmp( psz_name, "length" ) )
181                         {
182                             free( psz_item_size );
183                             psz_item_size = psz_value;
184                         }
185                         else if( !strcmp( psz_name, "type" ) )
186                         {
187                             free( psz_item_type );
188                             psz_item_type = psz_value;
189                         }
190                         else
191                         {
192                             msg_Dbg( p_demux,"unhandled attribure %s in element %s",
193                                      psz_name, psz_elname );
194                             free( psz_value );
195                         }
196                     }
197                     else
198                     {
199                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
200                                   psz_name, psz_elname );
201                         free( psz_value );
202                     }
203                     free( psz_name );
204                 }
205                 break;
206             }
207             case XML_READER_TEXT:
208             {
209                 if(!psz_elname) break;
210
211                 char *psz_text = xml_ReaderValue( p_xml_reader );
212
213 #define SET_DATA( field, name )                 \
214     else if( !strcmp( psz_elname, name ) )      \
215     {                                           \
216         field = psz_text;                       \
217     }
218                 /* item specific meta data */
219                 if( b_item == true )
220                 {
221                     if( !strcmp( psz_elname, "title" ) )
222                     {
223                         psz_item_name = psz_text;
224                     }
225                     else if( !strcmp( psz_elname, "itunes:author" ) ||
226                              !strcmp( psz_elname, "author" ) )
227                     { /* <author> isn't standard iTunes podcast stuff */
228                         psz_item_author = psz_text;
229                     }
230                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
231                              !strcmp( psz_elname, "description" ) )
232                     { /* <description> isn't standard iTunes podcast stuff */
233                         psz_item_summary = psz_text;
234                     }
235                     SET_DATA( psz_item_date, "pubDate" )
236                     SET_DATA( psz_item_category, "itunes:category" )
237                     SET_DATA( psz_item_duration, "itunes:duration" )
238                     SET_DATA( psz_item_keywords, "itunes:keywords" )
239                     SET_DATA( psz_item_subtitle, "itunes:subtitle" )
240                     else
241                         free( psz_text );
242                 }
243 #undef SET_DATA
244
245                 /* toplevel meta data */
246                 else if( b_image == false )
247                 {
248                     if( !strcmp( psz_elname, "title" ) )
249                     {
250                         input_item_SetName( p_current_input, psz_text );
251                     }
252 #define ADD_GINFO( info, name ) \
253     else if( !strcmp( psz_elname, name ) ) \
254     { \
255         input_item_AddInfo( p_current_input, _("Podcast Info"), \
256                                 _( info ), "%s", psz_text ); \
257     }
258                     ADD_GINFO( "Podcast Link", "link" )
259                     ADD_GINFO( "Podcast Copyright", "copyright" )
260                     ADD_GINFO( "Podcast Category", "itunes:category" )
261                     ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
262                     ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
263 #undef ADD_GINFO
264                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
265                              !strcmp( psz_elname, "description" ) )
266                     { /* <description> isn't standard iTunes podcast stuff */
267                         input_item_AddInfo( p_current_input,
268                             _( "Podcast Info" ), _( "Podcast Summary" ),
269                             "%s", psz_text );
270                     }
271                     free( psz_text );
272                 }
273                 else
274                 {
275                     if( !strcmp( psz_elname, "url" ) )
276                     {
277                         free( psz_art_url );
278                         psz_art_url = psz_text;
279                     }
280                     else
281                     {
282                         msg_Dbg( p_demux, "unhandled text in element '%s'",
283                                  psz_elname );
284                         free( psz_text );
285                     }
286                 }
287                 break;
288             }
289             // End element
290             case XML_READER_ENDELEM:
291             {
292                 // Read the element name
293                 free( psz_elname );
294                 psz_elname = xml_ReaderName( p_xml_reader );
295                 if( !psz_elname )
296                     goto error;
297                 if( !strcmp( psz_elname, "item" ) )
298                 {
299                     if( psz_item_mrl == NULL )
300                     {
301                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
302                         goto error;
303                     }
304                     p_input = input_item_New( p_demux, psz_item_mrl, psz_item_name );
305                     if( p_input == NULL ) break;
306 #define ADD_INFO( info, field ) \
307     if( field ) { input_item_AddInfo( p_input, \
308                             _( "Podcast Info" ),  _( info ), "%s", field ); }
309                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
310                     ADD_INFO( "Podcast Author", psz_item_author );
311                     ADD_INFO( "Podcast Subcategory", psz_item_category );
312                     ADD_INFO( "Podcast Duration", psz_item_duration );
313                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
314                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
315                     ADD_INFO( "Podcast Summary", psz_item_summary );
316                     ADD_INFO( "Podcast Type", psz_item_type );
317 #undef ADD_INFO
318
319                     /* Set the duration if available */
320                     if( psz_item_duration )
321                         input_item_SetDuration( p_input, strTimeToMTime( psz_item_duration ) );
322                     /* Add the global art url to this item, if any */
323                     if( psz_art_url )
324                         input_item_SetArtURL( p_input, psz_art_url );
325
326                     if( psz_item_size )
327                     {
328                         input_item_AddInfo( p_input,
329                                                 _( "Podcast Info" ),
330                                                 _( "Podcast Size" ),
331                                                 "%s bytes",
332                                                 psz_item_size );
333                     }
334                     input_item_node_AppendItem( p_subitems, p_input );
335                     vlc_gc_decref( p_input );
336                     FREENULL( psz_item_name );
337                     FREENULL( psz_item_mrl );
338                     FREENULL( psz_item_size );
339                     FREENULL( psz_item_type );
340                     FREENULL( psz_item_date );
341                     FREENULL( psz_item_author );
342                     FREENULL( psz_item_category );
343                     FREENULL( psz_item_duration );
344                     FREENULL( psz_item_keywords );
345                     FREENULL( psz_item_subtitle );
346                     FREENULL( psz_item_summary );
347                     b_item = false;
348                 }
349                 else if( !strcmp( psz_elname, "image" ) )
350                 {
351                     b_image = false;
352                 }
353                 free( psz_elname );
354                 psz_elname = strdup( "" );
355
356                 break;
357             }
358         }
359     }
360
361     if( i_ret != 0 )
362     {
363         msg_Warn( p_demux, "error while parsing data" );
364     }
365
366     free( psz_art_url );
367     free( psz_elname );
368     xml_ReaderDelete( p_xml, p_xml_reader );
369     xml_Delete( p_xml );
370
371     input_item_AddSubItemTree( p_subitems );
372     input_item_node_Delete( p_subitems );
373     vlc_gc_decref(p_current_input);
374     return 0; /* Needed for correct operation of go back */
375
376 error:
377     free( psz_item_name );
378     free( psz_item_mrl );
379     free( psz_item_size );
380     free( psz_item_type );
381     free( psz_item_date );
382     free( psz_item_author );
383     free( psz_item_category );
384     free( psz_item_duration );
385     free( psz_item_keywords );
386     free( psz_item_subtitle );
387     free( psz_item_summary );
388     free( psz_art_url );
389     free( psz_elname );
390
391     if( p_xml_reader )
392         xml_ReaderDelete( p_xml, p_xml_reader );
393     if( p_xml )
394         xml_Delete( p_xml );
395     if( p_subitems )
396     {
397         input_item_node_Delete( p_subitems );
398     }
399
400     vlc_gc_decref(p_current_input);
401     return -1;
402 }
403
404 static int Control( demux_t *p_demux, int i_query, va_list args )
405 {
406     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
407     return VLC_EGENERIC;
408 }
409
410 static mtime_t strTimeToMTime( const char *psz )
411 {
412     int h, m, s;
413     switch( sscanf( psz, "%u:%u:%u", &h, &m, &s ) )
414     {
415     case 3:
416         return (mtime_t)( ( h*60 + m )*60 + s ) * 1000000;
417     case 2:
418         return (mtime_t)( h*60 + m ) * 1000000;
419         break;
420     default:
421         return -1;
422     }
423 }