RssPlaylistPlugin.cxx 4 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "Log.hxx"
28 29

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

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

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

59 60
	TagBuilder tag_builder;

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

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

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

		break;

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

		break;
	}
}

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

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

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

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

		break;
	}
}

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

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

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

		break;
	}
}

/*
 * The playlist object
 *
 */

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

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

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

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

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

const struct playlist_plugin rss_playlist_plugin = {
171
	"rss",
172

173 174 175 176
	nullptr,
	nullptr,
	nullptr,
	rss_open_stream,
177

178 179 180
	nullptr,
	rss_suffixes,
	rss_mime_types,
181
};