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