]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
podcast: set the duration of an input item when available.
[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 struct demux_sys_t
38 {
39     char *psz_prefix;
40     xml_t *p_xml;
41     xml_reader_t *p_xml_reader;
42 };
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
49 static mtime_t strTimeToMTime( const char *psz );
50
51 /*****************************************************************************
52  * Import_podcast: main import function
53  *****************************************************************************/
54 int Import_podcast( vlc_object_t *p_this )
55 {
56     demux_t *p_demux = (demux_t *)p_this;
57
58     if( !demux_IsForced( p_demux, "podcast" ) )
59         return VLC_EGENERIC;
60
61     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
62     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
63     p_demux->p_sys->p_xml = NULL;
64     p_demux->p_sys->p_xml_reader = NULL;
65
66     return VLC_SUCCESS;
67 }
68
69 /*****************************************************************************
70  * Deactivate: frees unused data
71  *****************************************************************************/
72 void Close_podcast( vlc_object_t *p_this )
73 {
74     demux_t *p_demux = (demux_t *)p_this;
75     demux_sys_t *p_sys = p_demux->p_sys;
76
77     free( p_sys->psz_prefix );
78     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
79     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
80     free( p_sys );
81 }
82
83 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
84 static int Demux( demux_t *p_demux )
85 {
86     demux_sys_t *p_sys = p_demux->p_sys;
87
88     bool b_item = false;
89     bool b_image = false;
90     int i_ret;
91
92     xml_t *p_xml;
93     xml_reader_t *p_xml_reader;
94     char *psz_elname = NULL;
95     char *psz_item_mrl = NULL;
96     char *psz_item_size = NULL;
97     char *psz_item_type = NULL;
98     char *psz_item_name = NULL;
99     char *psz_item_date = NULL;
100     char *psz_item_author = NULL;
101     char *psz_item_category = NULL;
102     char *psz_item_duration = NULL;
103     char *psz_item_keywords = NULL;
104     char *psz_item_subtitle = NULL;
105     char *psz_item_summary = NULL;
106     int i_type;
107     input_item_t *p_input;
108
109     INIT_PLAYLIST_STUFF;
110
111     p_xml = p_sys->p_xml = xml_Create( p_demux );
112     if( !p_xml ) return -1;
113
114 /*    psz_elname = stream_ReadLine( p_demux->s );
115     if( psz_elname ) free( psz_elname );
116     psz_elname = 0;*/
117
118     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
119     if( !p_xml_reader ) return -1;
120     p_sys->p_xml_reader = p_xml_reader;
121
122     /* xml */
123     /* check root node */
124     if( xml_ReaderRead( p_xml_reader ) != 1 )
125     {
126         msg_Err( p_demux, "invalid file (no root node)" );
127         return -1;
128     }
129
130     while( xml_ReaderNodeType( p_xml_reader ) == XML_READER_NONE )
131     {
132         if( xml_ReaderRead( p_xml_reader ) != 1 )
133         {
134             msg_Err( p_demux, "invalid file (no root node)" );
135             return -1;
136         }
137     }
138
139     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
140         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
141         strcmp( psz_elname, "rss" ) )
142     {
143         msg_Err( p_demux, "invalid root node %i, %s",
144                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
145         free( psz_elname );
146         return -1;
147     }
148     FREENULL( psz_elname );
149
150     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
151     {
152         // Get the node type
153         i_type = xml_ReaderNodeType( p_xml_reader );
154         switch( i_type )
155         {
156             // Error
157             case -1:
158                 return -1;
159                 break;
160
161             case XML_READER_STARTELEM:
162             {
163                 // Read the element name
164                 free( psz_elname );
165                 psz_elname = xml_ReaderName( p_xml_reader );
166                 if( !psz_elname ) return -1;
167
168                 if( !strcmp( psz_elname, "item" ) )
169                 {
170                     b_item = true;
171                 }
172                 else if( !strcmp( psz_elname, "image" ) )
173                 {
174                     b_item = true;
175                 }
176
177                 // Read the attributes
178                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
179                 {
180                     char *psz_name = xml_ReaderName( p_xml_reader );
181                     char *psz_value = xml_ReaderValue( p_xml_reader );
182                     if( !psz_name || !psz_value )
183                     {
184                         free( psz_name );
185                         free( psz_value );
186                         free( psz_elname );
187                         return -1;
188                     }
189
190                     if( !strcmp( psz_elname, "enclosure" ) )
191                     {
192                         if( !strcmp( psz_name, "url" ) )
193                         {
194                             free( psz_item_mrl );
195                             psz_item_mrl = strdup( psz_value );
196                         }
197                         else if( !strcmp( psz_name, "length" ) )
198                         {
199                             free( psz_item_size );
200                             psz_item_size = strdup( psz_value );
201                         }
202                         else if( !strcmp( psz_name, "type" ) )
203                         {
204                             free( psz_item_type );
205                             psz_item_type = strdup( psz_value );
206                         }
207                         else
208                             msg_Dbg( p_demux,"unhandled attribure %s in element %s",
209                                      psz_name, psz_elname );
210                     }
211                     else
212                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
213                                   psz_name, psz_elname );
214
215                     free( psz_name );
216                     free( psz_value );
217                 }
218                 break;
219             }
220             case XML_READER_TEXT:
221             {
222                 char *psz_text = xml_ReaderValue( p_xml_reader );
223
224 #define SET_DATA( field, name )                 \
225     else if( !strcmp( psz_elname, name ) )      \
226     {                                           \
227         field = psz_text;                       \
228     }
229                 /* item specific meta data */
230                 if( b_item == true )
231                 {
232                     if( !strcmp( psz_elname, "title" ) )
233                     {
234                         psz_item_name = psz_text;
235                     }
236                     else if( !strcmp( psz_elname, "itunes:author" ) ||
237                              !strcmp( psz_elname, "author" ) )
238                     { /* <author> isn't standard iTunes podcast stuff */
239                         psz_item_author = psz_text;
240                     }
241                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
242                              !strcmp( psz_elname, "description" ) )
243                     { /* <description> isn't standard iTunes podcast stuff */
244                         psz_item_summary = psz_text;
245                     }
246                     SET_DATA( psz_item_date, "pubDate" )
247                     SET_DATA( psz_item_category, "itunes:category" )
248                     SET_DATA( psz_item_duration, "itunes:duration" )
249                     SET_DATA( psz_item_keywords, "itunes:keywords" )
250                     SET_DATA( psz_item_subtitle, "itunes:subtitle" )
251                     else
252                         free( psz_text );
253                 }
254 #undef SET_DATA
255
256                 /* toplevel meta data */
257                 else if( b_image == false )
258                 {
259                     if( !strcmp( psz_elname, "title" ) )
260                     {
261                         input_item_SetName( p_current_input, psz_text );
262                     }
263 #define ADD_GINFO( info, name ) \
264     else if( !strcmp( psz_elname, name ) ) \
265     { \
266         input_item_AddInfo( p_current_input, _("Podcast Info"), \
267                                 _( info ), "%s", psz_text ); \
268     }
269                     ADD_GINFO( "Podcast Link", "link" )
270                     ADD_GINFO( "Podcast Copyright", "copyright" )
271                     ADD_GINFO( "Podcast Category", "itunes:category" )
272                     ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
273                     ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
274 #undef ADD_GINFO
275                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
276                              !strcmp( psz_elname, "description" ) )
277                     { /* <description> isn't standard iTunes podcast stuff */
278                         input_item_AddInfo( p_current_input,
279                             _( "Podcast Info" ), _( "Podcast Summary" ),
280                             "%s", psz_text );
281                     }
282                     free( psz_text );
283                 }
284                 else
285                 {
286                     msg_Dbg( p_demux, "unhandled text in element '%s'",
287                               psz_elname );
288                     free( psz_text );
289                 }
290                 break;
291             }
292             // End element
293             case XML_READER_ENDELEM:
294             {
295                 // Read the element name
296                 free( psz_elname );
297                 psz_elname = xml_ReaderName( p_xml_reader );
298                 if( !psz_elname ) return -1;
299                 if( !strcmp( psz_elname, "item" ) )
300                 {
301                     if( psz_item_mrl == NULL )
302                     {
303                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
304                         free( psz_elname );
305                         return -1;
306                     }
307                     p_input = input_item_New( p_demux, psz_item_mrl, psz_item_name );
308                     if( p_input == NULL ) break;
309 #define ADD_INFO( info, field ) \
310     if( field ) { input_item_AddInfo( p_input, \
311                             _( "Podcast Info" ),  _( info ), "%s", field ); }
312                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
313                     ADD_INFO( "Podcast Author", psz_item_author );
314                     ADD_INFO( "Podcast Subcategory", psz_item_category );
315                     ADD_INFO( "Podcast Duration", psz_item_duration );
316                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
317                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
318                     ADD_INFO( "Podcast Summary", psz_item_summary );
319                     ADD_INFO( "Podcast Type", psz_item_type );
320 #undef ADD_INFO
321
322                     /* Set the duration if available */
323                     if( psz_item_duration )
324                         input_item_SetDuration( p_input, strTimeToMTime( psz_item_duration ) );
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_AddSubItem( p_current_input, 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_elname );
367
368     HANDLE_PLAY_AND_RELEASE;
369     return 0; /* Needed for correct operation of go back */
370 }
371
372 static int Control( demux_t *p_demux, int i_query, va_list args )
373 {
374     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
375     return VLC_EGENERIC;
376 }
377
378 static mtime_t strTimeToMTime( const char *psz )
379 {
380     int h, m, s;
381     switch( sscanf( psz, "%u:%u:%u", &h, &m, &s ) )
382     {
383     case 3:
384         return (mtime_t)( ( h*60 + m )*60 + s ) * 1000000;
385     case 2:
386         return (mtime_t)( h*60 + m ) * 1000000;
387         break;
388     default:
389         return -1;
390     }
391 }