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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
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 Import_podcast( vlc_object_t *p_this )
55 demux_t *p_demux = (demux_t *)p_this;
57 if( !demux_IsForced( p_demux, "podcast" ) )
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;
68 /*****************************************************************************
69 * Deactivate: frees unused data
70 *****************************************************************************/
71 void 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 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 );
82 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
83 static int Demux( demux_t *p_demux )
85 demux_sys_t *p_sys = p_demux->p_sys;
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;
106 input_item_t *p_input;
110 p_xml = p_sys->p_xml = xml_Create( p_demux );
111 if( !p_xml ) return -1;
113 /* psz_elname = stream_ReadLine( p_demux->s );
114 if( psz_elname ) free( psz_elname );
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;
122 /* check root node */
123 if( xml_ReaderRead( p_xml_reader ) != 1 )
125 msg_Err( p_demux, "invalid file (no root node)" );
128 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
129 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
130 strcmp( psz_elname, "rss" ) )
132 msg_Err( p_demux, "invalid root node %i, %s",
133 xml_ReaderNodeType( p_xml_reader ), psz_elname );
137 free( psz_elname ); psz_elname = NULL;
139 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
142 i_type = xml_ReaderNodeType( p_xml_reader );
150 case XML_READER_STARTELEM:
152 // Read the element name
154 psz_elname = xml_ReaderName( p_xml_reader );
155 if( !psz_elname ) return -1;
157 if( !strcmp( psz_elname, "item" ) )
161 else if( !strcmp( psz_elname, "image" ) )
166 // Read the attributes
167 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
169 char *psz_name = xml_ReaderName( p_xml_reader );
170 char *psz_value = xml_ReaderValue( p_xml_reader );
171 if( !psz_name || !psz_value ) return -1;
172 if( !strcmp( psz_elname, "enclosure" ) &&
173 !strcmp( psz_name, "url" ) )
175 psz_item_mrl = strdup( psz_value );
177 else if( !strcmp( psz_elname, "enclosure" ) &&
178 !strcmp( psz_name, "length" ) )
180 psz_item_size = strdup( psz_value );
182 else if( !strcmp( psz_elname, "enclosure" ) &&
183 !strcmp( psz_name, "type" ) )
185 psz_item_type = strdup( psz_value );
189 msg_Dbg( p_demux,"unhandled attribure %s in element %s",
190 psz_name, psz_elname );
197 case XML_READER_TEXT:
199 #define SET_DATA( field, name ) else if( b_item == true \
200 && !strcmp( psz_elname, name ) ) \
202 field = strdup( psz_text ); \
204 char *psz_text = xml_ReaderValue( p_xml_reader );
205 /* item specific meta data */
206 if( b_item == true && !strcmp( psz_elname, "title" ) )
208 psz_item_name = strdup( psz_text );
210 else if( b_item == true
211 && ( !strcmp( psz_elname, "itunes:author" )
212 ||!strcmp( psz_elname, "author" ) ) )
213 { /* <author> isn't standard iTunes podcast stuff */
214 psz_item_author = strdup( psz_text );
216 else if( b_item == true
217 && ( !strcmp( psz_elname, "itunes:summary" )
218 ||!strcmp( psz_elname, "description" ) ) )
219 { /* <description> isn't standard iTunes podcast stuff */
220 psz_item_summary = strdup( psz_text );
222 SET_DATA( psz_item_date, "pubDate" )
223 SET_DATA( psz_item_category, "itunes:category" )
224 SET_DATA( psz_item_duration, "itunes:duration" )
225 SET_DATA( psz_item_keywords, "itunes:keywords" )
226 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
227 /* toplevel meta data */
228 else if( b_item == false && b_image == false
229 && !strcmp( psz_elname, "title" ) )
231 input_item_SetName( p_current_input, psz_text );
233 #define ADD_GINFO( info, name ) \
234 else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
236 input_ItemAddInfo( p_current_input, _("Podcast Info"), \
237 _( info ), "%s", psz_text ); \
239 ADD_GINFO( "Podcast Link", "link" )
240 ADD_GINFO( "Podcast Copyright", "copyright" )
241 ADD_GINFO( "Podcast Category", "itunes:category" )
242 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
243 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
245 else if( b_item == false && b_image == false
246 && ( !strcmp( psz_elname, "itunes:summary" )
247 ||!strcmp( psz_elname, "description" ) ) )
248 { /* <description> isn't standard iTunes podcast stuff */
249 input_ItemAddInfo( p_current_input,
250 _( "Podcast Info" ), _( "Podcast Summary" ),
255 msg_Dbg( p_demux, "unhandled text in element '%s'",
262 case XML_READER_ENDELEM:
264 // Read the element name
266 psz_elname = xml_ReaderName( p_xml_reader );
267 if( !psz_elname ) return -1;
268 if( !strcmp( psz_elname, "item" ) )
270 if( psz_item_mrl == NULL )
272 msg_Err( p_demux, "invalid XML (no enclosure markup)" );
275 p_input = input_ItemNewExt( p_demux, psz_item_mrl,
276 psz_item_name, 0, NULL, -1 );
277 if( p_input == NULL ) break;
278 #define ADD_INFO( info, field ) \
279 if( field ) { input_ItemAddInfo( p_input, \
280 _( "Podcast Info" ), _( info ), "%s", field ); }
281 ADD_INFO( "Podcast Publication Date", psz_item_date );
282 ADD_INFO( "Podcast Author", psz_item_author );
283 ADD_INFO( "Podcast Subcategory", psz_item_category );
284 ADD_INFO( "Podcast Duration", psz_item_duration );
285 ADD_INFO( "Podcast Keywords", psz_item_keywords );
286 ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
287 ADD_INFO( "Podcast Summary", psz_item_summary );
288 ADD_INFO( "Podcast Type", psz_item_type );
291 input_ItemAddInfo( p_input,
297 input_ItemAddSubItem( p_current_input, p_input );
298 vlc_gc_decref( p_input );
299 FREENULL( psz_item_name );
300 FREENULL( psz_item_mrl );
301 FREENULL( psz_item_size );
302 FREENULL( psz_item_type );
303 FREENULL( psz_item_date );
304 FREENULL( psz_item_author );
305 FREENULL( psz_item_category );
306 FREENULL( psz_item_duration );
307 FREENULL( psz_item_keywords );
308 FREENULL( psz_item_subtitle );
309 FREENULL( psz_item_summary );
312 else if( !strcmp( psz_elname, "image" ) )
317 psz_elname = strdup("");
326 msg_Warn( p_demux, "error while parsing data" );
329 HANDLE_PLAY_AND_RELEASE;
330 return 0; /* Needed for correct operation of go back */
333 static int Control( demux_t *p_demux, int i_query, va_list args )
335 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);