1 /*****************************************************************************
2 * podcast.c : podcast playlist imports
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <ctype.h> /* isspace() */
30 #include <vlc/input.h>
39 playlist_t *p_playlist;
41 xml_reader_t *p_xml_reader;
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
50 /*****************************************************************************
51 * Import_podcast: main import function
52 *****************************************************************************/
53 int E_(Import_podcast)( vlc_object_t *p_this )
55 demux_t *p_demux = (demux_t *)p_this;
57 if( !isDemux( p_demux, "podcast" ) )
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;
69 /*****************************************************************************
70 * Deactivate: frees unused data
71 *****************************************************************************/
72 void E_(Close_podcast)( vlc_object_t *p_this )
74 demux_t *p_demux = (demux_t *)p_this;
75 demux_sys_t *p_sys = p_demux->p_sys;
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 );
84 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
85 static int Demux( demux_t *p_demux )
87 demux_sys_t *p_sys = p_demux->p_sys;
89 vlc_bool_t b_item = VLC_FALSE;
90 vlc_bool_t b_image = VLC_FALSE;
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;
108 input_item_t *p_input;
112 p_xml = p_sys->p_xml = xml_Create( p_demux );
113 if( !p_xml ) return -1;
115 /* psz_elname = stream_ReadLine( p_demux->s );
116 if( psz_elname ) free( psz_elname );
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;
124 /* check root node */
125 if( xml_ReaderRead( p_xml_reader ) != 1 )
127 msg_Err( p_demux, "invalid file (no root node)" );
130 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
131 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
132 strcmp( psz_elname, "rss" ) )
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 );
139 free( psz_elname ); psz_elname = NULL;
141 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
144 i_type = xml_ReaderNodeType( p_xml_reader );
152 case XML_READER_STARTELEM:
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;
159 if( !strcmp( psz_elname, "item" ) )
163 else if( !strcmp( psz_elname, "image" ) )
168 // Read the attributes
169 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
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" ) )
177 psz_item_mrl = strdup( psz_value );
179 else if( !strcmp( psz_elname, "enclosure" ) &&
180 !strcmp( psz_name, "length" ) )
182 psz_item_size = strdup( psz_value );
184 else if( !strcmp( psz_elname, "enclosure" ) &&
185 !strcmp( psz_name, "type" ) )
187 psz_item_type = strdup( psz_value );
191 msg_Dbg( p_demux,"unhandled attribure %s in element %s",
192 psz_name, psz_elname );
199 case XML_READER_TEXT:
201 #define SET_DATA( field, name ) else if( b_item == VLC_TRUE \
202 && !strcmp( psz_elname, name ) ) \
204 field = strdup( psz_text ); \
206 char *psz_text = xml_ReaderValue( p_xml_reader );
207 /* item specific meta data */
208 if( b_item == VLC_TRUE && !strcmp( psz_elname, "title" ) )
210 psz_item_name = strdup( psz_text );
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 );
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 );
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" ) )
233 playlist_ItemSetName( p_current, psz_text );
235 #define ADD_GINFO( info, name ) \
236 else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
238 input_ItemAddInfo( p_current->p_input, _("Podcast Info"), \
239 _( info ), "%s", psz_text ); \
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" )
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" ),
257 msg_Dbg( p_demux, "unhandled text in element '%s'",
264 case XML_READER_ENDELEM:
266 // Read the element name
268 psz_elname = xml_ReaderName( p_xml_reader );
269 if( !psz_elname ) return -1;
270 if( !strcmp( psz_elname, "item" ) )
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 );
288 input_ItemAddInfo( p_input,
294 playlist_BothAddInput( p_playlist, p_input,
296 PLAYLIST_APPEND | PLAYLIST_SPREPARSE
298 FREENULL( psz_item_name );
299 FREENULL( psz_item_mrl );
300 FREENULL( psz_item_size );
301 FREENULL( psz_item_type );
302 FREENULL( psz_item_date );
303 FREENULL( psz_item_author );
304 FREENULL( psz_item_category );
305 FREENULL( psz_item_duration );
306 FREENULL( psz_item_keywords );
307 FREENULL( psz_item_subtitle );
308 FREENULL( psz_item_summary );
311 else if( !strcmp( psz_elname, "image" ) )
316 psz_elname = strdup("");
325 msg_Warn( p_demux, "error while parsing data" );
328 HANDLE_PLAY_AND_RELEASE;
329 return -1; /* Needed for correct operation of go back */
332 static int Control( demux_t *p_demux, int i_query, va_list args )