X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=8b025f78b0dd78b0d79f03eb106e7858c6ff502e;hb=55ffa21bfedade5cc1702622855277b860f0a7f3;hp=d6a21795b18752124205d93ba16dba24b41200a6;hpb=5ed73c995d6fcb5f3b4626bf7ebf609fc52f5d94;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index d6a2179..8b025f7 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -1,38 +1,79 @@ /* - Copyright (C) 2011 jeanfi@gmail.com + * Copyright (C) 2011-2014 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#define _XOPEN_SOURCE_EXTENDED +#define _XOPEN_SOURCE +#include +#include +#include +#include - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. +#include "lp_json.h" +#include "lp_ws.h" +#include - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. +static time_t json_to_time(json_object *json) +{ + const char *str; + struct tm tm; + char *ret; - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA -*/ + str = json_object_get_string(json); + if (!str) + return -1; -#define _XOPEN_SOURCE -#include + memset(&tm, 0, sizeof(struct tm)); + tm.tm_isdst = -1; + ret = strptime(str, "%FT%T", &tm); -#include -#include + if (ret) + return mktime(&tm); + else + return -1; +} -#include "lp_json.h" +static json_object *time_to_json(time_t t) +{ + char *str; + json_object *j; + + str = time_to_ISO8601_time(&t); + + if (str) { + j = json_object_new_string(str); + free(str); + return j; + } else { + return NULL; + } +} -struct binary_package_publishing_history * -json_object_to_binary_package_publishing_history(json_object *o) +static struct bpph *json_to_bpph(json_object *o) { const char *binary_package_name; const char *binary_package_version; const char *distro_arch_series_link; const char *self_link; + int arch_specific; + struct bpph *bpph; + const char *status; + time_t date_created; binary_package_name = json_object_get_string (json_object_object_get(o, "binary_package_name")); @@ -46,10 +87,61 @@ json_object_to_binary_package_publishing_history(json_object *o) self_link = json_object_get_string (json_object_object_get(o, "self_link")); - return binary_package_publishing_history_new(binary_package_name, - binary_package_version, - distro_arch_series_link, - self_link); + arch_specific = json_object_get_boolean + (json_object_object_get(o, "architecture_specific")); + + date_created = json_to_time(json_object_object_get(o, "date_created")); + + status = json_object_get_string(json_object_object_get(o, "status")); + + bpph = bpph_new(binary_package_name, + binary_package_version, + distro_arch_series_link, + self_link, + status, + arch_specific, + date_created); + + return bpph; +} + +static json_object *bpph_to_json(struct bpph *bpph) +{ + json_object *json, *time; + + json = json_object_new_object(); + + json_object_object_add + (json, + "binary_package_name", + json_object_new_string(bpph->binary_package_name)); + + json_object_object_add + (json, + "binary_package_version", + json_object_new_string(bpph->binary_package_version)); + + json_object_object_add + (json, + "distro_arch_series_link", + json_object_new_string(bpph->distro_arch_series_link)); + + json_object_object_add + (json, "self_link", json_object_new_string(bpph->self_link)); + + json_object_object_add + (json, + "architecture_specific", + json_object_new_boolean(bpph->architecture_specific)); + + json_object_object_add + (json, "status", json_object_new_string(bpph->status)); + + time = time_to_json(bpph->date_created); + json_object_object_add + (json, "date_created", time); + + return json; } struct distro_arch_series *json_object_to_distro_arch_series(json_object *o) @@ -57,6 +149,8 @@ struct distro_arch_series *json_object_to_distro_arch_series(json_object *o) const char *display_name; const char *title; const char *architecture_tag; + json_bool is_nominated_arch_indep; + const char *distroseries_link; display_name = json_object_get_string (json_object_object_get(o, "display_name")); @@ -67,17 +161,47 @@ struct distro_arch_series *json_object_to_distro_arch_series(json_object *o) architecture_tag = json_object_get_string (json_object_object_get(o, "architecture_tag")); + distroseries_link = json_object_get_string + (json_object_object_get(o, "distroseries_link")); + + is_nominated_arch_indep = json_object_get_boolean + (json_object_object_get(o, "is_nominated_arch_indep")); + return distro_arch_series_new(display_name, title, - architecture_tag); + architecture_tag, + is_nominated_arch_indep, + distroseries_link); } -struct binary_package_publishing_history * * -json_object_to_binary_package_publishing_history_list(json_object *o) +struct distro_series *json_object_to_distro_series(json_object *o) +{ + const char *displayname; + const char *title; + const char *name; + const char *version; + + displayname = json_object_get_string + (json_object_object_get(o, "displayname")); + + title = json_object_get_string(json_object_object_get(o, "title")); + + version = json_object_get_string(json_object_object_get(o, "version")); + + name = json_object_get_string(json_object_object_get(o, "name")); + + return distro_series_new(name, + version, + title, + displayname); +} + +struct bpph **json_object_to_bpph_list(json_object *o) { json_object *o_entries; - int i, n; - struct binary_package_publishing_history **entries; + int i, n, i2; + struct bpph **entries, *h; + const struct distro_arch_series *distro; o_entries = json_object_object_get(o, "entries"); @@ -87,17 +211,47 @@ json_object_to_binary_package_publishing_history_list(json_object *o) n = json_object_array_length(o_entries); entries = malloc - (sizeof(struct binary_package_publishing_history *)*(n+1)); + (sizeof(struct bpph *)*(n+1)); - for (i = 0; i < n; i++) { - entries[i] = json_object_to_binary_package_publishing_history - (json_object_array_get_idx(o_entries, i)); + for (i = 0, i2 = 0; i < n; i++) { + h = json_to_bpph(json_object_array_get_idx(o_entries, + i)); + + if (!h->architecture_specific) { + distro = get_distro_arch_series + (h->distro_arch_series_link); + + if (!distro || !distro->is_nominated_arch_indep) { + bpph_free(h); + continue ; + } + } + + entries[i2] = h; + i2++; } - entries[n] = NULL; + entries[i2] = NULL; return entries; } +json_object *bpph_list_to_json(struct bpph **list) +{ + json_object *result, *entries; + struct bpph **cur; + + result = json_object_new_object(); + + entries = json_object_new_array(); + json_object_object_add(result, "entries", entries); + + if (list) + for (cur = list; *cur; cur++) + json_object_array_add(entries, bpph_to_json(*cur)); + + return result; +} + struct daily_download_total * json_object_to_daily_download_total(const char *d, json_object *o_c) { @@ -106,6 +260,7 @@ json_object_to_daily_download_total(const char *d, json_object *o_c) result = malloc(sizeof(struct daily_download_total)); result->count = json_object_get_int(o_c); + memset(&result->date, 0, sizeof(struct tm)); strptime(d, "%FT%T%z", &result->date); return result; @@ -114,9 +269,13 @@ json_object_to_daily_download_total(const char *d, json_object *o_c) static int json_object_get_fields_count(json_object *o) { int n = 0; + struct lh_entry *entry; - json_object_object_foreach(o, key, val) + entry = json_object_get_object(o)->head; + while (entry) { + entry = entry->next; n++; + } return n; } @@ -142,3 +301,41 @@ json_object_to_daily_download_totals(json_object *o) return result; } + +struct json_object *date_to_json(struct tm *tm) +{ + json_object *json; + + json = json_object_new_array(); + json_object_array_add(json, json_object_new_int(tm->tm_year+1900)); + json_object_array_add(json, json_object_new_int(tm->tm_mon+1)); + json_object_array_add(json, json_object_new_int(tm->tm_mday)); + + return json; +} + +json_object *ddts_to_json(struct daily_download_total **ddts) +{ + json_object *json_ddt, *json_ddts; + struct daily_download_total *ddt; + + json_ddts = json_object_new_array(); + + while (ddts && *ddts) { + ddt = *ddts; + + json_ddt = json_object_new_object(); + json_object_object_add(json_ddt, + "value", + json_object_new_int(ddt->count)); + json_object_object_add(json_ddt, + "time", + date_to_json(&ddt->date)); + + json_object_array_add(json_ddts, json_ddt); + + ddts++; + } + + return json_ddts; +}