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