]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
* Remove some unneeded complexity in playlist and directory
[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/input.h>
31 #include <vlc/intf.h>
32
33 #include "playlist.h"
34 #include "vlc_xml.h"
35
36 struct demux_sys_t
37 {
38     char *psz_prefix;
39     playlist_t *p_playlist;
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 E_(Import_podcast)( vlc_object_t *p_this )
54 {
55     demux_t *p_demux = (demux_t *)p_this;
56
57     if( !isDemux( p_demux, "podcast" ) )
58         return VLC_EGENERIC;
59     
60     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
61     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
62     p_demux->p_sys->p_playlist = NULL;
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 E_(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     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
78     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
79     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
80     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
81     free( p_sys );
82 }
83
84 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
85 static int Demux( demux_t *p_demux )
86 {
87     demux_sys_t *p_sys = p_demux->p_sys;
88
89     vlc_bool_t b_item = VLC_FALSE;
90     vlc_bool_t b_image = VLC_FALSE;
91     int i_ret;
92
93     xml_t *p_xml;
94     xml_reader_t *p_xml_reader;
95     char *psz_elname = NULL;
96     char *psz_item_mrl = NULL;
97     char *psz_item_size = NULL;
98     char *psz_item_type = NULL;
99     char *psz_item_name = NULL;
100     char *psz_item_date = NULL;
101     char *psz_item_author = NULL;
102     char *psz_item_category = NULL;
103     char *psz_item_duration = NULL;
104     char *psz_item_keywords = NULL;
105     char *psz_item_subtitle = NULL;
106     char *psz_item_summary = NULL;
107     int i_type;
108     input_item_t *p_input;
109
110     INIT_PLAYLIST_STUFF;
111
112     p_xml = p_sys->p_xml = xml_Create( p_demux );
113     if( !p_xml ) return -1;
114
115 /*    psz_elname = stream_ReadLine( p_demux->s );
116     if( psz_elname ) free( psz_elname );
117     psz_elname = 0;*/
118
119     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
120     if( !p_xml_reader ) return -1;
121     p_sys->p_xml_reader = p_xml_reader;
122
123     /* xml */
124     /* check root node */
125     if( xml_ReaderRead( p_xml_reader ) != 1 )
126     {
127         msg_Err( p_demux, "invalid file (no root node)" );
128         return -1;
129     }
130     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
131         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
132         strcmp( psz_elname, "rss" ) )
133     {
134         msg_Err( p_demux, "invalid root node %i, %s",
135                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
136         if( psz_elname ) free( psz_elname );
137         return -1;
138     }
139     free( psz_elname ); psz_elname = NULL;
140
141     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
142     {
143         // Get the node type
144         i_type = xml_ReaderNodeType( p_xml_reader );
145         switch( i_type )
146         {
147             // Error
148             case -1:
149                 return -1;
150                 break;
151
152             case XML_READER_STARTELEM:
153             {
154                 // Read the element name
155                 if( psz_elname ) free( psz_elname );
156                 psz_elname = xml_ReaderName( p_xml_reader );
157                 if( !psz_elname ) return -1;
158
159                 if( !strcmp( psz_elname, "item" ) )
160                 {
161                     b_item = VLC_TRUE;
162                 }
163                 else if( !strcmp( psz_elname, "image" ) )
164                 {
165                     b_item = VLC_TRUE;
166                 }
167
168                 // Read the attributes
169                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
170                 {
171                     char *psz_name = xml_ReaderName( p_xml_reader );
172                     char *psz_value = xml_ReaderValue( p_xml_reader );
173                     if( !psz_name || !psz_value ) return -1;
174                     if( !strcmp( psz_elname, "enclosure" ) &&
175                         !strcmp( psz_name, "url" ) )
176                     {
177                         psz_item_mrl = strdup( psz_value );
178                     }
179                     else if( !strcmp( psz_elname, "enclosure" ) &&
180                         !strcmp( psz_name, "length" ) )
181                     {
182                         psz_item_size = strdup( psz_value );
183                     }
184                     else if( !strcmp( psz_elname, "enclosure" ) &&
185                         !strcmp( psz_name, "type" ) )
186                     {
187                         psz_item_type = strdup( psz_value );
188                     }
189                     else
190                     {
191                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
192                                   psz_name, psz_elname );
193                     }
194                     free( psz_name );
195                     free( psz_value );
196                 }
197                 break;
198             }
199             case XML_READER_TEXT:
200             {
201 #define SET_DATA( field, name ) else if( b_item == VLC_TRUE \
202                 && !strcmp( psz_elname, name ) ) \
203                 { \
204                     field = strdup( psz_text ); \
205                 }
206                 char *psz_text = xml_ReaderValue( p_xml_reader );
207                 /* item specific meta data */
208                 if( b_item == VLC_TRUE && !strcmp( psz_elname, "title" ) )
209                 {
210                     psz_item_name = strdup( psz_text );
211                 }
212                 else if( b_item == VLC_TRUE
213                          && ( !strcmp( psz_elname, "itunes:author" )
214                             ||!strcmp( psz_elname, "author" ) ) )
215                 { /* <author> isn't standard iTunes podcast stuff */
216                     psz_item_author = strdup( psz_text );
217                 }
218                 else if( b_item == VLC_TRUE
219                          && ( !strcmp( psz_elname, "itunes:summary" )
220                             ||!strcmp( psz_elname, "description" ) ) )
221                 { /* <description> isn't standard iTunes podcast stuff */
222                     psz_item_summary = strdup( psz_text );
223                 }
224                 SET_DATA( psz_item_date, "pubDate" )
225                 SET_DATA( psz_item_category, "itunes:category" ) 
226                 SET_DATA( psz_item_duration, "itunes:duration" )
227                 SET_DATA( psz_item_keywords, "itunes:keywords" )
228                 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
229                 /* toplevel meta data */
230                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
231                          && !strcmp( psz_elname, "title" ) )
232                 {
233                     playlist_ItemSetName( p_current, psz_text );
234                 }
235 #define ADD_GINFO( info, name ) \
236     else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
237     { \
238         input_ItemAddInfo( p_current->p_input, _("Podcast Info"), \
239                                 _( info ), "%s", psz_text ); \
240     }
241                 ADD_GINFO( "Podcast Link", "link" )
242                 ADD_GINFO( "Podcast Copyright", "copyright" )
243                 ADD_GINFO( "Podcast Category", "itunes:category" )
244                 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
245                 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
246 #undef ADD_GINFO
247                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
248                          && ( !strcmp( psz_elname, "itunes:summary" )
249                             ||!strcmp( psz_elname, "description" ) ) )
250                 { /* <description> isn't standard iTunes podcast stuff */
251                     input_ItemAddInfo( p_current->p_input,
252                              _( "Podcast Info" ), _( "Podcast Summary" ),
253                              "%s", psz_text );
254                 }
255                 else
256                 {
257                     msg_Dbg( p_demux, "unhandled text in element '%s'",
258                               psz_elname );
259                 }
260                 free( psz_text );
261                 break;
262             }
263             // End element
264             case XML_READER_ENDELEM:
265             {
266                 // Read the element name
267                 free( psz_elname );
268                 psz_elname = xml_ReaderName( p_xml_reader );
269                 if( !psz_elname ) return -1;
270                 if( !strcmp( psz_elname, "item" ) )
271                 {
272                     p_input = input_ItemNewExt( p_playlist, psz_item_mrl,
273                                                 psz_item_name, 0, NULL, -1 );
274                     if( p_input == NULL ) break;
275 #define ADD_INFO( info, field ) \
276     if( field ) { input_ItemAddInfo( p_input, \
277                             _( "Podcast Info" ),  _( info ), "%s", field ); }
278                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
279                     ADD_INFO( "Podcast Author", psz_item_author );
280                     ADD_INFO( "Podcast Subcategory", psz_item_category );
281                     ADD_INFO( "Podcast Duration", psz_item_duration );
282                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
283                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
284                     ADD_INFO( "Podcast Summary", psz_item_summary );
285                     ADD_INFO( "Podcast Type", psz_item_type );
286                     if( psz_item_size )
287                     {
288                         input_ItemAddInfo( p_input,
289                                                 _( "Podcast Info" ),
290                                                 _( "Podcast Size" ),
291                                                 "%s bytes",
292                                                 psz_item_size );
293                     }
294                     playlist_BothAddInput( p_playlist, p_input,
295                                            p_item_in_category,
296                                            PLAYLIST_APPEND, PLAYLIST_END );
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 VLC_SUCCESS;
329 }
330
331 static int Control( demux_t *p_demux, int i_query, va_list args )
332 {
333     return VLC_EGENERIC;
334 }