f52f935f16b82dbcd689dc4e6878d9fdc213f09a
[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 "list.h"
32 #include "log.h"
33 #include "lp_ws.h"
34 #include "lp_json.h"
35 #include "ppastats.h"
36
37 static const char *
38 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=300";
39 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
40 static const char *
41 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
42
43 static const int DEFAULT_FETCH_RETRIES = 10;
44
45 static CURL *curl;
46
47 struct ucontent {
48         char *data;
49         size_t len;
50 };
51
52 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
53 {
54         size_t realsize = size * nmemb;
55         struct ucontent *mem = (struct ucontent *)userp;
56
57         mem->data = realloc(mem->data, mem->len + realsize + 1);
58
59         memcpy(&(mem->data[mem->len]), buffer, realsize);
60         mem->len += realsize;
61         mem->data[mem->len] = 0;
62
63         return realsize;
64 }
65
66 static void init()
67 {
68         if (!curl) {
69                 log_debug(_("initializing CURL"));
70                 curl_global_init(CURL_GLOBAL_ALL);
71                 curl = curl_easy_init();
72         }
73
74         if (!curl)
75                 exit(EXIT_FAILURE);
76 }
77
78 static char *fetch_url(const char *url)
79 {
80         struct ucontent *content = malloc(sizeof(struct ucontent));
81         char *result;
82         long code;
83         int retries;
84         unsigned int s;
85
86         log_debug(_("fetch_url(): %s"), url);
87
88         init();
89
90         result = NULL;
91
92         retries = DEFAULT_FETCH_RETRIES;
93
94  retrieve:
95         content->data = malloc(1);
96         content->data[0] = '\0';
97         content->len = 0;
98
99         curl_easy_setopt(curl, CURLOPT_URL, url);
100         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
101         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
102         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
103         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
104
105         if (curl_easy_perform(curl) == CURLE_OK) {
106                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
107
108                 switch (code) {
109                 case 200:
110                         result = content->data;
111                         break;
112                 case 500:
113                 case 502:
114                 case 503:
115                 case 504:
116                         log_err(_("Fetch failed with code %ld for URL %s"),
117                                 code,
118                                 url);
119
120                         if (retries) {
121                                 s = 2 * (DEFAULT_FETCH_RETRIES - retries) + 2;
122                                 log_debug(_("Wait %ds before retry"), s);
123                                 sleep(s);
124
125                                 free(content->data);
126                                 retries--;
127                                 goto retrieve;
128                         }
129
130                         break;
131                 default:
132                         log_err(_("Fetch failed with code %ld for URL %s"),
133                                 code,
134                                 url);
135                 }
136         }
137
138         if (!result)
139                 free(content->data);
140
141         free(content);
142
143         return result;
144 }
145
146 static json_object *get_json_object(const char *url)
147 {
148         json_object *obj = NULL;
149         char *body;
150
151         body = fetch_url(url);
152
153         if (body) {
154                 obj = json_tokener_parse(body);
155
156                 free(body);
157
158                 return obj;
159         }
160
161         return NULL;
162 }
163
164 #define json_object_to_bpph_list \
165 json_object_to_binary_package_publishing_history_list
166
167 struct binary_package_publishing_history * *
168 get_binary_package_publishing_history_list(const char *archive_url,
169                                            const char *pkg_status)
170 {
171         struct json_object *o_next;
172         char *url;
173         json_object *o;
174         void **result = NULL;
175
176         url = malloc(strlen(archive_url)+
177                      strlen(QUERY_GET_PUBLISHED_BINARIES)+
178                      (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
179                      1);
180
181         strcpy(url, archive_url);
182         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
183
184         if (pkg_status) {
185                 strcat(url, "&status=");
186                 strcat(url, pkg_status);
187         }
188
189         while (url) {
190                 o = get_json_object(url);
191                 free(url);
192                 url = NULL;
193
194                 if (!o)
195                         break;
196
197                 result = list_append_list(result,
198                                           (void **)json_object_to_bpph_list(o));
199
200                 o_next = json_object_object_get(o, "next_collection_link");
201
202                 if (o_next)
203                         url = strdup(json_object_get_string(o_next));
204
205                 json_object_put(o);
206         }
207
208         return (struct binary_package_publishing_history **)result;
209 }
210
211 int get_download_count(const char *archive_url)
212 {
213         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
214         char *url = malloc(n);
215         int result;
216         json_object *obj;
217
218         strcpy(url, archive_url);
219         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
220
221         obj = get_json_object(url);
222         free(url);
223
224         if (!obj)
225                 return -1;
226
227         result = json_object_get_int(obj);
228
229         json_object_put(obj);
230
231         return result;
232 }
233
234 const struct distro_arch_series *get_distro_arch_series(const char *url)
235 {
236         json_object *obj;
237         const struct distro_arch_series *distro;
238
239         distro = cache_get(url);
240         if (distro)
241                 return (struct distro_arch_series *)distro;
242
243         obj = get_json_object(url);
244
245         if (!obj)
246                 return NULL;
247
248         distro = json_object_to_distro_arch_series(obj);
249
250         json_object_put(obj);
251
252         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
253
254         return distro;
255 }
256
257 const struct distro_series *get_distro_series(const char *url)
258 {
259         json_object *obj;
260         const struct distro_series *distro;
261
262         distro = cache_get(url);
263         if (distro)
264                 return (struct distro_series *)distro;
265
266         obj = get_json_object(url);
267
268         if (!obj)
269                 return NULL;
270
271         distro = json_object_to_distro_series(obj);
272
273         json_object_put(obj);
274
275         cache_put(url, distro, (void (*)(void *))&distro_series_free);
276
277         return distro;
278 }
279
280 struct daily_download_total **get_daily_download_totals(const char *binary_url)
281 {
282         char *url;
283         json_object *obj;
284         struct daily_download_total **result = NULL;
285
286         url = malloc(strlen(binary_url)+
287                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
288
289         strcpy(url, binary_url);
290         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
291
292         obj = get_json_object(url);
293
294         if (obj) {
295                 result = json_object_to_daily_download_totals(obj);
296                 json_object_put(obj);
297         }
298
299         free(url);
300
301         return result;
302 }
303
304 void lp_ws_cleanup()
305 {
306         log_debug(_("cleanup CURL"));
307
308         curl_easy_cleanup(curl);
309         curl_global_cleanup();
310 }