2010-10-11 20:25:05 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2010-10-11 20:25:05 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-01-27 17:38:09 +01:00
|
|
|
#include "RssPlaylistPlugin.hxx"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "../PlaylistPlugin.hxx"
|
|
|
|
#include "../MemorySongEnumerator.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Builder.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2015-09-30 22:03:01 +02:00
|
|
|
#include "util/StringView.hxx"
|
2014-01-24 16:26:42 +01:00
|
|
|
#include "lib/expat/ExpatParser.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-04 23:33:08 +01:00
|
|
|
* This is the state object for the our XML parser.
|
2010-10-11 20:25:05 +02:00
|
|
|
*/
|
2013-01-27 17:38:09 +01:00
|
|
|
struct RssParser {
|
2010-10-11 20:25:05 +02:00
|
|
|
/**
|
|
|
|
* The list of songs (in reverse order because that's faster
|
|
|
|
* while adding).
|
|
|
|
*/
|
2014-01-07 21:39:47 +01:00
|
|
|
std::forward_list<DetachedSong> songs;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current position in the XML file.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
ROOT, ITEM,
|
|
|
|
} state;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current tag within the "entry" element. This is only
|
|
|
|
* valid if state==ITEM. TAG_NUM_OF_ITEM_TYPES means there
|
|
|
|
* is no (known) tag.
|
|
|
|
*/
|
2013-12-03 12:24:25 +01:00
|
|
|
TagType tag_type;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
/**
|
2014-01-07 21:39:47 +01:00
|
|
|
* The current song URI. It is set by the "enclosure"
|
2010-10-11 20:25:05 +02:00
|
|
|
* element.
|
|
|
|
*/
|
2014-01-07 21:39:47 +01:00
|
|
|
std::string location;
|
2013-01-27 17:38:09 +01:00
|
|
|
|
2013-12-03 12:25:22 +01:00
|
|
|
TagBuilder tag_builder;
|
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
RssParser()
|
2013-01-29 18:51:40 +01:00
|
|
|
:state(ROOT) {}
|
2010-10-11 20:25:05 +02:00
|
|
|
};
|
|
|
|
|
2014-01-09 11:57:47 +01:00
|
|
|
static void XMLCALL
|
|
|
|
rss_start_element(void *user_data, const XML_Char *element_name,
|
|
|
|
const XML_Char **atts)
|
2010-10-11 20:25:05 +02:00
|
|
|
{
|
2013-01-27 17:38:09 +01:00
|
|
|
RssParser *parser = (RssParser *)user_data;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
switch (parser->state) {
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ROOT:
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(element_name, "item")) {
|
2013-01-27 17:38:09 +01:00
|
|
|
parser->state = RssParser::ITEM;
|
2014-01-07 21:39:47 +01:00
|
|
|
parser->location.clear();
|
2013-12-03 12:24:25 +01:00
|
|
|
parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
|
2010-10-11 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ITEM:
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(element_name, "enclosure")) {
|
2014-01-09 11:57:47 +01:00
|
|
|
const char *href =
|
|
|
|
ExpatParser::GetAttributeCase(atts, "url");
|
2014-01-07 21:39:47 +01:00
|
|
|
if (href != nullptr)
|
|
|
|
parser->location = href;
|
2013-10-20 23:09:51 +02:00
|
|
|
} else if (StringEqualsCaseASCII(element_name, "title"))
|
2013-12-03 12:24:25 +01:00
|
|
|
parser->tag_type = TAG_TITLE;
|
2013-10-20 23:09:51 +02:00
|
|
|
else if (StringEqualsCaseASCII(element_name, "itunes:author"))
|
2013-12-03 12:24:25 +01:00
|
|
|
parser->tag_type = TAG_ARTIST;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 11:57:47 +01:00
|
|
|
static void XMLCALL
|
|
|
|
rss_end_element(void *user_data, const XML_Char *element_name)
|
2010-10-11 20:25:05 +02:00
|
|
|
{
|
2013-01-27 17:38:09 +01:00
|
|
|
RssParser *parser = (RssParser *)user_data;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
switch (parser->state) {
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ROOT:
|
2010-10-11 20:25:05 +02:00
|
|
|
break;
|
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ITEM:
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(element_name, "item")) {
|
2014-01-07 21:39:47 +01:00
|
|
|
if (!parser->location.empty())
|
|
|
|
parser->songs.emplace_front(std::move(parser->location),
|
|
|
|
parser->tag_builder.Commit());
|
2010-10-11 20:25:05 +02:00
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
parser->state = RssParser::ROOT;
|
2010-10-11 20:25:05 +02:00
|
|
|
} else
|
2013-12-03 12:24:25 +01:00
|
|
|
parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 11:57:47 +01:00
|
|
|
static void XMLCALL
|
|
|
|
rss_char_data(void *user_data, const XML_Char *s, int len)
|
2010-10-11 20:25:05 +02:00
|
|
|
{
|
2013-01-27 17:38:09 +01:00
|
|
|
RssParser *parser = (RssParser *)user_data;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
switch (parser->state) {
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ROOT:
|
2010-10-11 20:25:05 +02:00
|
|
|
break;
|
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
case RssParser::ITEM:
|
2013-12-03 12:25:22 +01:00
|
|
|
if (parser->tag_type != TAG_NUM_OF_ITEM_TYPES)
|
2015-09-30 22:03:01 +02:00
|
|
|
parser->tag_builder.AddItem(parser->tag_type,
|
|
|
|
StringView(s, len));
|
2010-10-11 20:25:05 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The playlist object
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-01-20 19:56:44 +01:00
|
|
|
static std::unique_ptr<SongEnumerator>
|
2016-02-21 12:53:47 +01:00
|
|
|
rss_open_stream(InputStreamPtr &&is)
|
2010-10-11 20:25:05 +02:00
|
|
|
{
|
2013-01-27 17:38:09 +01:00
|
|
|
RssParser parser;
|
2010-10-11 20:25:05 +02:00
|
|
|
|
2014-01-09 11:57:47 +01:00
|
|
|
{
|
|
|
|
ExpatParser expat(&parser);
|
|
|
|
expat.SetElementHandler(rss_start_element, rss_end_element);
|
|
|
|
expat.SetCharacterDataHandler(rss_char_data);
|
2016-09-09 18:47:42 +02:00
|
|
|
expat.Parse(*is);
|
2010-10-11 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:40 +01:00
|
|
|
parser.songs.reverse();
|
2018-01-20 19:56:44 +01:00
|
|
|
return std::make_unique<MemorySongEnumerator>(std::move(parser.songs));
|
2010-10-11 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const rss_suffixes[] = {
|
|
|
|
"rss",
|
2013-10-28 23:58:17 +01:00
|
|
|
nullptr
|
2010-10-11 20:25:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const rss_mime_types[] = {
|
|
|
|
"application/rss+xml",
|
|
|
|
"text/xml",
|
2013-10-28 23:58:17 +01:00
|
|
|
nullptr
|
2010-10-11 20:25:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct playlist_plugin rss_playlist_plugin = {
|
2013-01-27 17:38:09 +01:00
|
|
|
"rss",
|
2010-10-11 20:25:05 +02:00
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
rss_open_stream,
|
2010-10-11 20:25:05 +02:00
|
|
|
|
2013-01-27 17:38:09 +01:00
|
|
|
nullptr,
|
|
|
|
rss_suffixes,
|
|
|
|
rss_mime_types,
|
2010-10-11 20:25:05 +02:00
|
|
|
};
|