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