podcast.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:13k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * podcast.c : podcast playlist imports
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  * $Id: 922c0a27251cfbf363c59aab13516d81374e7872 $
  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.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_demux.h>
  31. #include "playlist.h"
  32. #include "vlc_xml.h"
  33. struct demux_sys_t
  34. {
  35.     char *psz_prefix;
  36.     xml_t *p_xml;
  37.     xml_reader_t *p_xml_reader;
  38. };
  39. /*****************************************************************************
  40.  * Local prototypes
  41.  *****************************************************************************/
  42. static int Demux( demux_t *p_demux);
  43. static int Control( demux_t *p_demux, int i_query, va_list args );
  44. /*****************************************************************************
  45.  * Import_podcast: main import function
  46.  *****************************************************************************/
  47. int Import_podcast( vlc_object_t *p_this )
  48. {
  49.     demux_t *p_demux = (demux_t *)p_this;
  50.     if( !demux_IsForced( p_demux, "podcast" ) )
  51.         return VLC_EGENERIC;
  52.     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
  53.     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
  54.     p_demux->p_sys->p_xml = NULL;
  55.     p_demux->p_sys->p_xml_reader = NULL;
  56.     return VLC_SUCCESS;
  57. }
  58. /*****************************************************************************
  59.  * Deactivate: frees unused data
  60.  *****************************************************************************/
  61. void Close_podcast( vlc_object_t *p_this )
  62. {
  63.     demux_t *p_demux = (demux_t *)p_this;
  64.     demux_sys_t *p_sys = p_demux->p_sys;
  65.     free( p_sys->psz_prefix );
  66.     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
  67.     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
  68.     free( p_sys );
  69. }
  70. /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
  71. static int Demux( demux_t *p_demux )
  72. {
  73.     demux_sys_t *p_sys = p_demux->p_sys;
  74.     bool b_item = false;
  75.     bool b_image = false;
  76.     int i_ret;
  77.     xml_t *p_xml;
  78.     xml_reader_t *p_xml_reader;
  79.     char *psz_elname = NULL;
  80.     char *psz_item_mrl = NULL;
  81.     char *psz_item_size = NULL;
  82.     char *psz_item_type = NULL;
  83.     char *psz_item_name = NULL;
  84.     char *psz_item_date = NULL;
  85.     char *psz_item_author = NULL;
  86.     char *psz_item_category = NULL;
  87.     char *psz_item_duration = NULL;
  88.     char *psz_item_keywords = NULL;
  89.     char *psz_item_subtitle = NULL;
  90.     char *psz_item_summary = NULL;
  91.     int i_type;
  92.     input_item_t *p_input;
  93.     INIT_PLAYLIST_STUFF;
  94.     p_xml = p_sys->p_xml = xml_Create( p_demux );
  95.     if( !p_xml ) return -1;
  96. /*    psz_elname = stream_ReadLine( p_demux->s );
  97.     if( psz_elname ) free( psz_elname );
  98.     psz_elname = 0;*/
  99.     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
  100.     if( !p_xml_reader ) return -1;
  101.     p_sys->p_xml_reader = p_xml_reader;
  102.     /* xml */
  103.     /* check root node */
  104.     if( xml_ReaderRead( p_xml_reader ) != 1 )
  105.     {
  106.         msg_Err( p_demux, "invalid file (no root node)" );
  107.         return -1;
  108.     }
  109.     while( xml_ReaderNodeType( p_xml_reader ) == XML_READER_NONE )
  110.         if( xml_ReaderRead( p_xml_reader ) != 1 )
  111.         {
  112.             msg_Err( p_demux, "invalid file (no root node)" );
  113.             return -1;
  114.         }
  115.     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
  116.         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
  117.         strcmp( psz_elname, "rss" ) )
  118.     {
  119.         msg_Err( p_demux, "invalid root node %i, %s",
  120.                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
  121.         free( psz_elname );
  122.         return -1;
  123.     }
  124.     free( psz_elname ); psz_elname = NULL;
  125.     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
  126.     {
  127.         // Get the node type
  128.         i_type = xml_ReaderNodeType( p_xml_reader );
  129.         switch( i_type )
  130.         {
  131.             // Error
  132.             case -1:
  133.                 return -1;
  134.                 break;
  135.             case XML_READER_STARTELEM:
  136.             {
  137.                 // Read the element name
  138.                 free( psz_elname );
  139.                 psz_elname = xml_ReaderName( p_xml_reader );
  140.                 if( !psz_elname ) return -1;
  141.                 if( !strcmp( psz_elname, "item" ) )
  142.                 {
  143.                     b_item = true;
  144.                 }
  145.                 else if( !strcmp( psz_elname, "image" ) )
  146.                 {
  147.                     b_item = true;
  148.                 }
  149.                 // Read the attributes
  150.                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
  151.                 {
  152.                     char *psz_name = xml_ReaderName( p_xml_reader );
  153.                     char *psz_value = xml_ReaderValue( p_xml_reader );
  154.                     if( !psz_name || !psz_value )
  155.                     {
  156.                         free( psz_name );
  157.                         free( psz_value );
  158.                         free( psz_elname );
  159.                         return -1;
  160.                     }
  161.                     if( !strcmp( psz_elname, "enclosure" ) &&
  162.                         !strcmp( psz_name, "url" ) )
  163.                     {
  164.                         free( psz_item_mrl );
  165.                         psz_item_mrl = strdup( psz_value );
  166.                     }
  167.                     else if( !strcmp( psz_elname, "enclosure" ) &&
  168.                         !strcmp( psz_name, "length" ) )
  169.                     {
  170.                         free( psz_item_size );
  171.                         psz_item_size = strdup( psz_value );
  172.                     }
  173.                     else if( !strcmp( psz_elname, "enclosure" ) &&
  174.                         !strcmp( psz_name, "type" ) )
  175.                     {
  176.                         free( psz_item_type );
  177.                         psz_item_type = strdup( psz_value );
  178.                     }
  179.                     else
  180.                     {
  181.                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
  182.                                   psz_name, psz_elname );
  183.                     }
  184.                     free( psz_name );
  185.                     free( psz_value );
  186.                 }
  187.                 break;
  188.             }
  189.             case XML_READER_TEXT:
  190.             {
  191. #define SET_DATA( field, name ) else if( b_item == true 
  192.                 && !strcmp( psz_elname, name ) ) 
  193.                 { 
  194.                     field = strdup( psz_text ); 
  195.                 }
  196.                 char *psz_text = xml_ReaderValue( p_xml_reader );
  197.                 /* item specific meta data */
  198.                 if( b_item == true && !strcmp( psz_elname, "title" ) )
  199.                 {
  200.                     psz_item_name = strdup( psz_text );
  201.                 }
  202.                 else if( b_item == true
  203.                          && ( !strcmp( psz_elname, "itunes:author" )
  204.                             ||!strcmp( psz_elname, "author" ) ) )
  205.                 { /* <author> isn't standard iTunes podcast stuff */
  206.                     psz_item_author = strdup( psz_text );
  207.                 }
  208.                 else if( b_item == true
  209.                          && ( !strcmp( psz_elname, "itunes:summary" )
  210.                             ||!strcmp( psz_elname, "description" ) ) )
  211.                 { /* <description> isn't standard iTunes podcast stuff */
  212.                     psz_item_summary = strdup( psz_text );
  213.                 }
  214.                 SET_DATA( psz_item_date, "pubDate" )
  215.                 SET_DATA( psz_item_category, "itunes:category" )
  216.                 SET_DATA( psz_item_duration, "itunes:duration" )
  217.                 SET_DATA( psz_item_keywords, "itunes:keywords" )
  218.                 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
  219. #undef SET_DATA
  220.                 /* toplevel meta data */
  221.                 else if( b_item == false && b_image == false
  222.                          && !strcmp( psz_elname, "title" ) )
  223.                 {
  224.                     input_item_SetName( p_current_input, psz_text );
  225.                 }
  226. #define ADD_GINFO( info, name ) 
  227.     else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) 
  228.     { 
  229.         input_item_AddInfo( p_current_input, _("Podcast Info"), 
  230.                                 _( info ), "%s", psz_text ); 
  231.     }
  232.                 ADD_GINFO( "Podcast Link", "link" )
  233.                 ADD_GINFO( "Podcast Copyright", "copyright" )
  234.                 ADD_GINFO( "Podcast Category", "itunes:category" )
  235.                 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
  236.                 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
  237. #undef ADD_GINFO
  238.                 else if( b_item == false && b_image == false
  239.                          && ( !strcmp( psz_elname, "itunes:summary" )
  240.                             ||!strcmp( psz_elname, "description" ) ) )
  241.                 { /* <description> isn't standard iTunes podcast stuff */
  242.                     input_item_AddInfo( p_current_input,
  243.                              _( "Podcast Info" ), _( "Podcast Summary" ),
  244.                              "%s", psz_text );
  245.                 }
  246.                 else
  247.                 {
  248.                     msg_Dbg( p_demux, "unhandled text in element '%s'",
  249.                               psz_elname );
  250.                 }
  251.                 free( psz_text );
  252.                 break;
  253.             }
  254.             // End element
  255.             case XML_READER_ENDELEM:
  256.             {
  257.                 // Read the element name
  258.                 free( psz_elname );
  259.                 psz_elname = xml_ReaderName( p_xml_reader );
  260.                 if( !psz_elname ) return -1;
  261.                 if( !strcmp( psz_elname, "item" ) )
  262.                 {
  263.                     if( psz_item_mrl == NULL )
  264.                     {
  265.                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
  266.                         free( psz_elname );
  267.                         return -1;
  268.                     }
  269.                     p_input = input_item_New( p_demux, psz_item_mrl, psz_item_name );
  270.                     if( p_input == NULL ) break;
  271. #define ADD_INFO( info, field ) 
  272.     if( field ) { input_item_AddInfo( p_input, 
  273.                             _( "Podcast Info" ),  _( info ), "%s", field ); }
  274.                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
  275.                     ADD_INFO( "Podcast Author", psz_item_author );
  276.                     ADD_INFO( "Podcast Subcategory", psz_item_category );
  277.                     ADD_INFO( "Podcast Duration", psz_item_duration );
  278.                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
  279.                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
  280.                     ADD_INFO( "Podcast Summary", psz_item_summary );
  281.                     ADD_INFO( "Podcast Type", psz_item_type );
  282. #undef ADD_INFO
  283.                     if( psz_item_size )
  284.                     {
  285.                         input_item_AddInfo( p_input,
  286.                                                 _( "Podcast Info" ),
  287.                                                 _( "Podcast Size" ),
  288.                                                 "%s bytes",
  289.                                                 psz_item_size );
  290.                     }
  291.                     input_item_AddSubItem( p_current_input, p_input );
  292.                     vlc_gc_decref( p_input );
  293.                     FREENULL( psz_item_name );
  294.                     FREENULL( psz_item_mrl );
  295.                     FREENULL( psz_item_size );
  296.                     FREENULL( psz_item_type );
  297.                     FREENULL( psz_item_date );
  298.                     FREENULL( psz_item_author );
  299.                     FREENULL( psz_item_category );
  300.                     FREENULL( psz_item_duration );
  301.                     FREENULL( psz_item_keywords );
  302.                     FREENULL( psz_item_subtitle );
  303.                     FREENULL( psz_item_summary );
  304.                     b_item = false;
  305.                 }
  306.                 else if( !strcmp( psz_elname, "image" ) )
  307.                 {
  308.                     b_image = false;
  309.                 }
  310.                 free( psz_elname );
  311.                 psz_elname = strdup( "" );
  312.                 break;
  313.             }
  314.         }
  315.     }
  316.     if( i_ret != 0 )
  317.     {
  318.         msg_Warn( p_demux, "error while parsing data" );
  319.     }
  320.     free( psz_elname );
  321.     HANDLE_PLAY_AND_RELEASE;
  322.     return 0; /* Needed for correct operation of go back */
  323. }
  324. static int Control( demux_t *p_demux, int i_query, va_list args )
  325. {
  326.     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
  327.     return VLC_EGENERIC;
  328. }