RssPlaylistPlugin.cxx 3.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "RssPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
23
#include "tag/Builder.hxx"
24
#include "util/ASCII.hxx"
25
#include "util/StringView.hxx"
26
#include "lib/expat/ExpatParser.hxx"
27 28

/**
29
 * This is the state object for the our XML parser.
30
 */
31
struct RssParser {
32 33 34 35
	/**
	 * The list of songs (in reverse order because that's faster
	 * while adding).
	 */
36
	std::forward_list<DetachedSong> songs;
37 38 39 40 41 42 43 44 45 46 47 48 49

	/**
	 * 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.
	 */
50
	TagType tag_type;
51 52

	/**
53
	 * The current song URI.  It is set by the "enclosure"
54 55
	 * element.
	 */
56
	std::string location;
57

58 59
	TagBuilder tag_builder;

60
	RssParser()
61
		:state(ROOT) {}
62 63
};

64 65 66
static void XMLCALL
rss_start_element(void *user_data, const XML_Char *element_name,
		  const XML_Char **atts)
67
{
68
	RssParser *parser = (RssParser *)user_data;
69 70

	switch (parser->state) {
71
	case RssParser::ROOT:
72
		if (StringEqualsCaseASCII(element_name, "item")) {
73
			parser->state = RssParser::ITEM;
74
			parser->location.clear();
75
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
76 77 78 79
		}

		break;

80
	case RssParser::ITEM:
81
		if (StringEqualsCaseASCII(element_name, "enclosure")) {
82 83
			const char *href =
				ExpatParser::GetAttributeCase(atts, "url");
84 85
			if (href != nullptr)
				parser->location = href;
86
		} else if (StringEqualsCaseASCII(element_name, "title"))
87
			parser->tag_type = TAG_TITLE;
88
		else if (StringEqualsCaseASCII(element_name, "itunes:author"))
89
			parser->tag_type = TAG_ARTIST;
90 91 92 93 94

		break;
	}
}

95 96
static void XMLCALL
rss_end_element(void *user_data, const XML_Char *element_name)
97
{
98
	RssParser *parser = (RssParser *)user_data;
99 100

	switch (parser->state) {
101
	case RssParser::ROOT:
102 103
		break;

104
	case RssParser::ITEM:
105
		if (StringEqualsCaseASCII(element_name, "item")) {
106 107 108
			if (!parser->location.empty())
				parser->songs.emplace_front(std::move(parser->location),
							    parser->tag_builder.Commit());
109

110
			parser->state = RssParser::ROOT;
111
		} else
112
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
113 114 115 116 117

		break;
	}
}

118 119
static void XMLCALL
rss_char_data(void *user_data, const XML_Char *s, int len)
120
{
121
	RssParser *parser = (RssParser *)user_data;
122 123

	switch (parser->state) {
124
	case RssParser::ROOT:
125 126
		break;

127
	case RssParser::ITEM:
128
		if (parser->tag_type != TAG_NUM_OF_ITEM_TYPES)
129 130
			parser->tag_builder.AddItem(parser->tag_type,
						    StringView(s, len));
131 132 133 134 135 136 137 138 139 140

		break;
	}
}

/*
 * The playlist object
 *
 */

141
static std::unique_ptr<SongEnumerator>
142
rss_open_stream(InputStreamPtr &&is)
143
{
144
	RssParser parser;
145

146 147 148 149
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(rss_start_element, rss_end_element);
		expat.SetCharacterDataHandler(rss_char_data);
150
		expat.Parse(*is);
151 152
	}

153
	parser.songs.reverse();
154
	return std::make_unique<MemorySongEnumerator>(std::move(parser.songs));
155 156 157 158
}

static const char *const rss_suffixes[] = {
	"rss",
159
	nullptr
160 161 162 163 164
};

static const char *const rss_mime_types[] = {
	"application/rss+xml",
	"text/xml",
165
	nullptr
166 167 168
};

const struct playlist_plugin rss_playlist_plugin = {
169
	"rss",
170

171 172 173 174
	nullptr,
	nullptr,
	nullptr,
	rss_open_stream,
175

176 177 178
	nullptr,
	rss_suffixes,
	rss_mime_types,
179
};