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