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 <json/json.h>
39 QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries&ws.size=150";
40 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
42 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
44 static json_object *get_json_object(const char *url)
46 json_object *obj = NULL;
49 body = get_url_content(url, 0);
52 obj = json_tokener_parse(body);
62 static char *get_bpph_list_cache_key(const char *archive_url)
66 key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
67 sprintf(key, "%s/bpph", archive_url + 7);
72 static struct bpph **get_bpph_list_from_cache(const char *key)
78 content = fcache_get(key);
82 json = json_tokener_parse(content);
86 list = json_object_to_bpph_list(json);
88 json_object_put(json);
94 static char *get_last_creation_date(struct bpph **list)
102 for (cur = list; *cur; cur++) {
103 t = (*cur)->date_created;
109 return time_to_str(last);
114 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
116 char *url, *key, *tmp;
117 struct bpph **result = NULL;
118 struct json_object *o, *bpph_json, *o_next;
119 char *created_since_date;
122 url = malloc(strlen(archive_url)
123 + strlen(QUERY_GET_PUBLISHED_BINARIES)
125 strcpy(url, archive_url);
126 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
128 key = get_bpph_list_cache_key(archive_url);
130 result = get_bpph_list_from_cache(key);
133 created_since_date = get_last_creation_date(result);
135 if (created_since_date) {
136 printf("Update package since: %s\n",
139 tmp = malloc(strlen(url)
140 + strlen("&created_since_date=")
141 + strlen(created_since_date)+1);
143 strcat(tmp, "&created_since_date=");
144 strcat(tmp, created_since_date);
149 free(created_since_date);
155 o = get_json_object(url);
164 result = bpph_list_append_list(result,
165 json_object_to_bpph_list(o));
167 o_next = json_object_object_get(o, "next_collection_link");
170 url = strdup(json_object_get_string(o_next));
177 bpph_json = bpph_list_to_json(result);
178 fcache_put(key, json_object_to_json_string(bpph_json));
179 json_object_put(bpph_json);
187 int get_download_count(const char *archive_url)
189 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
190 char *url = malloc(n);
194 strcpy(url, archive_url);
195 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
197 obj = get_json_object(url);
203 result = json_object_get_int(obj);
205 json_object_put(obj);
210 const struct distro_arch_series *get_distro_arch_series(const char *url)
213 const struct distro_arch_series *distro;
216 distro = cache_get(url);
218 return (struct distro_arch_series *)distro;
220 content = get_url_content(url, 1);
225 obj = json_tokener_parse(content);
232 distro = json_object_to_distro_arch_series(obj);
234 json_object_put(obj);
236 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
241 const struct distro_series *get_distro_series(const char *url)
244 const struct distro_series *distro;
247 distro = cache_get(url);
249 return (struct distro_series *)distro;
251 content = get_url_content(url, 1);
256 obj = json_tokener_parse(content);
263 distro = json_object_to_distro_series(obj);
265 json_object_put(obj);
267 cache_put(url, distro, (void (*)(void *))&distro_series_free);
272 struct daily_download_total **get_daily_download_totals(const char *binary_url)
276 struct daily_download_total **result = NULL;
278 url = malloc(strlen(binary_url)+
279 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
281 strcpy(url, binary_url);
282 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
284 obj = get_json_object(url);
287 result = json_object_to_daily_download_totals(obj);
288 json_object_put(obj);