load bpph from cache and merge it the http request result
[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 static 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 static 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 bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
95 {
96         char *url, *key;
97         struct bpph **result = NULL;
98         struct json_object *o, *bpph_json, *o_next;
99
100         url = malloc(strlen(archive_url)+
101                      strlen(QUERY_GET_PUBLISHED_BINARIES)+
102                      (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
103                      1);
104
105         strcpy(url, archive_url);
106         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
107
108         if (pkg_status) {
109                 strcat(url, "&status=");
110                 strcat(url, pkg_status);
111         }
112
113         key = get_bpph_list_cache_key(archive_url);
114         result = get_bpph_list_from_cache(key);
115
116         while (url) {
117                 o = get_json_object(url);
118                 free(url);
119                 url = NULL;
120
121                 if (!o)
122                         break;
123
124                 result = bpph_list_append_list(result,
125                                                json_object_to_bpph_list(o));
126
127                 o_next = json_object_object_get(o, "next_collection_link");
128
129                 if (o_next)
130                         url = strdup(json_object_get_string(o_next));
131
132                 json_object_put(o);
133         }
134
135         bpph_json = bpph_list_to_json(result);
136
137         fcache_put(key, json_object_to_json_string(bpph_json));
138
139         json_object_put(bpph_json);
140         free(key);
141
142         return result;
143 }
144
145 int get_download_count(const char *archive_url)
146 {
147         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
148         char *url = malloc(n);
149         int result;
150         json_object *obj;
151
152         strcpy(url, archive_url);
153         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
154
155         obj = get_json_object(url);
156         free(url);
157
158         if (!obj)
159                 return -1;
160
161         result = json_object_get_int(obj);
162
163         json_object_put(obj);
164
165         return result;
166 }
167
168 const struct distro_arch_series *get_distro_arch_series(const char *url)
169 {
170         json_object *obj;
171         const struct distro_arch_series *distro;
172         char *content;
173
174         distro = cache_get(url);
175         if (distro)
176                 return (struct distro_arch_series *)distro;
177
178         content = get_url_content(url, 1);
179
180         if (!content)
181                 return NULL;
182
183         obj = json_tokener_parse(content);
184
185         free(content);
186
187         if (!obj)
188                 return NULL;
189
190         distro = json_object_to_distro_arch_series(obj);
191
192         json_object_put(obj);
193
194         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
195
196         return distro;
197 }
198
199 const struct distro_series *get_distro_series(const char *url)
200 {
201         json_object *obj;
202         const struct distro_series *distro;
203         char *content;
204
205         distro = cache_get(url);
206         if (distro)
207                 return (struct distro_series *)distro;
208
209         content = get_url_content(url, 1);
210
211         if (!content)
212                 return NULL;
213
214         obj = json_tokener_parse(content);
215
216         free(content);
217
218         if (!obj)
219                 return NULL;
220
221         distro = json_object_to_distro_series(obj);
222
223         json_object_put(obj);
224
225         cache_put(url, distro, (void (*)(void *))&distro_series_free);
226
227         return distro;
228 }
229
230 struct daily_download_total **get_daily_download_totals(const char *binary_url)
231 {
232         char *url;
233         json_object *obj;
234         struct daily_download_total **result = NULL;
235
236         url = malloc(strlen(binary_url)+
237                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
238
239         strcpy(url, binary_url);
240         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
241
242         obj = get_json_object(url);
243
244         if (obj) {
245                 result = json_object_to_daily_download_totals(obj);
246                 json_object_put(obj);
247         }
248
249         free(url);
250
251         return result;
252 }
253