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