2010-02-09 12:11:56 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2007-06-01 15:49:49 +00:00
|
|
|
* Copyright (c) 2007 Mikko Sysikaski <mikko.sysikaski@gmail.com>
|
2008-02-20 20:30:45 +00:00
|
|
|
* Toni Spets <toni.spets@gmail.com>
|
2007-06-01 15:49:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
2009-07-27 20:47:19 +00:00
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2007-06-01 15:49:49 +00:00
|
|
|
|
2008-04-16 01:36:55 +00:00
|
|
|
#include "conky.h"
|
2008-06-19 06:17:53 +00:00
|
|
|
#include "prss.h"
|
2009-08-05 04:56:19 +00:00
|
|
|
#include "logging.h"
|
2007-06-01 10:42:57 +00:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
|
|
|
|
#ifndef PARSE_OPTIONS
|
|
|
|
#define PARSE_OPTIONS 0
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 05:43:36 +00:00
|
|
|
void prss_parse_doc(PRSS *result, xmlDocPtr doc);
|
2007-06-03 08:58:05 +00:00
|
|
|
|
2009-07-20 05:43:36 +00:00
|
|
|
void prss_parse_data(void *result, const char *xml_data)
|
2007-06-01 10:42:57 +00:00
|
|
|
{
|
2009-07-20 05:43:36 +00:00
|
|
|
PRSS *data = (PRSS*)result;
|
2009-08-05 04:56:19 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL,
|
|
|
|
PARSE_OPTIONS);
|
|
|
|
|
|
|
|
if (!doc) {
|
2009-07-20 05:43:36 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2009-07-20 05:43:36 +00:00
|
|
|
prss_parse_doc(data, doc);
|
2009-08-04 12:26:33 +00:00
|
|
|
xmlFreeDoc(doc);
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
void free_rss_items(PRSS *data)
|
2007-06-01 10:42:57 +00:00
|
|
|
{
|
2009-08-05 15:55:09 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < data->item_count; i++) {
|
2009-08-05 04:56:19 +00:00
|
|
|
#define CLEAR(a) if (data->items[i].a) { free(data->items[i].a); data->items[i].a = 0; }
|
|
|
|
CLEAR(title);
|
|
|
|
CLEAR(link);
|
|
|
|
CLEAR(description);
|
|
|
|
CLEAR(category);
|
|
|
|
CLEAR(pubDate);
|
|
|
|
CLEAR(guid);
|
|
|
|
#undef CLEAR
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-06-01 10:42:57 +00:00
|
|
|
free(data->items);
|
2009-08-05 04:56:19 +00:00
|
|
|
data->item_count = 0;
|
|
|
|
data->items = 0;
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
void prss_free(PRSS *data)
|
2007-06-01 15:49:49 +00:00
|
|
|
{
|
2009-08-05 04:56:19 +00:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data->version) {
|
|
|
|
free(data->version);
|
|
|
|
data->version = 0;
|
|
|
|
}
|
|
|
|
if (data->items) {
|
|
|
|
free_rss_items(data);
|
|
|
|
}
|
|
|
|
data->version = 0;
|
|
|
|
data->items = 0;
|
|
|
|
#define CLEAR(a) if (data->a) { free(data->a); data->a = 0; }
|
|
|
|
CLEAR(title);
|
|
|
|
CLEAR(link);
|
|
|
|
CLEAR(description);
|
|
|
|
CLEAR(language);
|
|
|
|
CLEAR(pubDate);
|
|
|
|
CLEAR(lastBuildDate);
|
|
|
|
CLEAR(generator);
|
|
|
|
CLEAR(docs);
|
|
|
|
CLEAR(managingEditor);
|
|
|
|
CLEAR(webMaster);
|
|
|
|
CLEAR(copyright);
|
|
|
|
CLEAR(ttl);
|
|
|
|
#undef CLEAR
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
2009-08-05 04:56:19 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
static inline void prss_null_item(PRSS_Item *i)
|
2007-06-01 15:49:49 +00:00
|
|
|
{
|
2007-06-02 08:17:33 +00:00
|
|
|
memset(i, 0, sizeof(PRSS_Item));
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
static inline void read_item(PRSS_Item *res, xmlNodePtr data)
|
2007-06-01 10:42:57 +00:00
|
|
|
{
|
2007-06-01 15:49:49 +00:00
|
|
|
prss_null_item(res);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
for (; data; data = data->next) {
|
2008-03-29 06:24:04 +00:00
|
|
|
xmlNodePtr child;
|
2008-03-29 03:45:36 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (data->type != XML_ELEMENT_NODE) {
|
2007-06-01 10:42:57 +00:00
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2008-03-29 06:24:04 +00:00
|
|
|
child = data->children;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (!child) {
|
2007-06-01 10:42:57 +00:00
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
#define ASSIGN(a) if (strcasecmp((const char*)data->name, #a) == EQUAL) { \
|
|
|
|
if (res->a) free(res->a); \
|
|
|
|
res->a = strdup((const char*)child->content); \
|
|
|
|
continue; \
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
2009-08-05 04:56:19 +00:00
|
|
|
ASSIGN(title);
|
|
|
|
ASSIGN(link);
|
|
|
|
ASSIGN(description);
|
|
|
|
ASSIGN(category);
|
|
|
|
ASSIGN(pubDate);
|
|
|
|
ASSIGN(guid);
|
|
|
|
#undef ASSIGN
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
static inline void read_element(PRSS *res, xmlNodePtr n)
|
2007-06-01 10:42:57 +00:00
|
|
|
{
|
2008-03-29 06:24:04 +00:00
|
|
|
xmlNodePtr child;
|
2008-03-29 03:45:36 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (n->type != XML_ELEMENT_NODE) {
|
2007-06-01 15:49:49 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2008-03-29 06:24:04 +00:00
|
|
|
child = n->children;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (!child) {
|
2007-06-01 15:49:49 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
#define ASSIGN(a) if (strcasecmp((const char*)n->name, #a) == EQUAL) { \
|
|
|
|
if (res->a) free(res->a); \
|
|
|
|
res->a = strdup((const char*)child->content); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
ASSIGN(title);
|
|
|
|
ASSIGN(link);
|
|
|
|
ASSIGN(description);
|
|
|
|
ASSIGN(language);
|
|
|
|
ASSIGN(pubDate);
|
|
|
|
ASSIGN(lastBuildDate);
|
|
|
|
ASSIGN(generator);
|
|
|
|
ASSIGN(docs);
|
|
|
|
ASSIGN(managingEditor);
|
|
|
|
ASSIGN(webMaster);
|
|
|
|
ASSIGN(copyright);
|
|
|
|
ASSIGN(ttl);
|
|
|
|
#undef ASSIGN
|
|
|
|
if (!strcasecmp((const char*)n->name, "item")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
read_item(&res->items[res->item_count++], n->children);
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
|
2007-06-01 15:49:49 +00:00
|
|
|
{
|
|
|
|
xmlNodePtr channel = root->children;
|
2008-03-29 06:24:04 +00:00
|
|
|
xmlNodePtr n;
|
|
|
|
int items = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
DBGP("parsing rss 2.0 or <1 doc");
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
while (channel && (channel->type != XML_ELEMENT_NODE
|
2009-08-05 04:56:19 +00:00
|
|
|
|| strcmp((const char *) channel->name, "channel"))) {
|
2007-06-01 15:49:49 +00:00
|
|
|
channel = channel->next;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (!channel) {
|
2007-06-01 15:49:49 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (n = channel->children; n; n = n->next) {
|
2008-03-29 03:45:36 +00:00
|
|
|
if (n->type == XML_ELEMENT_NODE &&
|
|
|
|
!strcmp((const char *) n->name, "item")) {
|
2007-06-01 10:42:57 +00:00
|
|
|
++items;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
if (res->version) free(res->version);
|
2008-04-02 18:44:49 +00:00
|
|
|
res->version = strndup("2.0", text_buffer_size);
|
2009-08-05 04:56:19 +00:00
|
|
|
if (res->items) free_rss_items(res);
|
2010-02-09 14:24:58 +00:00
|
|
|
res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item));
|
2007-06-01 15:49:49 +00:00
|
|
|
res->item_count = 0;
|
2007-06-01 10:42:57 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (n = channel->children; n; n = n->next) {
|
2007-06-01 15:49:49 +00:00
|
|
|
read_element(res, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
|
2007-06-01 15:49:49 +00:00
|
|
|
{
|
|
|
|
int items = 0;
|
|
|
|
xmlNodePtr n;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
DBGP("parsing rss 1.0 doc");
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (n = root->children; n; n = n->next) {
|
|
|
|
if (n->type == XML_ELEMENT_NODE) {
|
2008-03-29 03:45:36 +00:00
|
|
|
if (!strcmp((const char *) n->name, "item")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
++items;
|
2008-03-29 03:45:36 +00:00
|
|
|
} else if (!strcmp((const char *) n->name, "channel")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
xmlNodePtr i;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
for (i = n->children; i; i = i->next) {
|
2007-06-01 15:49:49 +00:00
|
|
|
read_element(res, i);
|
|
|
|
}
|
|
|
|
}
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-08-05 04:56:19 +00:00
|
|
|
if (res->version) free(res->version);
|
2008-04-02 18:44:49 +00:00
|
|
|
res->version = strndup("1.0", text_buffer_size);
|
2009-08-05 04:56:19 +00:00
|
|
|
if (res->items) free_rss_items(res);
|
2010-02-09 14:24:58 +00:00
|
|
|
res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item));
|
2007-06-01 15:49:49 +00:00
|
|
|
res->item_count = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (n = root->children; n; n = n->next) {
|
2008-03-29 03:45:36 +00:00
|
|
|
if (n->type == XML_ELEMENT_NODE &&
|
|
|
|
!strcmp((const char *) n->name, "item")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
read_item(&res->items[res->item_count++], n->children);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-20 05:43:36 +00:00
|
|
|
void prss_parse_doc(PRSS *result, xmlDocPtr doc)
|
2007-06-01 15:49:49 +00:00
|
|
|
{
|
|
|
|
xmlNodePtr root = xmlDocGetRootElement(doc);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
do {
|
|
|
|
if (root->type == XML_ELEMENT_NODE) {
|
2008-03-29 03:45:36 +00:00
|
|
|
if (!strcmp((const char *) root->name, "RDF")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
// RSS 1.0 document
|
2009-07-20 05:43:36 +00:00
|
|
|
parse_rss_1_0(result, root);
|
|
|
|
return;
|
2008-03-29 03:45:36 +00:00
|
|
|
} else if (!strcmp((const char *) root->name, "rss")) {
|
2007-06-01 15:49:49 +00:00
|
|
|
// RSS 2.0 or <1.0 document
|
2009-07-20 05:43:36 +00:00
|
|
|
parse_rss_2_0(result, root);
|
|
|
|
return;
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
root = root->next;
|
2008-02-20 20:30:45 +00:00
|
|
|
} while (root);
|
2009-07-20 05:43:36 +00:00
|
|
|
return;
|
2007-06-01 10:42:57 +00:00
|
|
|
}
|