A simple RSS feed downloader that saves items in a Maildir
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
1.5 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "fetch.h"
struct datastruct {
char *data;
size_t size;
};
/* curl write function */
static size_t curl_cb(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t actualsize = size * nmemb;
struct datastruct *mem = (struct datastruct *) userp;
char *ptr = realloc(mem->data, mem->size + actualsize + 1);
if (!ptr) {
printf("not enough memory (realloc retuned NULL)\n");
return -1;
}
mem->data = ptr;
memcpy(&(mem->data[mem->size]), contents, actualsize);
mem->size += actualsize;
/* string null terminator at end */
mem->data[mem->size] = '\0';
return actualsize;
}
/* requests raw rss data */
char *fetchrss(char *url)
{
CURL *curl;
CURLcode res;
struct datastruct chunk;
chunk.data = malloc(1);
chunk.size = 0;
curl = curl_easy_init();
if (curl == NULL)
return NULL;
/* set url options */
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
/* make request and print to stderr if error occurs */
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\nurl: %s\n",
curl_easy_strerror(res), url);
/* perform curl cleanup */
curl_easy_cleanup(curl);
curl_global_cleanup();
return NULL;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
return chunk.data;
}