1 /* Conky, a system monitor, based on torsmo
3 * Please see COPYING for details
5 * Copyright (c) 2007 Toni Spets
6 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <curl/curl.h>
30 #include <curl/types.h>
31 #include <curl/easy.h>
35 typedef struct feed_ {
42 feed feeds[MAX_FEEDS];
44 int rss_delay(int *wait_time, int delay)
46 time_t now = time(NULL);
55 *wait_time = now + delay;
59 if (now >= *wait_time + delay) {
60 *wait_time = now + delay;
67 void init_rss_info(void)
71 for (i = 0; i < MAX_FEEDS; i++) {
74 feeds[i].last_update = 0;
78 void free_rss_info(void)
82 for (i = 0; i < num_feeds; i++) {
83 if (feeds[i].uri != NULL) {
89 PRSS *get_rss_info(char *uri, int delay)
102 struct MemoryStruct chunk;
107 // first seek for the uri in list
108 for (i = 0; i < num_feeds; i++) {
109 if (feeds[i].uri != NULL) {
110 if (!strcmp(feeds[i].uri, uri)) {
117 if (!curfeed) { // new feed
118 if (num_feeds == MAX_FEEDS - 1) {
121 curfeed = &feeds[num_feeds];
122 curfeed->uri = strndup(uri, text_buffer_size);
126 last_update = &curfeed->last_update;
127 curdata = curfeed->data;
129 if (!rss_delay(last_update, delay)) {
130 return curdata; // wait for delay to pass
133 if (curdata != NULL) {
134 prss_free(curdata); // clean up old data
138 curl = curl_easy_init();
140 curl_easy_setopt(curl, CURLOPT_URL, uri);
141 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
142 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
143 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
144 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
146 res = curl_easy_perform(curl);
147 if (res == CURLE_OK && chunk.size) {
148 curdata = prss_parse_data(chunk.memory);
151 ERR("No data from server");
154 curl_easy_cleanup(curl);
157 curfeed->data = curdata;