2 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <procheader.h>
30 #include "feeditemenclosure.h"
33 #include "common/utils.h"
35 void feed_parser_rss20_start(void *data, const gchar *el, const gchar **attr)
37 FeedParserCtx *ctx = (FeedParserCtx *)data;
38 FeedItemEnclosure *enclosure = NULL;
39 gchar *url, *type, *size_s;
42 /* ------------------- */
43 if( ctx->depth == 2 ) {
44 if( !strcmp(el, "item") ) { /* Start of new item */
46 if( ctx->curitem != NULL )
47 feed_item_free(ctx->curitem);
49 ctx->curitem = feed_item_new(ctx->feed);
51 } else ctx->location = 0;
52 /* ------------------- */
53 } else if( ctx->depth == 3 ) {
54 if( !strcmp(el, "enclosure") ) { /* Media enclosure */
56 url = feed_parser_get_attribute_value(attr, "url");
57 type = feed_parser_get_attribute_value(attr, "type");
58 size_s = feed_parser_get_attribute_value(attr, "length");
60 size = (gulong)atol(size_s);
62 if( url != NULL && type != NULL && size > 0 ) {
63 if( (enclosure = feed_item_enclosure_new(url, type, size)) )
64 feed_item_set_enclosure(ctx->curitem, enclosure);
67 } else if( !strcmp(el, "guid") ) { /* Unique ID */
68 type = feed_parser_get_attribute_value(attr, "isPermaLink");
69 if( type != NULL && !strcmp(type, "false") )
70 feed_item_set_id_permalink(ctx->curitem, TRUE);
72 } else ctx->location = 0;
78 void feed_parser_rss20_end(void *data, const gchar *el)
80 FeedParserCtx *ctx = (FeedParserCtx *)data;
81 Feed *feed = ctx->feed;
84 if( ctx->str != NULL )
85 text = g_strstrip(g_strdup(ctx->str->str));
91 switch( ctx->depth ) {
93 /* ------------------- */
96 if( !strcmp(el, "rss") ) {
97 /* we finished parsing the feed */
98 ctx->feed->items = g_slist_reverse(ctx->feed->items);
103 /* ------------------- */
106 break; /* nothing to do at this depth */
108 /* ------------------- */
111 /* decide if we just received </item>, so we can
112 * add a complete item to feed */
113 if( !strcmp(el, "item") ) {
115 /* append the complete feed item, if it is valid
116 * "All elements of an item are optional, however at least one
117 * of title or description must be present." */
118 if( ctx->curitem->title != NULL || ctx->curitem->summary != NULL ) {
120 g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
123 /* since it's in the linked list, lose this pointer */
126 } else if( !strcmp(el, "title") ) { /* so it wasn't end of item */
128 } else if( !strcmp(el, "description" ) ) {
129 FILL(feed->description)
130 } else if( !strcmp(el, "dc:language") ) {
132 } else if( !strcmp(el, "author") ) {
134 } else if( !strcmp(el, "admin:generatorAgent") ) {
135 FILL(feed->generator)
136 } else if( !strcmp(el, "dc:date") ) {
137 feed->date = procheader_date_parse(NULL, text, 0);
138 } else if( !strcmp(el, "pubDate") ) {
139 feed->date = procheader_date_parse(NULL, text, 0);
144 /* ------------------- */
147 if( ctx->curitem == NULL ) {
151 /* decide which field did we just get */
152 if( !strcmp(el, "title") ) {
153 FILL(ctx->curitem->title)
154 } else if( !strcmp(el, "author") ) {
155 FILL(ctx->curitem->author)
156 } else if( !strcmp(el, "description") ) {
157 FILL(ctx->curitem->summary)
158 } else if( !strcmp(el, "content:encoded") ) {
159 FILL(ctx->curitem->text)
160 } else if( !strcmp(el, "link") ) {
161 FILL(ctx->curitem->url)
162 } else if( !strcmp(el, "guid") ) {
163 FILL(ctx->curitem->id)
164 } else if( !strcmp(el, "wfw:commentRSS") || !strcmp(el, "wfw:commentRss") ) {
165 FILL(ctx->curitem->comments_url)
166 } else if( !strcmp(el, "dc:date") ) {
167 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
168 } else if( !strcmp(el, "pubDate") ) {
169 ctx->curitem->date_published = procheader_date_parse(NULL, text, 0);
170 } else if( !strcmp(el, "dc:creator")) {
171 FILL(ctx->curitem->author)
178 if( ctx->str != NULL ) {
180 g_string_free(ctx->str, TRUE);