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>
29 #define QUERY_GET_PUBLISHED_BINARIES \
30 "?ws.op=getPublishedBinaries&status=Published"
31 #define QUERY_GET_DOWNLOAD_COUNT "?ws.op=getDownloadCount"
32 #define QUERY_GET_DAILY_DOWNLOAD_TOTALS \
33 "?ws.op=getDailyDownloadTotals"
42 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44 size_t realsize = size * nmemb;
45 struct ucontent *mem = (struct ucontent *)userp;
47 mem->data = realloc(mem->data, mem->len + realsize + 1);
49 memcpy(&(mem->data[mem->len]), buffer, realsize);
51 mem->data[mem->len] = 0;
56 static char *fetch_url(const char *url)
58 struct ucontent *content = malloc(sizeof(struct ucontent));
63 curl = curl_easy_init();
68 content->data = malloc(1);
69 content->data[0] = '\0';
72 curl_easy_setopt(curl, CURLOPT_URL, url);
73 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
74 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
75 curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
76 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
78 if (curl_easy_perform(curl) == CURLE_OK) {
80 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
82 result = content->data;
93 static json_object *get_json_object(const char *url)
95 json_object *obj = NULL;
98 body = fetch_url(url);
101 obj = json_tokener_parse(body);
111 struct binary_package_publishing_history * *
112 get_binary_package_publishing_history_list(const char *archive_url)
114 char *url = malloc(strlen(archive_url)+
115 strlen(QUERY_GET_PUBLISHED_BINARIES)+
118 struct binary_package_publishing_history **result;
120 strcpy(url, archive_url);
121 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
123 o = get_json_object(url);
129 result = json_object_to_binary_package_publishing_history_list(o);
136 int get_download_count(const char *archive_url)
138 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
139 char *url = malloc(n);
143 strcpy(url, archive_url);
144 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
146 obj = get_json_object(url);
152 result = json_object_get_int(obj);
154 json_object_put(obj);
159 struct distro_arch_series *get_distro_arch_series(const char *url)
162 struct distro_arch_series *distro;
164 obj = get_json_object(url);
169 distro = json_object_to_distro_arch_series(obj);
171 json_object_put(obj);
176 struct daily_download_total **get_daily_download_totals(const char *binary_url)
180 struct daily_download_total **result = NULL;
182 url = malloc(strlen(binary_url)+
183 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
185 strcpy(url, binary_url);
186 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
188 obj = get_json_object(url);
191 result = json_object_to_daily_download_totals(obj);
194 json_object_put(obj);