retry on network failure during url fetch
[ppastats.git] / src / lp_json.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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 binary_package_publishing_history *
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 binary_package_publishing_history * *
121 json_object_to_binary_package_publishing_history_list(json_object *o)
122 {
123         json_object *o_entries;
124         int i, n, i2;
125         struct binary_package_publishing_history **entries;
126         struct binary_package_publishing_history *h;
127         const struct distro_arch_series *distro;
128
129         o_entries = json_object_object_get(o, "entries");
130
131         if (!o_entries)
132                 return NULL;
133
134         n = json_object_array_length(o_entries);
135
136         entries = malloc
137                 (sizeof(struct binary_package_publishing_history *)*(n+1));
138
139         for (i = 0, i2 = 0; i < n; i++) {
140                 h = json_object_to_binary_package_publishing_history
141                         (json_object_array_get_idx(o_entries, i));
142
143                 if (!h->architecture_specific) {
144                         distro = get_distro_arch_series
145                                 (h->distro_arch_series_link);
146
147                         if (!distro->is_nominated_arch_indep) {
148                                 binary_package_publishing_history_free(h);
149                                 continue ;
150                         }
151                 }
152
153                 entries[i2] = h;
154                 i2++;
155         }
156         entries[i2] = NULL;
157
158         return entries;
159 }
160
161 struct daily_download_total *
162 json_object_to_daily_download_total(const char *d, json_object *o_c)
163 {
164         struct daily_download_total *result;
165
166         result = malloc(sizeof(struct daily_download_total));
167         result->count = json_object_get_int(o_c);
168
169         strptime(d, "%FT%T%z", &result->date);
170
171         return result;
172 }
173
174 static int json_object_get_fields_count(json_object *o)
175 {
176         int n = 0;
177         struct lh_entry *entry;
178
179         entry = json_object_get_object(o)->head;
180         while (entry) {
181                 entry = entry->next;
182                 n++;
183         }
184
185         return n;
186 }
187
188 struct daily_download_total * *
189 json_object_to_daily_download_totals(json_object *o)
190 {
191         int n, i;
192         struct daily_download_total **result;
193
194         n = json_object_get_fields_count(o);
195
196         result = malloc
197                 (sizeof(struct daily_download_total *)*(n+1));
198
199         i = 0;
200         json_object_object_foreach(o, key, val) {
201                 result[i] = json_object_to_daily_download_total(key, val);
202                 i++;
203         }
204
205         result[n] = NULL;
206
207         return result;
208 }