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
23 #include <curl/curl.h>
24 #include <json/json.h>
32 #define QUERY_GET_PUBLISHED_BINARIES \
33 "?ws.op=getPublishedBinaries"
34 #define QUERY_GET_DOWNLOAD_COUNT "?ws.op=getDownloadCount"
35 #define QUERY_GET_DAILY_DOWNLOAD_TOTALS \
36 "?ws.op=getDailyDownloadTotals"
45 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
47 size_t realsize = size * nmemb;
48 struct ucontent *mem = (struct ucontent *)userp;
50 mem->data = realloc(mem->data, mem->len + realsize + 1);
52 memcpy(&(mem->data[mem->len]), buffer, realsize);
54 mem->data[mem->len] = 0;
59 static char *fetch_url(const char *url)
61 struct ucontent *content = malloc(sizeof(struct ucontent));
66 printf("DEBUG: fetch_url %s\n", url);
69 curl_global_init(CURL_GLOBAL_ALL);
70 curl = curl_easy_init();
76 content->data = malloc(1);
77 content->data[0] = '\0';
80 curl_easy_setopt(curl, CURLOPT_URL, url);
81 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
82 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
83 curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
84 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
86 if (curl_easy_perform(curl) == CURLE_OK) {
88 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
90 result = content->data;
101 static json_object *get_json_object(const char *url)
103 json_object *obj = NULL;
106 body = fetch_url(url);
109 obj = json_tokener_parse(body);
119 #define json_object_to_bpph_list \
120 json_object_to_binary_package_publishing_history_list
122 struct binary_package_publishing_history * *
123 get_binary_package_publishing_history_list(const char *archive_url,
124 const char *pkg_status)
126 struct json_object *o_next;
129 void **result = NULL;
131 url = malloc(strlen(archive_url)+
132 strlen(QUERY_GET_PUBLISHED_BINARIES)+
133 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
136 strcpy(url, archive_url);
137 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
140 strcat(url, "&status=");
141 strcat(url, pkg_status);
145 o = get_json_object(url);
152 result = list_append_list(result,
153 (void **)json_object_to_bpph_list(o));
155 o_next = json_object_object_get(o, "next_collection_link");
158 url = strdup(json_object_get_string(o_next));
163 return (struct binary_package_publishing_history **)result;
166 int get_download_count(const char *archive_url)
168 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
169 char *url = malloc(n);
173 strcpy(url, archive_url);
174 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
176 obj = get_json_object(url);
182 result = json_object_get_int(obj);
184 json_object_put(obj);
189 const struct distro_arch_series *get_distro_arch_series(const char *url)
192 const struct distro_arch_series *distro;
194 distro = cache_get(url);
196 return (struct distro_arch_series *)distro;
198 obj = get_json_object(url);
203 distro = json_object_to_distro_arch_series(obj);
205 json_object_put(obj);
207 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
212 const struct distro_series *get_distro_series(const char *url)
215 const struct distro_series *distro;
217 distro = cache_get(url);
219 return (struct distro_series *)distro;
221 obj = get_json_object(url);
226 distro = json_object_to_distro_series(obj);
228 json_object_put(obj);
230 cache_put(url, distro, (void (*)(void *))&distro_series_free);
235 struct daily_download_total **get_daily_download_totals(const char *binary_url)
239 struct daily_download_total **result = NULL;
241 url = malloc(strlen(binary_url)+
242 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
244 strcpy(url, binary_url);
245 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
247 obj = get_json_object(url);
250 result = json_object_to_daily_download_totals(obj);
251 json_object_put(obj);
262 printf("DEBUG: cleanup CURL\n");
264 curl_easy_cleanup(curl);
265 curl_global_cleanup();