renaming
[ppastats.git] / src / lp_json.c
1 /*
2  * Copyright (C) 2011-2012 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #define _XOPEN_SOURCE
21 #include <time.h>
22
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include "lp_json.h"
28 #include "lp_ws.h"
29
30 struct bpph *json_object_to_bpph(json_object *o)
31 {
32         const char *binary_package_name;
33         const char *binary_package_version;
34         const char *distro_arch_series_link;
35         const char *self_link;
36         int architecture_specific;
37
38         binary_package_name = json_object_get_string
39                 (json_object_object_get(o, "binary_package_name"));
40
41         binary_package_version = json_object_get_string
42                 (json_object_object_get(o, "binary_package_version"));
43
44         distro_arch_series_link = json_object_get_string
45                 (json_object_object_get(o, "distro_arch_series_link"));
46
47         self_link = json_object_get_string
48                 (json_object_object_get(o, "self_link"));
49
50         if (json_object_get_boolean
51             (json_object_object_get(o, "architecture_specific")))
52                 architecture_specific = 1;
53         else
54                 architecture_specific = 0;
55
56         return bpph_new(binary_package_name,
57                         binary_package_version,
58                         distro_arch_series_link,
59                         self_link,
60                         architecture_specific);
61 }
62
63 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
64 {
65         const char *display_name;
66         const char *title;
67         const char *architecture_tag;
68         boolean is_nominated_arch_indep;
69         const char *distroseries_link;
70
71
72         display_name = json_object_get_string
73                 (json_object_object_get(o, "display_name"));
74
75         title = json_object_get_string
76                 (json_object_object_get(o, "title"));
77
78         architecture_tag = json_object_get_string
79                 (json_object_object_get(o, "architecture_tag"));
80
81         distroseries_link = json_object_get_string
82                 (json_object_object_get(o, "distroseries_link"));
83
84         is_nominated_arch_indep = json_object_get_boolean
85                 (json_object_object_get(o, "is_nominated_arch_indep"));
86
87         return distro_arch_series_new(display_name,
88                                       title,
89                                       architecture_tag,
90                                       is_nominated_arch_indep,
91                                       distroseries_link);
92 }
93
94 struct distro_series *json_object_to_distro_series(json_object *o)
95 {
96         const char *displayname;
97         const char *title;
98         const char *name;
99         const char *version;
100
101         displayname = json_object_get_string
102                 (json_object_object_get(o, "displayname"));
103
104         title = json_object_get_string
105                 (json_object_object_get(o, "title"));
106
107         version = json_object_get_string
108                 (json_object_object_get(o, "version"));
109
110         name = json_object_get_string
111                 (json_object_object_get(o, "name"));
112
113         return distro_series_new(name,
114                                  version,
115                                  title,
116                                  displayname);
117 }
118
119 struct bpph **json_object_to_bpph_list(json_object *o)
120 {
121         json_object *o_entries;
122         int i, n, i2;
123         struct bpph **entries, *h;
124         const struct distro_arch_series *distro;
125
126         o_entries = json_object_object_get(o, "entries");
127
128         if (!o_entries)
129                 return NULL;
130
131         n = json_object_array_length(o_entries);
132
133         entries = malloc
134                 (sizeof(struct bpph *)*(n+1));
135
136         for (i = 0, i2 = 0; i < n; i++) {
137                 h = json_object_to_bpph(json_object_array_get_idx(o_entries,
138                                                                   i));
139
140                 if (!h->architecture_specific) {
141                         distro = get_distro_arch_series
142                                 (h->distro_arch_series_link);
143
144                         if (!distro || !distro->is_nominated_arch_indep) {
145                                 bpph_free(h);
146                                 continue ;
147                         }
148                 }
149
150                 entries[i2] = h;
151                 i2++;
152         }
153         entries[i2] = NULL;
154
155         return entries;
156 }
157
158 struct daily_download_total *
159 json_object_to_daily_download_total(const char *d, json_object *o_c)
160 {
161         struct daily_download_total *result;
162
163         result = malloc(sizeof(struct daily_download_total));
164         result->count = json_object_get_int(o_c);
165
166         strptime(d, "%FT%T%z", &result->date);
167
168         return result;
169 }
170
171 static int json_object_get_fields_count(json_object *o)
172 {
173         int n = 0;
174         struct lh_entry *entry;
175
176         entry = json_object_get_object(o)->head;
177         while (entry) {
178                 entry = entry->next;
179                 n++;
180         }
181
182         return n;
183 }
184
185 struct daily_download_total * *
186 json_object_to_daily_download_totals(json_object *o)
187 {
188         int n, i;
189         struct daily_download_total **result;
190
191         n = json_object_get_fields_count(o);
192
193         result = malloc
194                 (sizeof(struct daily_download_total *)*(n+1));
195
196         i = 0;
197         json_object_object_foreach(o, key, val) {
198                 result[i] = json_object_to_daily_download_total(key, val);
199                 i++;
200         }
201
202         result[n] = NULL;
203
204         return result;
205 }