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