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-2008 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>
44 typedef struct feed_ {
51 feed feeds[MAX_FEEDS];
53 size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
55 size_t realsize = size * nmemb;
56 struct MemoryStruct *mem = (struct MemoryStruct *) data;
58 mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
60 memcpy(&(mem->memory[mem->size]), ptr, realsize);
61 mem->size += realsize;
62 mem->memory[mem->size] = 0;
67 int rss_delay(int *wait_time, int delay)
69 time_t now = time(NULL);
78 *wait_time = now + delay;
82 if (now >= *wait_time + delay) {
83 *wait_time = now + delay;
90 void init_rss_info(void)
94 for (i = 0; i < MAX_FEEDS; i++) {
97 feeds[i].last_update = 0;
101 void free_rss_info(void)
105 for (i = 0; i < num_feeds; i++) {
106 if (feeds[i].uri != NULL) {
112 PRSS *get_rss_info(char *uri, int delay)
117 // pointers to struct
118 feed *curfeed = NULL;
119 PRSS *curdata = NULL;
120 int *last_update = 0;
125 struct MemoryStruct chunk;
130 // first seek for the uri in list
131 for (i = 0; i < num_feeds; i++) {
132 if (feeds[i].uri != NULL) {
133 if (!strcmp(feeds[i].uri, uri)) {
140 if (!curfeed) { // new feed
141 if (num_feeds == MAX_FEEDS - 1) {
144 curfeed = &feeds[num_feeds];
145 curfeed->uri = strndup(uri, text_buffer_size);
149 last_update = &curfeed->last_update;
150 curdata = curfeed->data;
152 if (!rss_delay(last_update, delay)) {
153 return curdata; // wait for delay to pass
156 if (curdata != NULL) {
157 prss_free(curdata); // clean up old data
161 curl = curl_easy_init();
163 curl_easy_setopt(curl, CURLOPT_URL, uri);
164 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
165 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
166 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
167 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
169 res = curl_easy_perform(curl);
171 curdata = prss_parse_data(chunk.memory);
174 ERR("No data from server");
177 curl_easy_cleanup(curl);
180 curfeed->data = curdata;