Esempio n. 1
0
static gchar*
feed_rss_formatter_format (GrssFeedFormatter *formatter)
{
	const gchar *str;
	const gchar *title;
	const gchar *link;
	gchar *formatted;
	time_t date;
	GList *iter;
	GList *items;
        GrssPerson *person;
	const GList *list;
	GString *text;
	GrssFeedChannel *channel;
	GrssFeedItem *item;

	/*
		Based on http://cyber.law.harvard.edu/rss/examples/rss2sample.xml
	*/

	text = g_string_new ("<?xml version=\"1.0\"?>\n<rss version=\"2.0\">\n");

	channel = grss_feed_formatter_get_channel (formatter);
	items = grss_feed_formatter_get_items (formatter);

	if (channel != NULL) {
		g_string_append_printf (text, "<channel>\n");

		title = grss_feed_channel_get_title (channel);
		if (title != NULL)
			g_string_append_printf (text, "\t<title>%s</title>\n", title);
		else
			title = "";

		str = grss_feed_channel_get_description (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<description>%s</description>\n", str);

		link = grss_feed_channel_get_homepage (channel);
		if (link != NULL)
			g_string_append_printf (text, "\t<link>%s</link>\n", link);
		else
			link = "";

		str = grss_feed_channel_get_copyright (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<copyright>%s</copyright>\n", str);

		person = grss_feed_channel_get_editor (channel);
		if (person != NULL)
			// TODO: implement handling additional attrs
			g_string_append_printf (text, "\t<managingEditor>%s</managingEditor>\n", grss_person_get_name (person));

		str = grss_feed_channel_get_generator (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<generator>%s</generator>\n", str);

		date = grss_feed_channel_get_update_time (channel);
		formatted = date_to_ISO8601 (date);
		g_string_append_printf (text, "\t<pubDate>%s</pubDate>\n", formatted);
		g_free (formatted);

		str = grss_feed_channel_get_image (channel);
		if (str != NULL) {
			g_string_append_printf (text, "\t<image>\n");
			g_string_append_printf (text, "\t\t<title>%s</title>\n", title);
			g_string_append_printf (text, "\t\t<url>%s</url>\n", str);
			g_string_append_printf (text, "\t\t<link>%s</link>\n", link);
			g_string_append_printf (text, "\t</image>\n");
		}

		for (iter = items; iter; iter = iter->next) {
			item = iter->data;

			g_string_append (text, "\t<item>\n");

			str = grss_feed_item_get_title (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<title>%s</title>\n", str);

			str = grss_feed_item_get_id (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<guid>%s</guid>\n", str);

			str = grss_feed_item_get_source (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<link>%s</link>\n", str);

			str = grss_feed_item_get_description (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<description>%s</description>\n", str);

			person = grss_feed_item_get_author (item);
			if (person != NULL)
				// FIXME: implement adding email and uri
				g_string_append_printf (text, "\t\t<author>%s</author>\n", grss_person_get_name (person));

			date = grss_feed_item_get_publish_time (item);
			formatted = date_to_ISO8601 (date);
			g_string_append_printf (text, "\t\t<pubDate>%s</pubDate>\n", formatted);
			g_free (formatted);

			g_string_append (text, "\t</item>\n");
		}
	}

	g_string_append (text, "</channel>\n</rss>");
	return g_string_free (text, FALSE);
}
Esempio n. 2
0
static gchar*
feed_atom_formatter_format (GrssFeedFormatter *formatter)
{
	const gchar *str;
	gchar *formatted;
	time_t date;
	GList *iter;
	GList *items;
	const GList *list;
	GString *text;
	GrssFeedChannel *channel;
	GrssFeedItem *item;

	text = g_string_new ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");

	channel = grss_feed_formatter_get_channel (formatter);
	items = grss_feed_formatter_get_items (formatter);

	if (channel != NULL) {
		str = grss_feed_channel_get_title (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<title>%s</title>\n", str);

		str = grss_feed_channel_get_description (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<subtitle>%s</subtitle>\n", str);

		str = grss_feed_channel_get_homepage (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<link href=\"%s\" />\n", str);

		str = grss_feed_channel_get_copyright (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<rights>%s</rights>\n", str);

		str = grss_feed_channel_get_editor (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<author>%s</author>\n", str);

		str = grss_feed_channel_get_generator (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<generator>%s</generator>\n", str);

		list = grss_feed_channel_get_contributors (channel);
		while (list != NULL) {
			g_string_append_printf (text, "\t<contributor>%s</contributor>\n", (gchar*) list->data);
			list = list->next;
		}

		date = grss_feed_channel_get_update_time (channel);
		if (date == 0)
			date = grss_feed_channel_get_publish_time (channel);
		formatted = date_to_ISO8601 (date);
		g_string_append_printf (text, "\t<updated>%s</updated>\n", formatted);
		g_free (formatted);

		str = grss_feed_channel_get_icon (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<icon>%s</icon>\n", str);

		str = grss_feed_channel_get_image (channel);
		if (str != NULL)
			g_string_append_printf (text, "\t<logo>%s</logo>\n", str);

		for (iter = items; iter; iter = iter->next) {
			item = iter->data;

			g_string_append (text, "\t<entry>\n");

			str = grss_feed_item_get_title (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<title>%s</title>\n", str);

			str = grss_feed_item_get_id (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<id>%s</id>\n", str);

			str = grss_feed_item_get_source (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<link href=\"%s\" />\n", str);

			str = grss_feed_item_get_description (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<summary>%s</summary>\n", str);

			str = grss_feed_item_get_author (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<author>%s</author>\n", str);

			str = grss_feed_item_get_copyright (item);
			if (str != NULL)
				g_string_append_printf (text, "\t\t<rights>%s</rights>\n", str);

			list = grss_feed_item_get_contributors (item);
			while (list != NULL) {
				g_string_append_printf (text, "\t\t<contributor>%s</contributor>\n", (gchar*) list->data);
				list = list->next;
			}

			date = grss_feed_item_get_publish_time (item);
			formatted = date_to_ISO8601 (date);
			g_string_append_printf (text, "\t\t<published>%s</published>\n", formatted);
			g_free (formatted);

			g_string_append (text, "\t</entry>\n");
		}
	}

	g_string_append (text, "</feed>");
	return g_string_free (text, FALSE);
}
Esempio n. 3
0
static GList*
feed_rss_handler_parse (FeedHandler *self, GrssFeedChannel *feed, xmlDocPtr doc, gboolean do_items, GError **error)
{
	gchar *tmp;
	gboolean rdf;
	time_t now;
	GList *items;
	xmlNodePtr cur;
	GrssFeedItem *item;
	FeedRssHandler *parser;

	items = NULL;
	rdf = FALSE;
	now = time (NULL);
	parser = FEED_RSS_HANDLER (self);

	cur = xmlDocGetRootElement (doc);
	while (cur && xmlIsBlankNode (cur))
		cur = cur->next;

	if (!xmlStrcmp (cur->name, BAD_CAST"rss")) {
		cur = cur->xmlChildrenNode;
		rdf = FALSE;
	}
	else if (!xmlStrcmp (cur->name, BAD_CAST"rdf") ||
	         !xmlStrcmp (cur->name, BAD_CAST"RDF")) {
		cur = cur->xmlChildrenNode;
		rdf = TRUE;
	}
	else if (!xmlStrcmp (cur->name, BAD_CAST"Channel")) {
		rdf = FALSE;
	}
	else {
		g_set_error (error, FEED_RSS_HANDLER_ERROR, FEED_RSS_HANDLER_PARSE_ERROR, "Could not find RDF/RSS header!");
		return NULL;
	}

	while (cur && xmlIsBlankNode (cur))
		cur = cur->next;

	while (cur) {
		if (!cur->name) {
			g_warning ("invalid XML: parser returns NULL value -> tag ignored!");
			cur = cur->next;
			continue;
		}

		if ((!xmlStrcmp (cur->name, BAD_CAST"channel")) ||
		    (!xmlStrcmp (cur->name, BAD_CAST"Channel"))) {
			parse_channel (parser, feed, doc, cur);
			if (rdf == FALSE)
				cur = cur->xmlChildrenNode;
			break;
		}

		cur = cur->next;
	}

	/* For RDF (rss 0.9 or 1.0), cur now points to the item after the channel tag. */
	/* For RSS, cur now points to the first item inside of the channel tag */
	/* This ends up being the thing with the items, (and images/textinputs for RDF) */

	/* parse channel contents */
	while (cur) {
		if (cur->type != XML_ELEMENT_NODE || NULL == cur->name) {
			cur = cur->next;
			continue;
		}

		/* save link to channel image */
		if ((!xmlStrcmp (cur->name, BAD_CAST"image"))) {
			if (NULL != (tmp = parse_image (cur))) {
				grss_feed_channel_set_image (feed, tmp);
				g_free (tmp);
			}
		}
		else if (do_items == TRUE && (!xmlStrcmp (cur->name, BAD_CAST"items"))) { /* RSS 1.1 */
			xmlNodePtr iter = cur->xmlChildrenNode;

			while (iter) {
				item = parse_rss_item (parser, feed, doc, iter);

				if (item != NULL) {
					if (grss_feed_item_get_publish_time (item) == 0)
						grss_feed_item_set_publish_time (item, now);
					items = g_list_append (items, item);
				}

				iter = iter->next;
			}
		}
		else if (do_items == TRUE && (!xmlStrcmp (cur->name, BAD_CAST"item"))) { /* RSS 1.0, 2.0 */
			item = parse_rss_item (parser, feed, doc, cur);

			if (item != NULL) {
				if (grss_feed_item_get_publish_time (item) == 0)
					grss_feed_item_set_publish_time (item, now);
				items = g_list_append (items, item);
			}
		}

		cur = cur->next;
	}

	grss_feed_channel_set_format (feed, "application/rss+xml");

	if (items != NULL)
		items = g_list_reverse (items);
	return items;
}