removed used local var initialization
[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 static char *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 = (*cur)->date_created;
104                         if (t > last)
105                                 last = t;
106                 }
107
108         if (last)
109                 return time_to_str(last);
110         else
111                 return NULL;
112 }
113
114 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
115 {
116         char *url, *key, *tmp;
117         struct bpph **result;
118         struct json_object *o, *bpph_json, *o_next;
119         char *created_since_date;
120         int ok;
121
122         url = malloc(strlen(archive_url)
123                      + strlen(QUERY_GET_PUBLISHED_BINARIES)
124                      + 1);
125         strcpy(url, archive_url);
126         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
127
128         key = get_bpph_list_cache_key(archive_url);
129
130         result = get_bpph_list_from_cache(key);
131
132         if (result) {
133                 created_since_date = get_last_creation_date(result);
134
135                 if (created_since_date) {
136                         printf("Update package since: %s\n",
137                                created_since_date);
138
139                         tmp = malloc(strlen(url)
140                                      + strlen("&created_since_date=")
141                                      + strlen(created_since_date)+1);
142                         strcpy(tmp, url);
143                         strcat(tmp, "&created_since_date=");
144                         strcat(tmp, created_since_date);
145
146                         free(url);
147                         url = tmp;
148
149                         free(created_since_date);
150                 }
151         }
152
153         ok = 1;
154         while (url) {
155                 o = get_json_object(url);
156                 free(url);
157                 url = NULL;
158
159                 if (!o) {
160                         ok = 0;
161                         break;
162                 }
163
164                 result = bpph_list_append_list(result,
165                                                json_object_to_bpph_list(o));
166
167                 o_next = json_object_object_get(o, "next_collection_link");
168
169                 if (o_next)
170                         url = strdup(json_object_get_string(o_next));
171
172                 json_object_put(o);
173         }
174
175         if (ok) {
176                 bpph_json = bpph_list_to_json(result);
177                 fcache_put(key, json_object_to_json_string(bpph_json));
178                 json_object_put(bpph_json);
179         }
180
181         free(key);
182
183         return result;
184 }
185
186 int get_download_count(const char *archive_url)
187 {
188         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
189         char *url = malloc(n);
190         int result;
191         json_object *obj;
192
193         strcpy(url, archive_url);
194         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
195
196         obj = get_json_object(url);
197         free(url);
198
199         if (!obj)
200                 return -1;
201
202         result = json_object_get_int(obj);
203
204         json_object_put(obj);
205
206         return result;
207 }
208
209 const struct distro_arch_series *get_distro_arch_series(const char *url)
210 {
211         json_object *obj;
212         const struct distro_arch_series *distro;
213         char *content;
214
215         distro = cache_get(url);
216         if (distro)
217                 return (struct distro_arch_series *)distro;
218
219         content = get_url_content(url, 1);
220
221         if (!content)
222                 return NULL;
223
224         obj = json_tokener_parse(content);
225
226         free(content);
227
228         if (!obj)
229                 return NULL;
230
231         distro = json_object_to_distro_arch_series(obj);
232
233         json_object_put(obj);
234
235         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
236
237         return distro;
238 }
239
240 const struct distro_series *get_distro_series(const char *url)
241 {
242         json_object *obj;
243         const struct distro_series *distro;
244         char *content;
245
246         distro = cache_get(url);
247         if (distro)
248                 return (struct distro_series *)distro;
249
250         content = get_url_content(url, 1);
251
252         if (!content)
253                 return NULL;
254
255         obj = json_tokener_parse(content);
256
257         free(content);
258
259         if (!obj)
260                 return NULL;
261
262         distro = json_object_to_distro_series(obj);
263
264         json_object_put(obj);
265
266         cache_put(url, distro, (void (*)(void *))&distro_series_free);
267
268         return distro;
269 }
270
271 struct daily_download_total **get_daily_download_totals(const char *binary_url)
272 {
273         char *url;
274         json_object *obj;
275         struct daily_download_total **result = NULL;
276
277         url = malloc(strlen(binary_url)+
278                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
279
280         strcpy(url, binary_url);
281         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
282
283         obj = get_json_object(url);
284
285         if (obj) {
286                 result = json_object_to_daily_download_totals(obj);
287                 json_object_put(obj);
288         }
289
290         free(url);
291
292         return result;
293 }
294