f57aa3b5ec9842a85f412009e1c36128dd2e5b1b
[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         int 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, const char *pkg_status)
148 {
149         char *url, *key, *tmp;
150         struct bpph **result;
151         struct json_object *o, *bpph_json, *o_next;
152         char *date;
153         int ok;
154
155         url = create_query_get_bpph(archive_url, pkg_status, -1);
156
157         key = get_bpph_list_cache_key(archive_url);
158
159         result = get_bpph_list_from_cache(key);
160
161         if (result) {
162                 date = get_last_creation_date(result);
163
164                 if (date) {
165                         printf("Update package since: %s\n", date);
166
167                         tmp = malloc(strlen(url)
168                                      + strlen("&created_since_date=")
169                                      + strlen(date)+1);
170                         strcpy(tmp, url);
171                         strcat(tmp, "&created_since_date=");
172                         strcat(tmp, date);
173
174                         free(url);
175                         url = tmp;
176
177                         free(date);
178                 }
179         }
180
181         ok = 1;
182         while (url) {
183                 o = get_json_object(url);
184                 free(url);
185                 url = NULL;
186
187                 if (!o) {
188                         ok = 0;
189                         break;
190                 }
191
192                 result = bpph_list_append_list(result,
193                                                json_object_to_bpph_list(o));
194
195                 o_next = json_object_object_get(o, "next_collection_link");
196
197                 if (o_next)
198                         url = strdup(json_object_get_string(o_next));
199
200                 json_object_put(o);
201         }
202
203         if (ok) {
204                 bpph_json = bpph_list_to_json(result);
205                 fcache_put(key, json_object_to_json_string(bpph_json));
206                 json_object_put(bpph_json);
207         }
208
209         free(key);
210
211         return result;
212 }
213
214 int get_download_count(const char *archive_url)
215 {
216         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
217         char *url = malloc(n);
218         int result;
219         json_object *obj;
220
221         strcpy(url, archive_url);
222         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
223
224         obj = get_json_object(url);
225         free(url);
226
227         if (!obj)
228                 return -1;
229
230         result = json_object_get_int(obj);
231
232         json_object_put(obj);
233
234         return result;
235 }
236
237 const struct distro_arch_series *get_distro_arch_series(const char *url)
238 {
239         json_object *obj;
240         const struct distro_arch_series *distro;
241         char *content;
242
243         distro = cache_get(url);
244         if (distro)
245                 return (struct distro_arch_series *)distro;
246
247         content = get_url_content(url, 1);
248
249         if (!content)
250                 return NULL;
251
252         obj = json_tokener_parse(content);
253
254         free(content);
255
256         if (!obj)
257                 return NULL;
258
259         distro = json_object_to_distro_arch_series(obj);
260
261         json_object_put(obj);
262
263         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
264
265         return distro;
266 }
267
268 const struct distro_series *get_distro_series(const char *url)
269 {
270         json_object *obj;
271         const struct distro_series *distro;
272         char *content;
273
274         distro = cache_get(url);
275         if (distro)
276                 return (struct distro_series *)distro;
277
278         content = get_url_content(url, 1);
279
280         if (!content)
281                 return NULL;
282
283         obj = json_tokener_parse(content);
284
285         free(content);
286
287         if (!obj)
288                 return NULL;
289
290         distro = json_object_to_distro_series(obj);
291
292         json_object_put(obj);
293
294         cache_put(url, distro, (void (*)(void *))&distro_series_free);
295
296         return distro;
297 }
298
299 struct daily_download_total **get_daily_download_totals(const char *binary_url)
300 {
301         char *url;
302         json_object *obj;
303         struct daily_download_total **result = NULL;
304
305         url = malloc(strlen(binary_url)+
306                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
307
308         strcpy(url, binary_url);
309         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
310
311         obj = get_json_object(url);
312
313         if (obj) {
314                 result = json_object_to_daily_download_totals(obj);
315                 json_object_put(obj);
316         }
317
318         free(url);
319
320         return result;
321 }
322