1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2007 Toni Spets
10 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
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/>.
33 #include <curl/curl.h>
34 #include <curl/types.h>
35 #include <curl/easy.h>
45 typedef struct feed_ {
52 feed feeds[MAX_FEEDS];
54 size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
56 size_t realsize = size * nmemb;
57 struct MemoryStruct *mem = (struct MemoryStruct *) data;
59 mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
61 memcpy(&(mem->memory[mem->size]), ptr, realsize);
62 mem->size += realsize;
63 mem->memory[mem->size] = 0;
68 int rss_delay(int *wait, int delay)
70 time_t now = time(NULL);
83 if (now >= *wait + delay) {
91 void init_rss_info(void)
95 for (i = 0; i < MAX_FEEDS; i++) {
98 feeds[i].last_update = 0;
102 void free_rss_info(void)
106 for (i = 0; i < num_feeds; i++) {
107 if (feeds[i].uri != NULL) {
113 PRSS *get_rss_info(char *uri, int delay)
119 struct MemoryStruct chunk;
124 // pointers to struct
125 feed *curfeed = NULL;
126 PRSS *curdata = NULL;
127 int *last_update = 0;
131 // first seek for the uri in list
132 for (i = 0; i < num_feeds; i++) {
133 if (feeds[i].uri != NULL) {
134 if (!strcmp(feeds[i].uri, uri)) {
141 if (!curfeed) { // new feed
142 if (num_feeds == MAX_FEEDS - 1) {
145 curfeed = &feeds[num_feeds];
146 curfeed->uri = strdup(uri);
150 last_update = &curfeed->last_update;
151 curdata = curfeed->data;
153 if (!rss_delay(last_update, delay)) {
154 return curdata; // wait for delay to pass
157 if (curdata != NULL) {
158 prss_free(curdata); // clean up old data
162 curl = curl_easy_init();
164 curl_easy_setopt(curl, CURLOPT_URL, uri);
165 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
166 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
167 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
168 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
170 res = curl_easy_perform(curl);
172 curdata = prss_parse_data(chunk.memory);
175 ERR("No data from server");
178 curl_easy_cleanup(curl);
181 curfeed->data = curdata;