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 #define QUERY_GET_PUBLISHED_BINARIES \
34 "?ws.op=getPublishedBinaries"
35 #define QUERY_GET_DOWNLOAD_COUNT "?ws.op=getDownloadCount"
36 #define QUERY_GET_DAILY_DOWNLOAD_TOTALS \
37 "?ws.op=getDailyDownloadTotals"
39 static int DEFAULT_FETCH_RETRIES = 3;
48 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
50 size_t realsize = size * nmemb;
51 struct ucontent *mem = (struct ucontent *)userp;
53 mem->data = realloc(mem->data, mem->len + realsize + 1);
55 memcpy(&(mem->data[mem->len]), buffer, realsize);
57 mem->data[mem->len] = 0;
62 static char *fetch_url(const char *url)
64 struct ucontent *content = malloc(sizeof(struct ucontent));
70 printf("DEBUG: fetch_url %s\n", url);
74 printf("DEBUG: initializing CURL\n");
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 fprintf(stderr, "Fetch failed: %ld\n", code);
112 printf("Wait 5s before retry.\n");
120 fprintf(stderr, "Fetch failed: %ld\n", code);
132 static json_object *get_json_object(const char *url)
134 json_object *obj = NULL;
137 body = fetch_url(url);
140 obj = json_tokener_parse(body);
150 #define json_object_to_bpph_list \
151 json_object_to_binary_package_publishing_history_list
153 struct binary_package_publishing_history * *
154 get_binary_package_publishing_history_list(const char *archive_url,
155 const char *pkg_status)
157 struct json_object *o_next;
160 void **result = NULL;
162 url = malloc(strlen(archive_url)+
163 strlen(QUERY_GET_PUBLISHED_BINARIES)+
164 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
167 strcpy(url, archive_url);
168 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
171 strcat(url, "&status=");
172 strcat(url, pkg_status);
176 o = get_json_object(url);
183 result = list_append_list(result,
184 (void **)json_object_to_bpph_list(o));
186 o_next = json_object_object_get(o, "next_collection_link");
189 url = strdup(json_object_get_string(o_next));
194 return (struct binary_package_publishing_history **)result;
197 int get_download_count(const char *archive_url)
199 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
200 char *url = malloc(n);
204 strcpy(url, archive_url);
205 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
207 obj = get_json_object(url);
213 result = json_object_get_int(obj);
215 json_object_put(obj);
220 const struct distro_arch_series *get_distro_arch_series(const char *url)
223 const struct distro_arch_series *distro;
225 distro = cache_get(url);
227 return (struct distro_arch_series *)distro;
229 obj = get_json_object(url);
234 distro = json_object_to_distro_arch_series(obj);
236 json_object_put(obj);
238 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
243 const struct distro_series *get_distro_series(const char *url)
246 const struct distro_series *distro;
248 distro = cache_get(url);
250 return (struct distro_series *)distro;
252 obj = get_json_object(url);
257 distro = json_object_to_distro_series(obj);
259 json_object_put(obj);
261 cache_put(url, distro, (void (*)(void *))&distro_series_free);
266 struct daily_download_total **get_daily_download_totals(const char *binary_url)
270 struct daily_download_total **result = NULL;
272 url = malloc(strlen(binary_url)+
273 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
275 strcpy(url, binary_url);
276 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
278 obj = get_json_object(url);
281 result = json_object_to_daily_download_totals(obj);
282 json_object_put(obj);
293 printf("DEBUG: cleanup CURL\n");
295 curl_easy_cleanup(curl);
296 curl_global_cleanup();