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 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
65 struct bpph **result = NULL;
66 struct json_object *o, *bpph_json, *o_next;
68 url = malloc(strlen(archive_url)+
69 strlen(QUERY_GET_PUBLISHED_BINARIES)+
70 (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
73 strcpy(url, archive_url);
74 strcat(url, QUERY_GET_PUBLISHED_BINARIES);
77 strcat(url, "&status=");
78 strcat(url, pkg_status);
82 o = get_json_object(url);
89 result = (struct bpph **)list_append_list
91 (void **)json_object_to_bpph_list(o));
93 o_next = json_object_object_get(o, "next_collection_link");
96 url = strdup(json_object_get_string(o_next));
101 bpph_json = bpph_list_to_json(result);
103 bpph_key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
104 sprintf(bpph_key, "%s/bpph", archive_url + 7);
105 fcache_put(bpph_key, json_object_to_json_string(bpph_json));
107 json_object_put(bpph_json);
113 int get_download_count(const char *archive_url)
115 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
116 char *url = malloc(n);
120 strcpy(url, archive_url);
121 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
123 obj = get_json_object(url);
129 result = json_object_get_int(obj);
131 json_object_put(obj);
136 const struct distro_arch_series *get_distro_arch_series(const char *url)
139 const struct distro_arch_series *distro;
142 distro = cache_get(url);
144 return (struct distro_arch_series *)distro;
146 content = get_url_content(url, 1);
151 obj = json_tokener_parse(content);
158 distro = json_object_to_distro_arch_series(obj);
160 json_object_put(obj);
162 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
167 const struct distro_series *get_distro_series(const char *url)
170 const struct distro_series *distro;
173 distro = cache_get(url);
175 return (struct distro_series *)distro;
177 content = get_url_content(url, 1);
182 obj = json_tokener_parse(content);
189 distro = json_object_to_distro_series(obj);
191 json_object_put(obj);
193 cache_put(url, distro, (void (*)(void *))&distro_series_free);
198 struct daily_download_total **get_daily_download_totals(const char *binary_url)
202 struct daily_download_total **result = NULL;
204 url = malloc(strlen(binary_url)+
205 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
207 strcpy(url, binary_url);
208 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
210 obj = get_json_object(url);
213 result = json_object_to_daily_download_totals(obj);
214 json_object_put(obj);