+ if (json) {
+ ddts = json_object_to_daily_download_totals(json);
+ json_object_put(json);
+ } else {
+ crt = time(NULL);
+ ddts = NULL;
+
+ while (date_since < crt) {
+ url = create_ddts_query(binary_url,
+ date_since,
+ date_since);
+ json = get_json_object(url);
+ free(url);
+
+ if (!json)
+ break;
+
+ tmp = json_object_to_daily_download_totals(json);
+ json_object_put(json);
+ ddts = ddts_merge(ddts, tmp);
+ free(tmp);
+
+ date_since = date_since + 24 * 60 * 60; /* +1 day */
+
+ url = create_ddts_query(binary_url, date_since, 0);
+ json = get_json_object(url);
+ free(url);
+
+ if (json) {
+ tmp = json_object_to_daily_download_totals
+ (json);
+ json_object_put(json);
+ ddts = ddts_merge(ddts, tmp);
+ free(tmp);
+ break;
+ }
+ }
+ }
+
+ return ddts;
+}
+
+struct daily_download_total **get_daily_download_totals(const char *binary_url,
+ time_t date_created)
+{
+ char *key, *content;
+ json_object *j_ddts, *json;
+ struct daily_download_total **retrieved_ddts = NULL;
+ struct daily_download_total **cached_ddts;
+ struct daily_download_total **ddts;
+ time_t last_t;
+
+ key = get_ddts_list_cache_key(binary_url);
+
+ content = fcache_get(key);
+ if (content) {
+ json = json_tokener_parse(content);
+ free(content);
+ } else {
+ json = NULL;
+ }
+
+ if (json) {
+ cached_ddts = json_object_to_daily_download_totals(json);
+ json_object_put(json);
+ last_t = ddts_get_last_date(cached_ddts);
+ } else {
+ last_t = 0;
+ cached_ddts = NULL;
+ }
+
+ if (last_t > 0)
+ retrieved_ddts = retrieve_ddts(binary_url, last_t);
+ else
+ retrieved_ddts = retrieve_ddts(binary_url, date_created);
+
+ ddts = ddts_merge(cached_ddts, retrieved_ddts);
+
+ if (ddts) {
+ j_ddts = ddts_to_json_for_cache(ddts);
+ fcache_put(key, json_object_get_string(j_ddts));
+ json_object_put(j_ddts);
+ }
+ free(key);
+
+ if (ddts != cached_ddts)
+ daily_download_total_list_free(cached_ddts);
+ daily_download_total_list_free(retrieved_ddts);
+
+ return ddts;