2 Copyright (C) 2011 jeanfi@gmail.com
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.
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.
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
20 #define _(String) gettext(String)
26 #include <curl/curl.h>
27 #include <json/json.h>
36 static const char *QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries";
37 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
39 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
41 static const int DEFAULT_FETCH_RETRIES = 3;
50 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
52 size_t realsize = size * nmemb;
53 struct ucontent *mem = (struct ucontent *)userp;
55 mem->data = realloc(mem->data, mem->len + realsize + 1);
57 memcpy(&(mem->data[mem->len]), buffer, realsize);
59 mem->data[mem->len] = 0;
64 static char *fetch_url(const char *url)
66 struct ucontent *content = malloc(sizeof(struct ucontent));
71 log_debug(_("fetch_url(): %s"), url);
74 log_debug(_("initializing CURL"));
75 curl_global_init(CURL_GLOBAL_ALL);
76 curl = curl_easy_init();
84 retries = DEFAULT_FETCH_RETRIES;
87 content->data = malloc(1);
88 content->data[0] = '\0';
91 curl_easy_setopt(curl, CURLOPT_URL, url);
92 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
93 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
94 curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
95 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
97 if (curl_easy_perform(curl) == CURLE_OK) {
98 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
102 result = content->data;
109 log_err(_("Fetch failed with code %ld "
114 log_debug(_("Wait 5s before retry"));
122 log_err(_("Fetch failed: %ld"), code);
134 static json_object *get_json_object(const char *url)
136 json_object *obj = NULL;
139 body = fetch_url(url);
142 obj = json_tokener_parse(body);
152 #define json_object_to_bpph_list \
153 json_object_to_binary_package_publishing_history_list
155 struct binary_package_publishing_history * *
156 get_binary_package_publishing_history_list(const char *archive_url,
157 const char *pkg_status)
159 struct json_object *o_next;
162 void **result = NULL;
164 url = malloc(strlen(archive_url)+
165 strlen(QUERY_GET_PUBLISHED_BINARIES)+
166 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
169 strcpy(url, archive_url);
170 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
173 strcat(url, "&status=");
174 strcat(url, pkg_status);
178 o = get_json_object(url);
185 result = list_append_list(result,
186 (void **)json_object_to_bpph_list(o));
188 o_next = json_object_object_get(o, "next_collection_link");
191 url = strdup(json_object_get_string(o_next));
196 return (struct binary_package_publishing_history **)result;
199 int get_download_count(const char *archive_url)
201 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
202 char *url = malloc(n);
206 strcpy(url, archive_url);
207 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
209 obj = get_json_object(url);
215 result = json_object_get_int(obj);
217 json_object_put(obj);
222 const struct distro_arch_series *get_distro_arch_series(const char *url)
225 const struct distro_arch_series *distro;
227 distro = cache_get(url);
229 return (struct distro_arch_series *)distro;
231 obj = get_json_object(url);
236 distro = json_object_to_distro_arch_series(obj);
238 json_object_put(obj);
240 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
245 const struct distro_series *get_distro_series(const char *url)
248 const struct distro_series *distro;
250 distro = cache_get(url);
252 return (struct distro_series *)distro;
254 obj = get_json_object(url);
259 distro = json_object_to_distro_series(obj);
261 json_object_put(obj);
263 cache_put(url, distro, (void (*)(void *))&distro_series_free);
268 struct daily_download_total **get_daily_download_totals(const char *binary_url)
272 struct daily_download_total **result = NULL;
274 url = malloc(strlen(binary_url)+
275 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
277 strcpy(url, binary_url);
278 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
280 obj = get_json_object(url);
283 result = json_object_to_daily_download_totals(obj);
284 json_object_put(obj);
294 log_debug(_("cleanup CURL"));
296 curl_easy_cleanup(curl);
297 curl_global_cleanup();