added --get-bpph-size to specify the size of the requests to get the list of binary...
[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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <json/json.h>
28
29 #include "cache.h"
30 #include "fcache.h"
31 #include "http.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 /** Default ws.size value for the getPublishedBinaries request. */
39 static const int DEFAULT_WS_SIZE = 150;
40
41 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
42 static const char *
43 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
44
45 static json_object *get_json_object(const char *url)
46 {
47         json_object *obj = NULL;
48         char *body;
49
50         body = get_url_content(url, 0);
51
52         if (body) {
53                 obj = json_tokener_parse(body);
54
55                 free(body);
56
57                 return obj;
58         }
59
60         return NULL;
61 }
62
63 static char *get_bpph_list_cache_key(const char *archive_url)
64 {
65         char *key;
66
67         key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
68         sprintf(key, "%s/bpph", archive_url + 7);
69
70         return key;
71 }
72
73 static struct bpph **get_bpph_list_from_cache(const char *key)
74 {
75         char *content;
76         struct bpph **list;
77         json_object *json;
78
79         content = fcache_get(key);
80         if (!content)
81                 return NULL;
82
83         json = json_tokener_parse(content);
84         if (!json)
85                 return NULL;
86
87         list = json_object_to_bpph_list(json);
88
89         json_object_put(json);
90         free(content);
91
92         return list;
93 }
94
95 static char *get_last_creation_date(struct bpph **list)
96 {
97         time_t last, t;
98         struct bpph **cur;
99
100         last = 0;
101
102         if (list)
103                 for (cur = list; *cur; cur++) {
104                         t = (*cur)->date_created;
105                         if (t > last)
106                                 last = t;
107                 }
108
109         if (last)
110                 return time_to_str(last);
111         else
112                 return NULL;
113 }
114
115 /*
116  * 'archive_url': LP URL of the archive.
117  * 'size': size of the reply array. Between 1-300, else default value is used.
118  */
119 static char *create_query_get_bpph(const char *archive_url,
120                                    const char *status,
121                                    int size)
122 {
123         static const char *default_opt = "?ws.op=getPublishedBinaries&ws.size=";
124         static const char *status_opt = "&status=";
125         char *url;
126         size_t n;
127
128         if (size < 1 || size > 300)
129                 size = DEFAULT_WS_SIZE;
130
131         n = strlen(archive_url) + strlen(default_opt) + 3 + 1;
132
133         if (status)
134                 n += strlen(status_opt) + strlen(status);
135
136         url = malloc(n);
137         sprintf(url, "%s%s%d", archive_url, default_opt, size);
138
139         if (status) {
140                 strcat(url, status_opt);
141                 strcat(url, status);
142         }
143
144         return url;
145 }
146
147 struct bpph **get_bpph_list(const char *archive_url,
148                             const char *pkg_status,
149                             int ws_size)
150 {
151         char *url, *key, *tmp;
152         struct bpph **result;
153         struct json_object *o, *bpph_json, *o_next;
154         char *date;
155         int ok;
156
157         url = create_query_get_bpph(archive_url, pkg_status, ws_size);
158
159         key = get_bpph_list_cache_key(archive_url);
160
161         result = get_bpph_list_from_cache(key);
162
163         if (result) {
164                 date = get_last_creation_date(result);
165
166                 if (date) {
167                         printf("Update package since: %s\n", date);
168
169                         tmp = malloc(strlen(url)
170                                      + strlen("&created_since_date=")
171                                      + strlen(date)+1);
172                         strcpy(tmp, url);
173                         strcat(tmp, "&created_since_date=");
174                         strcat(tmp, date);
175
176                         free(url);
177                         url = tmp;
178
179                         free(date);
180                 }
181         }
182
183         ok = 1;
184         while (url) {
185                 o = get_json_object(url);
186                 free(url);
187                 url = NULL;
188
189                 if (!o) {
190                         ok = 0;
191                         break;
192                 }
193
194                 result = bpph_list_append_list(result,
195                                                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         if (ok) {
206                 bpph_json = bpph_list_to_json(result);
207                 fcache_put(key, json_object_to_json_string(bpph_json));
208                 json_object_put(bpph_json);
209         }
210
211         free(key);
212
213         return result;
214 }
215
216 int get_download_count(const char *archive_url)
217 {
218         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
219         char *url = malloc(n);
220         int result;
221         json_object *obj;
222
223         strcpy(url, archive_url);
224         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
225
226         obj = get_json_object(url);
227         free(url);
228
229         if (!obj)
230                 return -1;
231
232         result = json_object_get_int(obj);
233
234         json_object_put(obj);
235
236         return result;
237 }
238
239 const struct distro_arch_series *get_distro_arch_series(const char *url)
240 {
241         json_object *obj;
242         const struct distro_arch_series *distro;
243         char *content;
244
245         distro = cache_get(url);
246         if (distro)
247                 return (struct distro_arch_series *)distro;
248
249         content = get_url_content(url, 1);
250
251         if (!content)
252                 return NULL;
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         char *content;
275
276         distro = cache_get(url);
277         if (distro)
278                 return (struct distro_series *)distro;
279
280         content = get_url_content(url, 1);
281
282         if (!content)
283                 return NULL;
284
285         obj = json_tokener_parse(content);
286
287         free(content);
288
289         if (!obj)
290                 return NULL;
291
292         distro = json_object_to_distro_series(obj);
293
294         json_object_put(obj);
295
296         cache_put(url, distro, (void (*)(void *))&distro_series_free);
297
298         return distro;
299 }
300
301 struct daily_download_total **get_daily_download_totals(const char *binary_url)
302 {
303         char *url;
304         json_object *obj;
305         struct daily_download_total **result = NULL;
306
307         url = malloc(strlen(binary_url)+
308                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
309
310         strcpy(url, binary_url);
311         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
312
313         obj = get_json_object(url);
314
315         if (obj) {
316                 result = json_object_to_daily_download_totals(obj);
317                 json_object_put(obj);
318         }
319
320         free(url);
321
322         return result;
323 }
324