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