2007-05-06 12:59:31 +00:00
|
|
|
/*
|
|
|
|
* rss.c
|
2007-06-01 10:42:57 +00:00
|
|
|
* RSS stuff (prss version)
|
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-01 10:42:57 +00:00
|
|
|
PRSS* save = NULL;
|
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 10:42:57 +00:00
|
|
|
int rss_delay(int delay)
|
2007-05-06 12:59:31 +00:00
|
|
|
{
|
2007-06-01 10:42:57 +00:00
|
|
|
static int wait = 0;
|
|
|
|
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;
|
|
|
|
//delay *= 60;
|
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
if(!wait) {
|
|
|
|
wait = now + delay;
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
if(now >= wait + delay) {
|
|
|
|
wait = now + delay;
|
|
|
|
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;
|
|
|
|
struct MemoryStruct chunk;
|
|
|
|
chunk.memory = NULL;
|
|
|
|
chunk.size = 0;
|
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
if(!rss_delay(delay))
|
|
|
|
return save; // wait for delay to pass
|
|
|
|
|
|
|
|
if(save != NULL)
|
|
|
|
prss_free(save); // 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) {
|
|
|
|
save = prss_parse_data(chunk.memory);
|
|
|
|
free(chunk.memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
return save;
|
2007-05-06 12:59:31 +00:00
|
|
|
}
|