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_demux.h>
38 playlist_t *p_playlist;
40 xml_reader_t *p_xml_reader;
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Demux( demux_t *p_demux);
47 static int Control( demux_t *p_demux, int i_query, va_list args );
49 /*****************************************************************************
50 * Import_podcast: main import function
51 *****************************************************************************/
52 int E_(Import_podcast)( vlc_object_t *p_this )
54 demux_t *p_demux = (demux_t *)p_this;
56 if( !demux2_IsForced( p_demux, "podcast" ) )
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;
68 /*****************************************************************************
69 * Deactivate: frees unused data
70 *****************************************************************************/
71 void E_(Close_podcast)( vlc_object_t *p_this )
73 demux_t *p_demux = (demux_t *)p_this;
74 demux_sys_t *p_sys = p_demux->p_sys;
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 );
83 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
84 static int Demux( demux_t *p_demux )
86 demux_sys_t *p_sys = p_demux->p_sys;
88 vlc_bool_t b_item = VLC_FALSE;
89 vlc_bool_t b_image = VLC_FALSE;
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;
107 input_item_t *p_input;
111 p_xml = p_sys->p_xml = xml_Create( p_demux );
112 if( !p_xml ) return -1;
114 /* psz_elname = stream_ReadLine( p_demux->s );
115 if( psz_elname ) free( psz_elname );
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;
123 /* check root node */
124 if( xml_ReaderRead( p_xml_reader ) != 1 )
126 msg_Err( p_demux, "invalid file (no root node)" );
129 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
130 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
131 strcmp( psz_elname, "rss" ) )
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 );
138 free( psz_elname ); psz_elname = NULL;
140 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
143 i_type = xml_ReaderNodeType( p_xml_reader );
151 case XML_READER_STARTELEM:
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;
158 if( !strcmp( psz_elname, "item" ) )
162 else if( !strcmp( psz_elname, "image" ) )
167 // Read the attributes
168 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
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" ) )
176 psz_item_mrl = strdup( psz_value );
178 else if( !strcmp( psz_elname, "enclosure" ) &&
179 !strcmp( psz_name, "length" ) )
181 psz_item_size = strdup( psz_value );
183 else if( !strcmp( psz_elname, "enclosure" ) &&
184 !strcmp( psz_name, "type" ) )
186 psz_item_type = strdup( psz_value );
190 msg_Dbg( p_demux,"unhandled attribure %s in element %s",
191 psz_name, psz_elname );
198 case XML_READER_TEXT:
200 #define SET_DATA( field, name ) else if( b_item == VLC_TRUE \
201 && !strcmp( psz_elname, name ) ) \
203 field = strdup( psz_text ); \
205 char *psz_text = xml_ReaderValue( p_xml_reader );
206 /* item specific meta data */
207 if( b_item == VLC_TRUE && !strcmp( psz_elname, "title" ) )
209 psz_item_name = strdup( psz_text );
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 );
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 );
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" ) )
232 playlist_ItemSetName( p_current, psz_text );
234 #define ADD_GINFO( info, name ) \
235 else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
237 input_ItemAddInfo( p_current->p_input, _("Podcast Info"), \
238 _( info ), "%s", psz_text ); \
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" )
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" ),
256 msg_Dbg( p_demux, "unhandled text in element '%s'",
263 case XML_READER_ENDELEM:
265 // Read the element name
267 psz_elname = xml_ReaderName( p_xml_reader );
268 if( !psz_elname ) return -1;
269 if( !strcmp( psz_elname, "item" ) )
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 );
287 input_ItemAddInfo( p_input,
293 playlist_BothAddInput( p_playlist, p_input,
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 );
310 else if( !strcmp( psz_elname, "image" ) )
315 psz_elname = strdup("");
324 msg_Warn( p_demux, "error while parsing data" );
327 HANDLE_PLAY_AND_RELEASE;
328 return -1; /* Needed for correct operation of go back */
331 static int Control( demux_t *p_demux, int i_query, va_list args )