]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
demux/playlist/: New "video portals" playlist demux. You feed it a url from a popular...
[vlc] / modules / demux / playlist / podcast.c
1 /*****************************************************************************
2  * podcast.c : podcast playlist imports
3  *****************************************************************************
4  * Copyright (C) 2005 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 #include <ctype.h>                                              /* isspace() */
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31
32 #include "playlist.h"
33 #include "vlc_xml.h"
34
35 struct demux_sys_t
36 {
37     char *psz_prefix;
38     playlist_t *p_playlist;
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
49 /*****************************************************************************
50  * Import_podcast: main import function
51  *****************************************************************************/
52 int E_(Import_podcast)( vlc_object_t *p_this )
53 {
54     demux_t *p_demux = (demux_t *)p_this;
55
56     if( !isDemux( p_demux, "podcast" ) )
57         return VLC_EGENERIC;
58
59     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
60     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
61     p_demux->p_sys->p_playlist = NULL;
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 E_(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     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
77     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
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     vlc_bool_t b_item = VLC_FALSE;
89     vlc_bool_t b_image = VLC_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     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
130         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
131         strcmp( psz_elname, "rss" ) )
132     {
133         msg_Err( p_demux, "invalid root node %i, %s",
134                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
135         if( psz_elname ) free( psz_elname );
136         return -1;
137     }
138     free( psz_elname ); psz_elname = NULL;
139
140     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
141     {
142         // Get the node type
143         i_type = xml_ReaderNodeType( p_xml_reader );
144         switch( i_type )
145         {
146             // Error
147             case -1:
148                 return -1;
149                 break;
150
151             case XML_READER_STARTELEM:
152             {
153                 // Read the element name
154                 if( psz_elname ) free( psz_elname );
155                 psz_elname = xml_ReaderName( p_xml_reader );
156                 if( !psz_elname ) return -1;
157
158                 if( !strcmp( psz_elname, "item" ) )
159                 {
160                     b_item = VLC_TRUE;
161                 }
162                 else if( !strcmp( psz_elname, "image" ) )
163                 {
164                     b_item = VLC_TRUE;
165                 }
166
167                 // Read the attributes
168                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
169                 {
170                     char *psz_name = xml_ReaderName( p_xml_reader );
171                     char *psz_value = xml_ReaderValue( p_xml_reader );
172                     if( !psz_name || !psz_value ) return -1;
173                     if( !strcmp( psz_elname, "enclosure" ) &&
174                         !strcmp( psz_name, "url" ) )
175                     {
176                         psz_item_mrl = strdup( psz_value );
177                     }
178                     else if( !strcmp( psz_elname, "enclosure" ) &&
179                         !strcmp( psz_name, "length" ) )
180                     {
181                         psz_item_size = strdup( psz_value );
182                     }
183                     else if( !strcmp( psz_elname, "enclosure" ) &&
184                         !strcmp( psz_name, "type" ) )
185                     {
186                         psz_item_type = strdup( psz_value );
187                     }
188                     else
189                     {
190                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
191                                   psz_name, psz_elname );
192                     }
193                     free( psz_name );
194                     free( psz_value );
195                 }
196                 break;
197             }
198             case XML_READER_TEXT:
199             {
200 #define SET_DATA( field, name ) else if( b_item == VLC_TRUE \
201                 && !strcmp( psz_elname, name ) ) \
202                 { \
203                     field = strdup( psz_text ); \
204                 }
205                 char *psz_text = xml_ReaderValue( p_xml_reader );
206                 /* item specific meta data */
207                 if( b_item == VLC_TRUE && !strcmp( psz_elname, "title" ) )
208                 {
209                     psz_item_name = strdup( psz_text );
210                 }
211                 else if( b_item == VLC_TRUE
212                          && ( !strcmp( psz_elname, "itunes:author" )
213                             ||!strcmp( psz_elname, "author" ) ) )
214                 { /* <author> isn't standard iTunes podcast stuff */
215                     psz_item_author = strdup( psz_text );
216                 }
217                 else if( b_item == VLC_TRUE
218                          && ( !strcmp( psz_elname, "itunes:summary" )
219                             ||!strcmp( psz_elname, "description" ) ) )
220                 { /* <description> isn't standard iTunes podcast stuff */
221                     psz_item_summary = strdup( psz_text );
222                 }
223                 SET_DATA( psz_item_date, "pubDate" )
224                 SET_DATA( psz_item_category, "itunes:category" ) 
225                 SET_DATA( psz_item_duration, "itunes:duration" )
226                 SET_DATA( psz_item_keywords, "itunes:keywords" )
227                 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
228                 /* toplevel meta data */
229                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
230                          && !strcmp( psz_elname, "title" ) )
231                 {
232                     playlist_ItemSetName( p_current, psz_text );
233                 }
234 #define ADD_GINFO( info, name ) \
235     else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
236     { \
237         input_ItemAddInfo( p_current->p_input, _("Podcast Info"), \
238                                 _( info ), "%s", psz_text ); \
239     }
240                 ADD_GINFO( "Podcast Link", "link" )
241                 ADD_GINFO( "Podcast Copyright", "copyright" )
242                 ADD_GINFO( "Podcast Category", "itunes:category" )
243                 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
244                 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
245 #undef ADD_GINFO
246                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
247                          && ( !strcmp( psz_elname, "itunes:summary" )
248                             ||!strcmp( psz_elname, "description" ) ) )
249                 { /* <description> isn't standard iTunes podcast stuff */
250                     input_ItemAddInfo( p_current->p_input,
251                              _( "Podcast Info" ), _( "Podcast Summary" ),
252                              "%s", psz_text );
253                 }
254                 else
255                 {
256                     msg_Dbg( p_demux, "unhandled text in element '%s'",
257                               psz_elname );
258                 }
259                 free( psz_text );
260                 break;
261             }
262             // End element
263             case XML_READER_ENDELEM:
264             {
265                 // Read the element name
266                 free( psz_elname );
267                 psz_elname = xml_ReaderName( p_xml_reader );
268                 if( !psz_elname ) return -1;
269                 if( !strcmp( psz_elname, "item" ) )
270                 {
271                     p_input = input_ItemNewExt( p_playlist, psz_item_mrl,
272                                                 psz_item_name, 0, NULL, -1 );
273                     if( p_input == NULL ) break;
274 #define ADD_INFO( info, field ) \
275     if( field ) { input_ItemAddInfo( p_input, \
276                             _( "Podcast Info" ),  _( info ), "%s", field ); }
277                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
278                     ADD_INFO( "Podcast Author", psz_item_author );
279                     ADD_INFO( "Podcast Subcategory", psz_item_category );
280                     ADD_INFO( "Podcast Duration", psz_item_duration );
281                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
282                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
283                     ADD_INFO( "Podcast Summary", psz_item_summary );
284                     ADD_INFO( "Podcast Type", psz_item_type );
285                     if( psz_item_size )
286                     {
287                         input_ItemAddInfo( p_input,
288                                                 _( "Podcast Info" ),
289                                                 _( "Podcast Size" ),
290                                                 "%s bytes",
291                                                 psz_item_size );
292                     }
293                     playlist_BothAddInput( p_playlist, p_input,
294                                            p_item_in_category,
295                                            PLAYLIST_APPEND | PLAYLIST_SPREPARSE
296                                            , PLAYLIST_END, NULL, NULL, VLC_FALSE );
297                     FREENULL( psz_item_name );
298                     FREENULL( psz_item_mrl );
299                     FREENULL( psz_item_size );
300                     FREENULL( psz_item_type );
301                     FREENULL( psz_item_date );
302                     FREENULL( psz_item_author );
303                     FREENULL( psz_item_category );
304                     FREENULL( psz_item_duration );
305                     FREENULL( psz_item_keywords );
306                     FREENULL( psz_item_subtitle );
307                     FREENULL( psz_item_summary );
308                     b_item = VLC_FALSE;
309                 }
310                 else if( !strcmp( psz_elname, "image" ) )
311                 {
312                     b_image = VLC_FALSE;
313                 }
314                 free( psz_elname );
315                 psz_elname = strdup("");
316
317                 break;
318             }
319         }
320     }
321
322     if( i_ret != 0 )
323     {
324         msg_Warn( p_demux, "error while parsing data" );
325     }
326
327     HANDLE_PLAY_AND_RELEASE;
328     return -1; /* Needed for correct operation of go back */
329 }
330
331 static int Control( demux_t *p_demux, int i_query, va_list args )
332 {
333     return VLC_EGENERIC;
334 }