use cache for bpph
[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 static const char *
39 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=150";
40 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
41 static const char *
42 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
43
44 static json_object *get_json_object(const char *url)
45 {
46         json_object *obj = NULL;
47         char *body;
48
49         body = get_url_content(url, 0);
50
51         if (body) {
52                 obj = json_tokener_parse(body);
53
54                 free(body);
55
56                 return obj;
57         }
58
59         return NULL;
60 }
61
62 char *get_bpph_list_cache_key(const char *archive_url)
63 {
64         char *key;
65
66         key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
67         sprintf(key, "%s/bpph", archive_url + 7);
68
69         return key;
70 }
71
72 struct bpph **get_bpph_list_from_cache(const char *key)
73 {
74         char *content;
75         struct bpph **list;
76         json_object *json;
77
78         content = fcache_get(key);
79         if (!content)
80                 return NULL;
81
82         json = json_tokener_parse(content);
83         if (!json)
84                 return NULL;
85
86         list = json_object_to_bpph_list(json);
87
88         json_object_put(json);
89         free(content);
90
91         return list;
92 }
93
94 struct tm *get_last_creation_date(struct bpph **list)
95 {
96         time_t last, t;
97         struct bpph **cur;
98
99         last = 0;
100
101         if (list)
102                 for (cur = list; *cur; cur++) {
103                         t = mktime(&(*cur)->date_created);
104                         if (t > last)
105                                 last = t;
106                 }
107
108         return localtime(&last);
109 }
110
111 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
112 {
113         char *url, *key, *tmp;
114         struct bpph **result = NULL;
115         struct json_object *o, *bpph_json, *o_next;
116         char *created_since_date;
117         struct tm *tm;
118         int ok;
119
120         url = malloc(strlen(archive_url)
121                 + strlen(QUERY_GET_PUBLISHED_BINARIES)
122                 + 1);
123         strcpy(url, archive_url);
124         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
125
126         key = get_bpph_list_cache_key(archive_url);
127
128         result = get_bpph_list_from_cache(key);
129
130         if (result) {
131                 tm = get_last_creation_date(result);
132
133                 created_since_date = malloc(200);
134                 strftime(created_since_date,
135                          100,
136                          "%FT%T",
137                          tm);
138
139                 printf("Update package since: %s\n", created_since_date);
140
141                 tmp = malloc(strlen(url)
142                              + strlen("&created_since_date=")
143                              + strlen(created_since_date)+1);
144                 strcpy(tmp, url);
145                 strcat(tmp, "&created_since_date=");
146                 strcat(tmp, created_since_date);
147
148                 free(url);
149                 url = tmp;
150
151                 free(created_since_date);
152         }
153
154         ok = 1;
155         while (url) {
156                 o = get_json_object(url);
157                 free(url);
158                 url = NULL;
159
160                 if (!o) {
161                         ok = 0;
162                         break;
163                 }
164
165                 result = bpph_list_append_list(result,
166                                                json_object_to_bpph_list(o));
167
168                 o_next = json_object_object_get(o, "next_collection_link");
169
170                 if (o_next)
171                         url = strdup(json_object_get_string(o_next));
172
173                 json_object_put(o);
174
175         }
176
177         if (ok) {
178                 bpph_json = bpph_list_to_json(result);
179                 fcache_put(key, json_object_to_json_string(bpph_json));
180                 json_object_put(bpph_json);
181         }
182
183         free(key);
184
185         return result;
186 }
187
188 int get_download_count(const char *archive_url)
189 {
190         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
191         char *url = malloc(n);
192         int result;
193         json_object *obj;
194
195         strcpy(url, archive_url);
196         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
197
198         obj = get_json_object(url);
199         free(url);
200
201         if (!obj)
202                 return -1;
203
204         result = json_object_get_int(obj);
205
206         json_object_put(obj);
207
208         return result;
209 }
210
211 const struct distro_arch_series *get_distro_arch_series(const char *url)
212 {
213         json_object *obj;
214         const struct distro_arch_series *distro;
215         char *content;
216
217         distro = cache_get(url);
218         if (distro)
219                 return (struct distro_arch_series *)distro;
220
221         content = get_url_content(url, 1);
222
223         if (!content)
224                 return NULL;
225
226         obj = json_tokener_parse(content);
227
228         free(content);
229
230         if (!obj)
231                 return NULL;
232
233         distro = json_object_to_distro_arch_series(obj);
234
235         json_object_put(obj);
236
237         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
238
239         return distro;
240 }
241
242 const struct distro_series *get_distro_series(const char *url)
243 {
244         json_object *obj;
245         const struct distro_series *distro;
246         char *content;
247
248         distro = cache_get(url);
249         if (distro)
250                 return (struct distro_series *)distro;
251
252         content = get_url_content(url, 1);
253
254         if (!content)
255                 return NULL;
256
257         obj = json_tokener_parse(content);
258
259         free(content);
260
261         if (!obj)
262                 return NULL;
263
264         distro = json_object_to_distro_series(obj);
265
266         json_object_put(obj);
267
268         cache_put(url, distro, (void (*)(void *))&distro_series_free);
269
270         return distro;
271 }
272
273 struct daily_download_total **get_daily_download_totals(const char *binary_url)
274 {
275         char *url;
276         json_object *obj;
277         struct daily_download_total **result = NULL;
278
279         url = malloc(strlen(binary_url)+
280                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
281
282         strcpy(url, binary_url);
283         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
284
285         obj = get_json_object(url);
286
287         if (obj) {
288                 result = json_object_to_daily_download_totals(obj);
289                 json_object_put(obj);
290         }
291
292         free(url);
293
294         return result;
295 }
296