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