save bpph list into file cache
[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 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
63 {
64         char *url, *bpph_key;
65         struct bpph **result = NULL;
66         struct json_object *o, *bpph_json, *o_next;
67
68         url = malloc(strlen(archive_url)+
69                      strlen(QUERY_GET_PUBLISHED_BINARIES)+
70                      (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
71                      1);
72
73         strcpy(url, archive_url);
74         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
75
76         if (pkg_status) {
77                 strcat(url, "&status=");
78                 strcat(url, pkg_status);
79         }
80
81         while (url) {
82                 o = get_json_object(url);
83                 free(url);
84                 url = NULL;
85
86                 if (!o)
87                         break;
88
89                 result = (struct bpph **)list_append_list
90                         ((void **)result,
91                          (void **)json_object_to_bpph_list(o));
92
93                 o_next = json_object_object_get(o, "next_collection_link");
94
95                 if (o_next)
96                         url = strdup(json_object_get_string(o_next));
97
98                 json_object_put(o);
99         }
100
101         bpph_json = bpph_list_to_json(result);
102
103         bpph_key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
104         sprintf(bpph_key, "%s/bpph", archive_url + 7);
105         fcache_put(bpph_key, json_object_to_json_string(bpph_json));
106
107         json_object_put(bpph_json);
108         free(bpph_key);
109
110         return result;
111 }
112
113 int get_download_count(const char *archive_url)
114 {
115         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
116         char *url = malloc(n);
117         int result;
118         json_object *obj;
119
120         strcpy(url, archive_url);
121         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
122
123         obj = get_json_object(url);
124         free(url);
125
126         if (!obj)
127                 return -1;
128
129         result = json_object_get_int(obj);
130
131         json_object_put(obj);
132
133         return result;
134 }
135
136 const struct distro_arch_series *get_distro_arch_series(const char *url)
137 {
138         json_object *obj;
139         const struct distro_arch_series *distro;
140         char *content;
141
142         distro = cache_get(url);
143         if (distro)
144                 return (struct distro_arch_series *)distro;
145
146         content = get_url_content(url, 1);
147
148         if (!content)
149                 return NULL;
150
151         obj = json_tokener_parse(content);
152
153         free(content);
154
155         if (!obj)
156                 return NULL;
157
158         distro = json_object_to_distro_arch_series(obj);
159
160         json_object_put(obj);
161
162         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
163
164         return distro;
165 }
166
167 const struct distro_series *get_distro_series(const char *url)
168 {
169         json_object *obj;
170         const struct distro_series *distro;
171         char *content;
172
173         distro = cache_get(url);
174         if (distro)
175                 return (struct distro_series *)distro;
176
177         content = get_url_content(url, 1);
178
179         if (!content)
180                 return NULL;
181
182         obj = json_tokener_parse(content);
183
184         free(content);
185
186         if (!obj)
187                 return NULL;
188
189         distro = json_object_to_distro_series(obj);
190
191         json_object_put(obj);
192
193         cache_put(url, distro, (void (*)(void *))&distro_series_free);
194
195         return distro;
196 }
197
198 struct daily_download_total **get_daily_download_totals(const char *binary_url)
199 {
200         char *url;
201         json_object *obj;
202         struct daily_download_total **result = NULL;
203
204         url = malloc(strlen(binary_url)+
205                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
206
207         strcpy(url, binary_url);
208         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
209
210         obj = get_json_object(url);
211
212         if (obj) {
213                 result = json_object_to_daily_download_totals(obj);
214                 json_object_put(obj);
215         }
216
217         free(url);
218
219         return result;
220 }
221