save bpph list into file cache
[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 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include "lp_json.h"
29 #include "lp_ws.h"
30
31 static struct bpph *json_to_bpph(json_object *o)
32 {
33         const char *binary_package_name;
34         const char *binary_package_version;
35         const char *distro_arch_series_link;
36         const char *self_link;
37         int architecture_specific;
38         const char *date_created;
39         struct bpph *bpph;
40
41         binary_package_name = json_object_get_string
42                 (json_object_object_get(o, "binary_package_name"));
43
44         binary_package_version = json_object_get_string
45                 (json_object_object_get(o, "binary_package_version"));
46
47         distro_arch_series_link = json_object_get_string
48                 (json_object_object_get(o, "distro_arch_series_link"));
49
50         self_link = json_object_get_string
51                 (json_object_object_get(o, "self_link"));
52
53         if (json_object_get_boolean
54             (json_object_object_get(o, "architecture_specific")))
55                 architecture_specific = 1;
56         else
57                 architecture_specific = 0;
58
59         bpph =  bpph_new(binary_package_name,
60                          binary_package_version,
61                          distro_arch_series_link,
62                          self_link,
63                          architecture_specific);
64
65         date_created = json_object_get_string
66                 (json_object_object_get(o, "date_created"));
67         if (date_created) {
68                 bpph->date_created.tm_isdst = -1;
69                 strptime(date_created, "%FT%T%z", &bpph->date_created);
70         }
71
72         return bpph;
73 }
74
75 static json_object *bpph_to_json(struct bpph *bpph)
76 {
77         json_object *json;
78         char *date;
79
80         json = json_object_new_object();
81
82         json_object_object_add
83                 (json,
84                  "binary_package_name",
85                  json_object_new_string(bpph->binary_package_name));
86
87         json_object_object_add
88                 (json,
89                  "binary_package_version",
90                  json_object_new_string(bpph->binary_package_version));
91
92         json_object_object_add
93                 (json,
94                  "distro_arch_series_link",
95                  json_object_new_string(bpph->distro_arch_series_link));
96
97         json_object_object_add
98                 (json,
99                  "architecture_specific",
100                  json_object_new_boolean(bpph->architecture_specific));
101
102         date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1);
103         strftime(date,
104                  strlen("YY-MM-DDThh:mm:ss+xxx") + 1,
105                  "%FT%T%z",
106                  &bpph->date_created);
107
108         json_object_object_add
109                 (json,
110                  "date_created",
111                  json_object_new_string(date));
112
113         free(date);
114
115         return json;
116 }
117
118 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
119 {
120         const char *display_name;
121         const char *title;
122         const char *architecture_tag;
123         boolean is_nominated_arch_indep;
124         const char *distroseries_link;
125
126         display_name = json_object_get_string
127                 (json_object_object_get(o, "display_name"));
128
129         title = json_object_get_string
130                 (json_object_object_get(o, "title"));
131
132         architecture_tag = json_object_get_string
133                 (json_object_object_get(o, "architecture_tag"));
134
135         distroseries_link = json_object_get_string
136                 (json_object_object_get(o, "distroseries_link"));
137
138         is_nominated_arch_indep = json_object_get_boolean
139                 (json_object_object_get(o, "is_nominated_arch_indep"));
140
141         return distro_arch_series_new(display_name,
142                                       title,
143                                       architecture_tag,
144                                       is_nominated_arch_indep,
145                                       distroseries_link);
146 }
147
148 struct distro_series *json_object_to_distro_series(json_object *o)
149 {
150         const char *displayname;
151         const char *title;
152         const char *name;
153         const char *version;
154
155         displayname = json_object_get_string
156                 (json_object_object_get(o, "displayname"));
157
158         title = json_object_get_string
159                 (json_object_object_get(o, "title"));
160
161         version = json_object_get_string
162                 (json_object_object_get(o, "version"));
163
164         name = json_object_get_string
165                 (json_object_object_get(o, "name"));
166
167         return distro_series_new(name,
168                                  version,
169                                  title,
170                                  displayname);
171 }
172
173 struct bpph **json_object_to_bpph_list(json_object *o)
174 {
175         json_object *o_entries;
176         int i, n, i2;
177         struct bpph **entries, *h;
178         const struct distro_arch_series *distro;
179
180         o_entries = json_object_object_get(o, "entries");
181
182         if (!o_entries)
183                 return NULL;
184
185         n = json_object_array_length(o_entries);
186
187         entries = malloc
188                 (sizeof(struct bpph *)*(n+1));
189
190         for (i = 0, i2 = 0; i < n; i++) {
191                 h = json_to_bpph(json_object_array_get_idx(o_entries,
192                                                            i));
193
194                 if (!h->architecture_specific) {
195                         distro = get_distro_arch_series
196                                 (h->distro_arch_series_link);
197
198                         if (!distro || !distro->is_nominated_arch_indep) {
199                                 bpph_free(h);
200                                 continue ;
201                         }
202                 }
203
204                 entries[i2] = h;
205                 i2++;
206         }
207         entries[i2] = NULL;
208
209         return entries;
210 }
211
212 json_object *bpph_list_to_json(struct bpph **list)
213 {
214         json_object *result, *entries;
215         struct bpph **cur;
216
217         result = json_object_new_object();
218
219         entries = json_object_new_array();
220         json_object_object_add(result, "entries", entries);
221
222         for (cur = list; *cur; cur++)
223                 json_object_array_add(entries, bpph_to_json(*cur));
224
225         return result;
226 }
227
228 struct daily_download_total *
229 json_object_to_daily_download_total(const char *d, json_object *o_c)
230 {
231         struct daily_download_total *result;
232
233         result = malloc(sizeof(struct daily_download_total));
234         result->count = json_object_get_int(o_c);
235
236         strptime(d, "%FT%T%z", &result->date);
237
238         return result;
239 }
240
241 static int json_object_get_fields_count(json_object *o)
242 {
243         int n = 0;
244         struct lh_entry *entry;
245
246         entry = json_object_get_object(o)->head;
247         while (entry) {
248                 entry = entry->next;
249                 n++;
250         }
251
252         return n;
253 }
254
255 struct daily_download_total * *
256 json_object_to_daily_download_totals(json_object *o)
257 {
258         int n, i;
259         struct daily_download_total **result;
260
261         n = json_object_get_fields_count(o);
262
263         result = malloc
264                 (sizeof(struct daily_download_total *)*(n+1));
265
266         i = 0;
267         json_object_object_foreach(o, key, val) {
268                 result[i] = json_object_to_daily_download_total(key, val);
269                 i++;
270         }
271
272         result[n] = NULL;
273
274         return result;
275 }