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