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,
148 const char *pkg_status,
151 char *url, *key, *tmp;
152 struct bpph **result;
153 struct json_object *o, *bpph_json, *o_next;
157 url = create_query_get_bpph(archive_url, pkg_status, ws_size);
159 key = get_bpph_list_cache_key(archive_url);
161 result = get_bpph_list_from_cache(key);
164 date = get_last_creation_date(result);
167 printf("Update package since: %s\n", date);
169 tmp = malloc(strlen(url)
170 + strlen("&created_since_date=")
173 strcat(tmp, "&created_since_date=");
185 o = get_json_object(url);
194 result = bpph_list_append_list(result,
195 json_object_to_bpph_list(o));
197 o_next = json_object_object_get(o, "next_collection_link");
200 url = strdup(json_object_get_string(o_next));
206 bpph_json = bpph_list_to_json(result);
207 fcache_put(key, json_object_to_json_string(bpph_json));
208 json_object_put(bpph_json);
216 int get_download_count(const char *archive_url)
218 int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
219 char *url = malloc(n);
223 strcpy(url, archive_url);
224 strcat(url, QUERY_GET_DOWNLOAD_COUNT);
226 obj = get_json_object(url);
232 result = json_object_get_int(obj);
234 json_object_put(obj);
239 const struct distro_arch_series *get_distro_arch_series(const char *url)
242 const struct distro_arch_series *distro;
245 distro = cache_get(url);
247 return (struct distro_arch_series *)distro;
249 content = get_url_content(url, 1);
254 obj = json_tokener_parse(content);
261 distro = json_object_to_distro_arch_series(obj);
263 json_object_put(obj);
265 cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
270 const struct distro_series *get_distro_series(const char *url)
273 const struct distro_series *distro;
276 distro = cache_get(url);
278 return (struct distro_series *)distro;
280 content = get_url_content(url, 1);
285 obj = json_tokener_parse(content);
292 distro = json_object_to_distro_series(obj);
294 json_object_put(obj);
296 cache_put(url, distro, (void (*)(void *))&distro_series_free);
301 struct daily_download_total **get_daily_download_totals(const char *binary_url)
305 struct daily_download_total **result = NULL;
307 url = malloc(strlen(binary_url)+
308 strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
310 strcpy(url, binary_url);
311 strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
313 obj = get_json_object(url);
316 result = json_object_to_daily_download_totals(obj);
317 json_object_put(obj);