use file cache for distros
[ppastats.git] / src / lp_ws.c
1 /*
2  * Copyright (C) 2011-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <curl/curl.h>
28 #include <json/json.h>
29
30 #include "cache.h"
31 #include "fcache.h"
32 #include "list.h"
33 #include "log.h"
34 #include "lp_ws.h"
35 #include "lp_json.h"
36 #include "ppastats.h"
37
38 static const char *
39 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=300";
40 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
41 static const char *
42 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
43
44 static const int DEFAULT_FETCH_RETRIES = 10;
45
46 static CURL *curl;
47
48 struct ucontent {
49         char *data;
50         size_t len;
51 };
52
53 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
54 {
55         size_t realsize = size * nmemb;
56         struct ucontent *mem = (struct ucontent *)userp;
57
58         mem->data = realloc(mem->data, mem->len + realsize + 1);
59
60         memcpy(&(mem->data[mem->len]), buffer, realsize);
61         mem->len += realsize;
62         mem->data[mem->len] = 0;
63
64         return realsize;
65 }
66
67 static void init()
68 {
69         if (!curl) {
70                 log_debug(_("initializing CURL"));
71                 curl_global_init(CURL_GLOBAL_ALL);
72                 curl = curl_easy_init();
73         }
74
75         if (!curl)
76                 exit(EXIT_FAILURE);
77 }
78
79 static char *fetch_url(const char *url)
80 {
81         struct ucontent *content = malloc(sizeof(struct ucontent));
82         char *result;
83         long code;
84         int retries;
85         unsigned int s;
86
87         log_debug(_("fetch_url(): %s"), url);
88
89         init();
90
91         result = NULL;
92
93         retries = DEFAULT_FETCH_RETRIES;
94
95  retrieve:
96         content->data = malloc(1);
97         content->data[0] = '\0';
98         content->len = 0;
99
100         curl_easy_setopt(curl, CURLOPT_URL, url);
101         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
102         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
103         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
104         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
105
106         if (curl_easy_perform(curl) == CURLE_OK) {
107                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
108
109                 switch (code) {
110                 case 200:
111                         result = content->data;
112                         break;
113                 case 500:
114                 case 502:
115                 case 503:
116                 case 504:
117                         log_err(_("Fetch failed with code %ld for URL %s"),
118                                 code,
119                                 url);
120
121                         if (retries) {
122                                 s = 2 * (DEFAULT_FETCH_RETRIES - retries) + 2;
123                                 log_debug(_("Wait %ds before retry"), s);
124                                 sleep(s);
125
126                                 free(content->data);
127                                 retries--;
128                                 goto retrieve;
129                         }
130
131                         break;
132                 default:
133                         log_err(_("Fetch failed with code %ld for URL %s"),
134                                 code,
135                                 url);
136                 }
137         }
138
139         if (!result)
140                 free(content->data);
141
142         free(content);
143
144         return result;
145 }
146
147 static json_object *get_json_object(const char *url)
148 {
149         json_object *obj = NULL;
150         char *body;
151
152         body = fetch_url(url);
153
154         if (body) {
155                 obj = json_tokener_parse(body);
156
157                 free(body);
158
159                 return obj;
160         }
161
162         return NULL;
163 }
164
165 #define json_object_to_bpph_list \
166 json_object_to_binary_package_publishing_history_list
167
168 struct binary_package_publishing_history * *
169 get_binary_package_publishing_history_list(const char *archive_url,
170                                            const char *pkg_status)
171 {
172         struct json_object *o_next;
173         char *url;
174         json_object *o;
175         void **result = NULL;
176
177         url = malloc(strlen(archive_url)+
178                      strlen(QUERY_GET_PUBLISHED_BINARIES)+
179                      (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
180                      1);
181
182         strcpy(url, archive_url);
183         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
184
185         if (pkg_status) {
186                 strcat(url, "&status=");
187                 strcat(url, pkg_status);
188         }
189
190         while (url) {
191                 o = get_json_object(url);
192                 free(url);
193                 url = NULL;
194
195                 if (!o)
196                         break;
197
198                 result = list_append_list(result,
199                                           (void **)json_object_to_bpph_list(o));
200
201                 o_next = json_object_object_get(o, "next_collection_link");
202
203                 if (o_next)
204                         url = strdup(json_object_get_string(o_next));
205
206                 json_object_put(o);
207         }
208
209         return (struct binary_package_publishing_history **)result;
210 }
211
212 int get_download_count(const char *archive_url)
213 {
214         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
215         char *url = malloc(n);
216         int result;
217         json_object *obj;
218
219         strcpy(url, archive_url);
220         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
221
222         obj = get_json_object(url);
223         free(url);
224
225         if (!obj)
226                 return -1;
227
228         result = json_object_get_int(obj);
229
230         json_object_put(obj);
231
232         return result;
233 }
234
235 const struct distro_arch_series *get_distro_arch_series(const char *url)
236 {
237         json_object *obj;
238         const struct distro_arch_series *distro;
239         char *content;
240
241         distro = cache_get(url);
242         if (distro)
243                 return (struct distro_arch_series *)distro;
244
245         content = fcache_get(url + 7);
246         if (!content) {
247                 content = fetch_url(url);
248                 if (content)
249                         fcache_put(url + 7, content);
250                 else
251                         return NULL;
252         }
253
254         obj = json_tokener_parse(content);
255
256         free(content);
257
258         if (!obj)
259                 return NULL;
260
261         distro = json_object_to_distro_arch_series(obj);
262
263         json_object_put(obj);
264
265         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
266
267         return distro;
268 }
269
270 const struct distro_series *get_distro_series(const char *url)
271 {
272         json_object *obj;
273         const struct distro_series *distro;
274
275         distro = cache_get(url);
276         if (distro)
277                 return (struct distro_series *)distro;
278
279         obj = get_json_object(url);
280
281         if (!obj)
282                 return NULL;
283
284         distro = json_object_to_distro_series(obj);
285
286         json_object_put(obj);
287
288         cache_put(url, distro, (void (*)(void *))&distro_series_free);
289
290         return distro;
291 }
292
293 struct daily_download_total **get_daily_download_totals(const char *binary_url)
294 {
295         char *url;
296         json_object *obj;
297         struct daily_download_total **result = NULL;
298
299         url = malloc(strlen(binary_url)+
300                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
301
302         strcpy(url, binary_url);
303         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
304
305         obj = get_json_object(url);
306
307         if (obj) {
308                 result = json_object_to_daily_download_totals(obj);
309                 json_object_put(obj);
310         }
311
312         free(url);
313
314         return result;
315 }
316
317 void lp_ws_cleanup()
318 {
319         log_debug(_("cleanup CURL"));
320
321         curl_easy_cleanup(curl);
322         curl_global_cleanup();
323 }