2
* Copyright (C) 2003-2010 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "playlist/rss_playlist_plugin.h"
22
#include "playlist_plugin.h"
23
#include "input_stream.h"
33
#define G_LOG_DOMAIN "rss"
36
* This is the state object for the GLib XML parser.
40
* The list of songs (in reverse order because that's faster
46
* The current position in the XML file.
53
* The current tag within the "entry" element. This is only
54
* valid if state==ITEM. TAG_NUM_OF_ITEM_TYPES means there
60
* The current song. It is allocated after the "location"
67
get_attribute(const gchar **attribute_names, const gchar **attribute_values,
70
for (unsigned i = 0; attribute_names[i] != NULL; ++i)
71
if (g_ascii_strcasecmp(attribute_names[i], name) == 0)
72
return attribute_values[i];
78
rss_start_element(G_GNUC_UNUSED GMarkupParseContext *context,
79
const gchar *element_name,
80
const gchar **attribute_names,
81
const gchar **attribute_values,
82
gpointer user_data, G_GNUC_UNUSED GError **error)
84
struct rss_parser *parser = user_data;
86
switch (parser->state) {
88
if (g_ascii_strcasecmp(element_name, "item") == 0) {
90
parser->song = song_remote_new("rss:");
91
parser->tag = TAG_NUM_OF_ITEM_TYPES;
97
if (g_ascii_strcasecmp(element_name, "enclosure") == 0) {
98
const gchar *href = get_attribute(attribute_names,
102
/* create new song object, and copy
103
the existing tag over; we cannot
104
replace the existing song's URI,
105
because that attribute is
107
struct song *song = song_remote_new(href);
109
if (parser->song != NULL) {
110
song->tag = parser->song->tag;
111
parser->song->tag = NULL;
112
song_free(parser->song);
117
} else if (g_ascii_strcasecmp(element_name, "title") == 0)
118
parser->tag = TAG_TITLE;
119
else if (g_ascii_strcasecmp(element_name, "itunes:author") == 0)
120
parser->tag = TAG_ARTIST;
127
rss_end_element(G_GNUC_UNUSED GMarkupParseContext *context,
128
const gchar *element_name,
129
gpointer user_data, G_GNUC_UNUSED GError **error)
131
struct rss_parser *parser = user_data;
133
switch (parser->state) {
138
if (g_ascii_strcasecmp(element_name, "item") == 0) {
139
if (strcmp(parser->song->uri, "rss:") != 0)
140
parser->songs = g_slist_prepend(parser->songs,
143
song_free(parser->song);
145
parser->state = ROOT;
147
parser->tag = TAG_NUM_OF_ITEM_TYPES;
154
rss_text(G_GNUC_UNUSED GMarkupParseContext *context,
155
const gchar *text, gsize text_len,
156
gpointer user_data, G_GNUC_UNUSED GError **error)
158
struct rss_parser *parser = user_data;
160
switch (parser->state) {
165
if (parser->tag != TAG_NUM_OF_ITEM_TYPES) {
166
if (parser->song->tag == NULL)
167
parser->song->tag = tag_new();
168
tag_add_item_n(parser->song->tag, parser->tag,
176
static const GMarkupParser rss_parser = {
177
.start_element = rss_start_element,
178
.end_element = rss_end_element,
183
song_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
185
struct song *song = data;
191
rss_parser_destroy(gpointer data)
193
struct rss_parser *parser = data;
195
if (parser->state >= ITEM)
196
song_free(parser->song);
198
g_slist_foreach(parser->songs, song_free_callback, NULL);
199
g_slist_free(parser->songs);
203
* The playlist object
207
struct rss_playlist {
208
struct playlist_provider base;
213
static struct playlist_provider *
214
rss_open_stream(struct input_stream *is)
216
struct rss_parser parser = {
220
struct rss_playlist *playlist;
221
GMarkupParseContext *context;
225
GError *error = NULL;
227
/* parse the RSS XML file */
229
context = g_markup_parse_context_new(&rss_parser,
230
G_MARKUP_TREAT_CDATA_AS_TEXT,
231
&parser, rss_parser_destroy);
234
nbytes = input_stream_read(is, buffer, sizeof(buffer), &error);
237
g_markup_parse_context_free(context);
238
g_warning("%s", error->message);
246
success = g_markup_parse_context_parse(context, buffer, nbytes,
249
g_warning("XML parser failed: %s", error->message);
251
g_markup_parse_context_free(context);
256
success = g_markup_parse_context_end_parse(context, &error);
258
g_warning("XML parser failed: %s", error->message);
260
g_markup_parse_context_free(context);
264
/* create a #rss_playlist object from the parsed song list */
266
playlist = g_new(struct rss_playlist, 1);
267
playlist_provider_init(&playlist->base, &rss_playlist_plugin);
268
playlist->songs = g_slist_reverse(parser.songs);
271
g_markup_parse_context_free(context);
273
return &playlist->base;
277
rss_close(struct playlist_provider *_playlist)
279
struct rss_playlist *playlist = (struct rss_playlist *)_playlist;
281
g_slist_foreach(playlist->songs, song_free_callback, NULL);
282
g_slist_free(playlist->songs);
287
rss_read(struct playlist_provider *_playlist)
289
struct rss_playlist *playlist = (struct rss_playlist *)_playlist;
292
if (playlist->songs == NULL)
295
song = playlist->songs->data;
296
playlist->songs = g_slist_remove(playlist->songs, song);
301
static const char *const rss_suffixes[] = {
306
static const char *const rss_mime_types[] = {
307
"application/rss+xml",
312
const struct playlist_plugin rss_playlist_plugin = {
315
.open_stream = rss_open_stream,
319
.suffixes = rss_suffixes,
320
.mime_types = rss_mime_types,