added package status option
[ppastats.git] / src / lp_ws.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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 <stdlib.h>
21 #include <string.h>
22
23 #include <curl/curl.h>
24 #include <json/json.h>
25
26 #include "cache.h"
27 #include "lp_ws.h"
28 #include "lp_json.h"
29 #include "ppastats.h"
30
31 #define QUERY_GET_PUBLISHED_BINARIES \
32         "?ws.op=getPublishedBinaries"
33 #define QUERY_GET_DOWNLOAD_COUNT "?ws.op=getDownloadCount"
34 #define QUERY_GET_DAILY_DOWNLOAD_TOTALS \
35         "?ws.op=getDailyDownloadTotals"
36
37 static CURL *curl;
38
39 struct ucontent {
40         char *data;
41         size_t len;
42 };
43
44 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
45 {
46         size_t realsize = size * nmemb;
47         struct ucontent *mem = (struct ucontent *)userp;
48
49         mem->data = realloc(mem->data, mem->len + realsize + 1);
50
51         memcpy(&(mem->data[mem->len]), buffer, realsize);
52         mem->len += realsize;
53         mem->data[mem->len] = 0;
54
55         return realsize;
56 }
57
58 static char *fetch_url(const char *url)
59 {
60         struct ucontent *content = malloc(sizeof(struct ucontent));
61         char *result = NULL;
62         long code;
63
64         if (debug)
65                 printf("DEBUG: fetch_url %s\n", url);
66
67         if (!curl) {
68                 curl_global_init(CURL_GLOBAL_ALL);
69                 curl = curl_easy_init();
70         }
71
72         if (!curl)
73                 exit(EXIT_FAILURE);
74
75         content->data = malloc(1);
76         content->data[0] = '\0';
77         content->len = 0;
78
79         curl_easy_setopt(curl, CURLOPT_URL, url);
80         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
81         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
82         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
83         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
84
85         if (curl_easy_perform(curl) == CURLE_OK) {
86
87                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
88                 if (code == 200)
89                         result = content->data;
90         }
91
92         if (!result)
93                 free(content->data);
94
95         free(content);
96
97         return result;
98 }
99
100 static json_object *get_json_object(const char *url)
101 {
102         json_object *obj = NULL;
103         char *body;
104
105         body = fetch_url(url);
106
107         if (body) {
108                 obj = json_tokener_parse(body);
109
110                 free(body);
111
112                 return obj;
113         }
114
115         return NULL;
116 }
117
118 struct binary_package_publishing_history * *
119 get_binary_package_publishing_history_list(const char *archive_url,
120                                            const char *package_status)
121 {
122         char *url = malloc(strlen(archive_url)+
123                            strlen(QUERY_GET_PUBLISHED_BINARIES)+
124                            strlen("&status=")+
125                            9+
126                            1);
127         json_object *o;
128         struct binary_package_publishing_history **result;
129
130         strcpy(url, archive_url);
131         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
132
133
134         if (package_status) {
135                 strcat(url, "&status=");
136                 strcat(url, package_status);
137         }
138
139         o = get_json_object(url);
140         free(url);
141
142         if (!o)
143                 return NULL;
144
145         result = json_object_to_binary_package_publishing_history_list(o);
146
147         json_object_put(o);
148
149         return result;
150 }
151
152 int get_download_count(const char *archive_url)
153 {
154         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
155         char *url = malloc(n);
156         int result;
157         json_object *obj;
158
159         strcpy(url, archive_url);
160         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
161
162         obj = get_json_object(url);
163         free(url);
164
165         if (!obj)
166                 return -1;
167
168         result = json_object_get_int(obj);
169
170         json_object_put(obj);
171
172         return result;
173 }
174
175 const struct distro_arch_series *get_distro_arch_series(const char *url)
176 {
177         json_object *obj;
178         const struct distro_arch_series *distro;
179
180         distro = cache_get(url);
181         if (distro)
182                 return (struct distro_arch_series *)distro;
183
184         obj = get_json_object(url);
185
186         if (!obj)
187                 return NULL;
188
189         distro = json_object_to_distro_arch_series(obj);
190
191         json_object_put(obj);
192
193         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
194
195         return distro;
196 }
197
198 const struct distro_series *get_distro_series(const char *url)
199 {
200         json_object *obj;
201         const struct distro_series *distro;
202
203         distro = cache_get(url);
204         if (distro)
205                 return (struct distro_series *)distro;
206
207         obj = get_json_object(url);
208
209         if (!obj)
210                 return NULL;
211
212         distro = json_object_to_distro_series(obj);
213
214         json_object_put(obj);
215
216         cache_put(url, distro, (void (*)(void *))&distro_series_free);
217
218         return distro;
219 }
220
221 struct daily_download_total **get_daily_download_totals(const char *binary_url)
222 {
223         char *url;
224         json_object *obj;
225         struct daily_download_total **result = NULL;
226
227         url = malloc(strlen(binary_url)+
228                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
229
230         strcpy(url, binary_url);
231         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
232
233         obj = get_json_object(url);
234
235         if (obj) {
236                 result = json_object_to_daily_download_totals(obj);
237                 json_object_put(obj);
238         }
239
240         free(url);
241
242         return result;
243 }
244
245 void lp_ws_cleanup()
246 {
247         if (debug)
248                 printf("DEBUG: cleanup CURL\n");
249
250         curl_easy_cleanup(curl);
251         curl_global_cleanup();
252 }