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
24 #include <curl/curl.h>
25 #include <json/json.h>
33 static const char *QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries";
34 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
36 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
38 static const int DEFAULT_FETCH_RETRIES = 3;
47 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
49 size_t realsize = size * nmemb;
50 struct ucontent *mem = (struct ucontent *)userp;
52 mem->data = realloc(mem->data, mem->len + realsize + 1);
54 memcpy(&(mem->data[mem->len]), buffer, realsize);
56 mem->data[mem->len] = 0;
61 static char *fetch_url(const char *url)
63 struct ucontent *content = malloc(sizeof(struct ucontent));
69 printf("DEBUG: fetch_url %s\n", url);
73 printf("DEBUG: initializing CURL\n");
74 curl_global_init(CURL_GLOBAL_ALL);
75 curl = curl_easy_init();
83 retries = DEFAULT_FETCH_RETRIES;
86 content->data = malloc(1);
87 content->data[0] = '\0';
90 curl_easy_setopt(curl, CURLOPT_URL, url);
91 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
92 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
93 curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
94 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
96 if (curl_easy_perform(curl) == CURLE_OK) {
97 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
101 result = content->data;
109 "Fetch failed: with code %ld "
115 printf("Wait 5s before retry.\n");
123 fprintf(stderr, "Fetch failed: %ld\n", code);
135 static json_object *get_json_object(const char *url)
137 json_object *obj = NULL;
140 body = fetch_url(url);
143 obj = json_tokener_parse(body);
153 #define json_object_to_bpph_list \
154 json_object_to_binary_package_publishing_history_list
156 struct binary_package_publishing_history * *
157 get_binary_package_publishing_history_list(const char *archive_url,
158 const char *pkg_status)
160 struct json_object *o_next;
163 void **result = NULL;
165 url = malloc(strlen(archive_url)+
166 strlen(QUERY_GET_PUBLISHED_BINARIES)+
167 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
170 strcpy(url, archive_url);
171 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
174 strcat(url, "&status=");
175 strcat(url, pkg_status);
179 o = get_json_object(url);
186 result = list_append_list(result,
187 (void **)json_object_to_bpph_list(o));
189 o_next = json_object_object_get(o, "next_collection_link");
192 url = strdup(json_object_get_string(o_next));
197 return (struct binary_package_publishing_history **)result;
200 int get_download_count(const char *archive_url)
202 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
203 char *url = malloc(n);
207 strcpy(url, archive_url);
208 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
210 obj = get_json_object(url);
216 result = json_object_get_int(obj);
218 json_object_put(obj);
223 const struct distro_arch_series *get_distro_arch_series(const char *url)
226 const struct distro_arch_series *distro;
228 distro = cache_get(url);
230 return (struct distro_arch_series *)distro;
232 obj = get_json_object(url);
237 distro = json_object_to_distro_arch_series(obj);
239 json_object_put(obj);
241 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
246 const struct distro_series *get_distro_series(const char *url)
249 const struct distro_series *distro;
251 distro = cache_get(url);
253 return (struct distro_series *)distro;
255 obj = get_json_object(url);
260 distro = json_object_to_distro_series(obj);
262 json_object_put(obj);
264 cache_put(url, distro, (void (*)(void *))&distro_series_free);
269 struct daily_download_total **get_daily_download_totals(const char *binary_url)
273 struct daily_download_total **result = NULL;
275 url = malloc(strlen(binary_url)+
276 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
278 strcpy(url, binary_url);
279 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
281 obj = get_json_object(url);
284 result = json_object_to_daily_download_totals(obj);
285 json_object_put(obj);
296 printf("DEBUG: cleanup CURL\n");
298 curl_easy_cleanup(curl);
299 curl_global_cleanup();