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.
25 #include <procheader.h>
29 #include "feeditemenclosure.h"
33 void feed_parser_rss20_start(void *data, const gchar *el, const gchar **attr)
35 FeedParserCtx *ctx = (FeedParserCtx *)data;
36 FeedItemEnclosure *enclosure = NULL;
37 gchar *url, *type, *size_s;
40 /* ------------------- */
41 if( ctx->depth == 2 ) {
42 if( !strcmp(el, "item") ) { /* Start of new item */
44 if( ctx->curitem != NULL )
45 feed_item_free(ctx->curitem);
47 ctx->curitem = feed_item_new(ctx->feed);
49 } else ctx->location = 0;
50 /* ------------------- */
51 } else if( ctx->depth == 3 ) {
52 if( !strcmp(el, "enclosure") ) { /* Media enclosure */
54 url = feed_parser_get_attribute_value(attr, "url");
55 type = feed_parser_get_attribute_value(attr, "type");
56 size_s = feed_parser_get_attribute_value(attr, "length");
58 size = (gulong)atol(size_s);
60 if( url != NULL && type != NULL && size > 0 ) {
61 if( (enclosure = feed_item_enclosure_new(url, type, size)) )
62 feed_item_set_enclosure(ctx->curitem, enclosure);
65 } else if( !strcmp(el, "guid") ) { /* Unique ID */
66 type = feed_parser_get_attribute_value(attr, "isPermaLink");
67 if( type != NULL && !strcmp(type, "false") )
68 feed_item_set_id_permalink(ctx->curitem, TRUE);
70 } else ctx->location = 0;
76 void feed_parser_rss20_end(void *data, const gchar *el)
78 FeedParserCtx *ctx = (FeedParserCtx *)data;
79 Feed *feed = ctx->feed;
82 if( ctx->str != NULL )
89 switch( ctx->depth ) {
91 /* ------------------- */
94 if( !strcmp(el, "rss") ) {
95 /* we finished parsing the feed */
96 ctx->feed->items = g_slist_reverse(ctx->feed->items);
101 /* ------------------- */
104 break; /* nothing to do at this depth */
106 /* ------------------- */
109 /* decide if we just received </item>, so we can
110 * add a complete item to feed */
111 if( !strcmp(el, "item") ) {
113 /* append the complete feed item, if it is valid
114 * "All elements of an item are optional, however at least one
115 * of title or description must be present." */
116 if( ctx->curitem->title != NULL || ctx->curitem->summary != NULL ) {
118 g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
121 /* since it's in the linked list, lose this pointer */
124 } else if( !strcmp(el, "title") ) { /* so it wasn't end of item */
126 } else if( !strcmp(el, "description" ) ) {
127 FILL(feed->description)
128 } else if( !strcmp(el, "dc:language") ) {
130 } else if( !strcmp(el, "author") ) {
132 } else if( !strcmp(el, "admin:generatorAgent") ) {
133 FILL(feed->generator)
134 } else if( !strcmp(el, "dc:date") ) {
135 feed->date = parseISO8601Date(text);
136 } else if( !strcmp(el, "pubDate") ) {
137 feed->date = procheader_date_parse(NULL, text, 0);
142 /* ------------------- */
145 if( ctx->curitem == NULL ) {
149 /* decide which field did we just get */
150 if( !strcmp(el, "title") ) {
151 FILL(ctx->curitem->title)
152 } else if( !strcmp(el, "author") ) {
153 FILL(ctx->curitem->author)
154 } else if( !strcmp(el, "description") ) {
155 FILL(ctx->curitem->summary)
156 } else if( !strcmp(el, "content:encoded") ) {
157 FILL(ctx->curitem->text)
158 } else if( !strcmp(el, "link") ) {
159 FILL(ctx->curitem->url)
160 } else if( !strcmp(el, "guid") ) {
161 FILL(ctx->curitem->id)
162 } else if( !strcmp(el, "wfw:commentRSS") || !strcmp(el, "wfw:commentRss") ) {
163 FILL(ctx->curitem->comments_url)
164 } else if( !strcmp(el, "dc:date") ) {
165 ctx->curitem->date_modified = parseISO8601Date(text);
166 } else if( !strcmp(el, "pubDate") ) {
167 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
168 } else if( !strcmp(el, "dc:creator")) {
169 FILL(ctx->curitem->author)
176 if( ctx->str != NULL ) {
177 g_string_free(ctx->str, TRUE);