2007-05-06 12:59:31 +00:00
|
|
|
/*
|
|
|
|
* rss.c
|
2007-06-01 10:42:57 +00:00
|
|
|
* RSS stuff (prss version)
|
2007-06-01 15:49:49 +00:00
|
|
|
*
|
|
|
|
* prss.c and prss.h written by Sisu (Mikko Sysikaski)
|
|
|
|
* new rss.c written by hifi (Toni Spets)
|
2007-05-06 12:59:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-06-01 10:42:57 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "prss.h"
|
2007-06-01 11:37:19 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/types.h>
|
|
|
|
#include <curl/easy.h>
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-02 08:17:33 +00:00
|
|
|
#include "conky.h"
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
#define MAX_FEEDS 16
|
2007-06-01 10:42:57 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
typedef struct feed_ {
|
|
|
|
char* uri;
|
|
|
|
int last_update;
|
|
|
|
PRSS* data;
|
|
|
|
} feed;
|
|
|
|
|
|
|
|
int num_feeds = 0;
|
|
|
|
feed feeds[MAX_FEEDS];
|
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
size_t
|
|
|
|
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct MemoryStruct *mem = (struct MemoryStruct *)data;
|
|
|
|
|
|
|
|
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
|
|
|
|
if (mem->memory) {
|
|
|
|
memcpy(&(mem->memory[mem->size]), ptr, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
}
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
int rss_delay(int *wait, int delay)
|
2007-05-06 12:59:31 +00:00
|
|
|
{
|
2007-06-01 10:42:57 +00:00
|
|
|
time_t now = time(NULL);
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
// make it minutes
|
|
|
|
if(delay < 1) delay = 1;
|
2007-06-01 11:46:14 +00:00
|
|
|
delay *= 60;
|
2007-06-01 11:37:19 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
if(!*wait) {
|
|
|
|
*wait = now + delay;
|
2007-06-01 10:42:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
if(now >= *wait + delay) {
|
|
|
|
*wait = now + delay;
|
2007-06-01 10:42:57 +00:00
|
|
|
return 1;
|
2007-05-06 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRSS*
|
|
|
|
get_rss_info(char *uri, int delay)
|
|
|
|
{
|
2007-06-01 11:37:19 +00:00
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode res;
|
2007-06-01 15:49:49 +00:00
|
|
|
// curl temps
|
2007-06-01 11:37:19 +00:00
|
|
|
struct MemoryStruct chunk;
|
|
|
|
chunk.memory = NULL;
|
|
|
|
chunk.size = 0;
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
// pointers to struct
|
|
|
|
feed *curfeed = NULL;
|
|
|
|
PRSS *curdata = NULL;
|
|
|
|
int *last_update = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// first seek for the uri in list
|
|
|
|
if(num_feeds > 0) {
|
|
|
|
for(i = 0; i < num_feeds; i++) {
|
|
|
|
if(feeds[i].uri != NULL)
|
|
|
|
if(!strcmp(feeds[i].uri, uri)) {
|
|
|
|
curfeed = &feeds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-01 10:42:57 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
if(!curfeed) { // new feed
|
|
|
|
if(num_feeds == MAX_FEEDS-1) return NULL;
|
|
|
|
curfeed = &feeds[num_feeds];
|
|
|
|
curfeed->uri = (char *)malloc(sizeof(char) * strlen(uri)+1);
|
|
|
|
strncpy(curfeed->uri, uri, strlen(uri)+1);
|
|
|
|
num_feeds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_update = &curfeed->last_update;
|
|
|
|
curdata = curfeed->data;
|
|
|
|
|
|
|
|
if(!rss_delay(last_update, delay))
|
|
|
|
return curdata; // wait for delay to pass
|
|
|
|
|
|
|
|
if(curdata != NULL)
|
|
|
|
prss_free(curdata); // clean up old data
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, uri);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
if(chunk.size) {
|
2007-06-01 15:49:49 +00:00
|
|
|
curdata = prss_parse_data(chunk.memory);
|
2007-06-01 11:37:19 +00:00
|
|
|
free(chunk.memory);
|
2007-06-02 08:17:33 +00:00
|
|
|
} else
|
|
|
|
ERR("No data from server");
|
2007-06-01 11:37:19 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
curfeed->data = curdata;
|
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
return curdata;
|
2007-05-06 12:59:31 +00:00
|
|
|
}
|