2 * Copyright (C) 2011-2012 jeanfi@gmail.com
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.
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.
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
21 #define _(String) gettext(String)
27 #include <curl/curl.h>
28 #include <json/json.h>
39 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=300";
40 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
42 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
44 static const int DEFAULT_FETCH_RETRIES = 10;
53 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
55 size_t realsize = size * nmemb;
56 struct ucontent *mem = (struct ucontent *)userp;
58 mem->data = realloc(mem->data, mem->len + realsize + 1);
60 memcpy(&(mem->data[mem->len]), buffer, realsize);
62 mem->data[mem->len] = 0;
70 log_debug(_("initializing CURL"));
71 curl_global_init(CURL_GLOBAL_ALL);
72 curl = curl_easy_init();
79 static char *fetch_url(const char *url)
81 struct ucontent *content = malloc(sizeof(struct ucontent));
87 log_debug(_("fetch_url(): %s"), url);
93 retries = DEFAULT_FETCH_RETRIES;
96 content->data = malloc(1);
97 content->data[0] = '\0';
100 curl_easy_setopt(curl, CURLOPT_URL, url);
101 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
102 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
103 curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
104 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
106 if (curl_easy_perform(curl) == CURLE_OK) {
107 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
111 result = content->data;
117 log_err(_("Fetch failed with code %ld for URL %s"),
122 s = 2 * (DEFAULT_FETCH_RETRIES - retries) + 2;
123 log_debug(_("Wait %ds before retry"), s);
133 log_err(_("Fetch failed with code %ld for URL %s"),
147 static json_object *get_json_object(const char *url)
149 json_object *obj = NULL;
152 body = fetch_url(url);
155 obj = json_tokener_parse(body);
165 #define json_object_to_bpph_list \
166 json_object_to_binary_package_publishing_history_list
168 struct binary_package_publishing_history * *
169 get_binary_package_publishing_history_list(const char *archive_url,
170 const char *pkg_status)
172 struct json_object *o_next;
175 void **result = NULL;
177 url = malloc(strlen(archive_url)+
178 strlen(QUERY_GET_PUBLISHED_BINARIES)+
179 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
182 strcpy(url, archive_url);
183 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
186 strcat(url, "&status=");
187 strcat(url, pkg_status);
191 o = get_json_object(url);
198 result = list_append_list(result,
199 (void **)json_object_to_bpph_list(o));
201 o_next = json_object_object_get(o, "next_collection_link");
204 url = strdup(json_object_get_string(o_next));
209 return (struct binary_package_publishing_history **)result;
212 int get_download_count(const char *archive_url)
214 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
215 char *url = malloc(n);
219 strcpy(url, archive_url);
220 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
222 obj = get_json_object(url);
228 result = json_object_get_int(obj);
230 json_object_put(obj);
235 const struct distro_arch_series *get_distro_arch_series(const char *url)
238 const struct distro_arch_series *distro;
241 distro = cache_get(url);
243 return (struct distro_arch_series *)distro;
245 content = fcache_get(url + 7);
247 content = fetch_url(url);
249 fcache_put(url + 7, content);
254 obj = json_tokener_parse(content);
261 distro = json_object_to_distro_arch_series(obj);
263 json_object_put(obj);
265 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
270 const struct distro_series *get_distro_series(const char *url)
273 const struct distro_series *distro;
275 distro = cache_get(url);
277 return (struct distro_series *)distro;
279 obj = get_json_object(url);
284 distro = json_object_to_distro_series(obj);
286 json_object_put(obj);
288 cache_put(url, distro, (void (*)(void *))&distro_series_free);
293 struct daily_download_total **get_daily_download_totals(const char *binary_url)
297 struct daily_download_total **result = NULL;
299 url = malloc(strlen(binary_url)+
300 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
302 strcpy(url, binary_url);
303 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
305 obj = get_json_object(url);
308 result = json_object_to_daily_download_totals(obj);
309 json_object_put(obj);
319 log_debug(_("cleanup CURL"));
321 curl_easy_cleanup(curl);
322 curl_global_cleanup();