2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2007 Toni Spets
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include <curl/curl.h>
35 #include <curl/types.h>
36 #include <curl/easy.h>
46 typedef struct feed_ {
53 feed feeds[MAX_FEEDS];
56 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
58 size_t realsize = size * nmemb;
59 struct MemoryStruct *mem = (struct MemoryStruct *)data;
61 mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
63 memcpy(&(mem->memory[mem->size]), ptr, realsize);
64 mem->size += realsize;
65 mem->memory[mem->size] = 0;
71 int rss_delay(int *wait, int delay)
73 time_t now = time(NULL);
76 if(delay < 1) delay = 1;
84 if(now >= *wait + delay) {
95 for(i = 0; i < MAX_FEEDS; i++) {
98 feeds[i].last_update = 0;
105 for(i = 0; i < num_feeds; i++)
106 if(feeds[i].uri != NULL)
111 get_rss_info(char *uri, int delay)
116 struct MemoryStruct chunk;
120 // pointers to struct
121 feed *curfeed = NULL;
122 PRSS *curdata = NULL;
123 int *last_update = 0;
127 // first seek for the uri in list
128 for(i = 0; i < num_feeds; i++) {
129 if(feeds[i].uri != NULL)
130 if(!strcmp(feeds[i].uri, uri)) {
136 if(!curfeed) { // new feed
137 if(num_feeds == MAX_FEEDS-1) return NULL;
138 curfeed = &feeds[num_feeds];
139 curfeed->uri = strdup(uri);
143 last_update = &curfeed->last_update;
144 curdata = curfeed->data;
146 if(!rss_delay(last_update, delay))
147 return curdata; // wait for delay to pass
149 if(curdata != NULL) {
150 prss_free(curdata); // clean up old data
154 curl = curl_easy_init();
156 curl_easy_setopt(curl, CURLOPT_URL, uri);
157 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
158 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
159 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
160 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
162 res = curl_easy_perform(curl);
164 curdata = prss_parse_data(chunk.memory);
167 ERR("No data from server");
169 curl_easy_cleanup(curl);
172 curfeed->data = curdata;