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>
38 /** Default ws.size value for the getPublishedBinaries request. */
39 static const int DEFAULT_WS_SIZE = 150;
41 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
43 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
45 static json_object *get_json_object(const char *url)
47 json_object *obj = NULL;
50 body = get_url_content(url, 0);
53 obj = json_tokener_parse(body);
63 static char *get_bpph_list_cache_key(const char *archive_url)
67 key = malloc(strlen(archive_url + 7) + strlen("/bpph") + 1);
68 sprintf(key, "%s/bpph", archive_url + 7);
73 static struct bpph **get_bpph_list_from_cache(const char *key)
79 content = fcache_get(key);
83 json = json_tokener_parse(content);
87 list = json_object_to_bpph_list(json);
89 json_object_put(json);
95 static char *get_last_creation_date(struct bpph **list)
103 for (cur = list; *cur; cur++) {
104 t = (*cur)->date_created;
110 return time_to_str(last);
116 * 'archive_url': LP URL of the archive.
117 * 'size': size of the reply array. Between 1-300, else default value is used.
119 static char *create_query_get_bpph(const char *archive_url,
123 static const char *default_opt = "?ws.op=getPublishedBinaries&ws.size=";
124 static const char *status_opt = "&status=";
128 if (size < 1 || size > 300)
129 size = DEFAULT_WS_SIZE;
131 n = strlen(archive_url) + strlen(default_opt) + 3 + 1;
134 n += strlen(status_opt) + strlen(status);
137 sprintf(url, "%s%s%d", archive_url, default_opt, size);
140 strcat(url, status_opt);
147 struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
149 char *url, *key, *tmp;
150 struct bpph **result;
151 struct json_object *o, *bpph_json, *o_next;
155 url = create_query_get_bpph(archive_url, pkg_status, -1);
157 key = get_bpph_list_cache_key(archive_url);
159 result = get_bpph_list_from_cache(key);
162 date = get_last_creation_date(result);
165 printf("Update package since: %s\n", date);
167 tmp = malloc(strlen(url)
168 + strlen("&created_since_date=")
171 strcat(tmp, "&created_since_date=");
183 o = get_json_object(url);
192 result = bpph_list_append_list(result,
193 json_object_to_bpph_list(o));
195 o_next = json_object_object_get(o, "next_collection_link");
198 url = strdup(json_object_get_string(o_next));
204 bpph_json = bpph_list_to_json(result);
205 fcache_put(key, json_object_to_json_string(bpph_json));
206 json_object_put(bpph_json);
214 int get_download_count(const char *archive_url)
216 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
217 char *url = malloc(n);
221 strcpy(url, archive_url);
222 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
224 obj = get_json_object(url);
230 result = json_object_get_int(obj);
232 json_object_put(obj);
237 const struct distro_arch_series *get_distro_arch_series(const char *url)
240 const struct distro_arch_series *distro;
243 distro = cache_get(url);
245 return (struct distro_arch_series *)distro;
247 content = get_url_content(url, 1);
252 obj = json_tokener_parse(content);
259 distro = json_object_to_distro_arch_series(obj);
261 json_object_put(obj);
263 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
268 const struct distro_series *get_distro_series(const char *url)
271 const struct distro_series *distro;
274 distro = cache_get(url);
276 return (struct distro_series *)distro;
278 content = get_url_content(url, 1);
283 obj = json_tokener_parse(content);
290 distro = json_object_to_distro_series(obj);
292 json_object_put(obj);
294 cache_put(url, distro, (void (*)(void *))&distro_series_free);
299 struct daily_download_total **get_daily_download_totals(const char *binary_url)
303 struct daily_download_total **result = NULL;
305 url = malloc(strlen(binary_url)+
306 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
308 strcpy(url, binary_url);
309 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
311 obj = get_json_object(url);
314 result = json_object_to_daily_download_totals(obj);
315 json_object_put(obj);