renaming to 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 <stdlib.h>
24 #include <string.h>
25
26 #include <json/json.h>
27
28 #include "cache.h"
29 #include "fcache.h"
30 #include "http.h"
31 #include "list.h"
32 #include "log.h"
33 #include "lp_ws.h"
34 #include "lp_json.h"
35 #include "ppastats.h"
36
37 static const char *
38 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=150";
39 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
40 static const char *
41 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
42
43 static json_object *get_json_object(const char *url)
44 {
45         json_object *obj = NULL;
46         char *body;
47
48         body = get_url_content(url, 0);
49
50         if (body) {
51                 obj = json_tokener_parse(body);
52
53                 free(body);
54
55                 return obj;
56         }
57
58         return NULL;
59 }
60
61 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
62 {
63         struct json_object *o_next;
64         char *url;
65         json_object *o;
66         void **result = NULL;
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 = list_append_list(result,
90                                           (void **)json_object_to_bpph_list(o));
91
92                 o_next = json_object_object_get(o, "next_collection_link");
93
94                 if (o_next)
95                         url = strdup(json_object_get_string(o_next));
96
97                 json_object_put(o);
98         }
99
100         return (struct bpph **)result;
101 }
102
103 int get_download_count(const char *archive_url)
104 {
105         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
106         char *url = malloc(n);
107         int result;
108         json_object *obj;
109
110         strcpy(url, archive_url);
111         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
112
113         obj = get_json_object(url);
114         free(url);
115
116         if (!obj)
117                 return -1;
118
119         result = json_object_get_int(obj);
120
121         json_object_put(obj);
122
123         return result;
124 }
125
126 const struct distro_arch_series *get_distro_arch_series(const char *url)
127 {
128         json_object *obj;
129         const struct distro_arch_series *distro;
130         char *content;
131
132         distro = cache_get(url);
133         if (distro)
134                 return (struct distro_arch_series *)distro;
135
136         content = get_url_content(url, 1);
137
138         if (!content)
139                 return NULL;
140
141         obj = json_tokener_parse(content);
142
143         free(content);
144
145         if (!obj)
146                 return NULL;
147
148         distro = json_object_to_distro_arch_series(obj);
149
150         json_object_put(obj);
151
152         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
153
154         return distro;
155 }
156
157 const struct distro_series *get_distro_series(const char *url)
158 {
159         json_object *obj;
160         const struct distro_series *distro;
161         char *content;
162
163         distro = cache_get(url);
164         if (distro)
165                 return (struct distro_series *)distro;
166
167         content = get_url_content(url, 1);
168
169         if (!content)
170                 return NULL;
171
172         obj = json_tokener_parse(content);
173
174         free(content);
175
176         if (!obj)
177                 return NULL;
178
179         distro = json_object_to_distro_series(obj);
180
181         json_object_put(obj);
182
183         cache_put(url, distro, (void (*)(void *))&distro_series_free);
184
185         return distro;
186 }
187
188 struct daily_download_total **get_daily_download_totals(const char *binary_url)
189 {
190         char *url;
191         json_object *obj;
192         struct daily_download_total **result = NULL;
193
194         url = malloc(strlen(binary_url)+
195                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
196
197         strcpy(url, binary_url);
198         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
199
200         obj = get_json_object(url);
201
202         if (obj) {
203                 result = json_object_to_daily_download_totals(obj);
204                 json_object_put(obj);
205         }
206
207         free(url);
208
209         return result;
210 }
211