added is_nominated_arch_dep info
[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
62
63         display_name = json_object_get_string
64                 (json_object_object_get(o, "display_name"));
65
66         title = json_object_get_string
67                 (json_object_object_get(o, "title"));
68
69         architecture_tag = json_object_get_string
70                 (json_object_object_get(o, "architecture_tag"));
71
72         is_nominated_arch_indep = json_object_get_boolean
73                 (json_object_object_get(o, "is_nominated_arch_indep"));
74
75         return distro_arch_series_new(display_name,
76                                       title,
77                                       architecture_tag,
78                                       is_nominated_arch_indep);
79 }
80
81 struct binary_package_publishing_history * *
82 json_object_to_binary_package_publishing_history_list(json_object *o)
83 {
84         json_object *o_entries;
85         int i, n;
86         struct binary_package_publishing_history **entries;
87
88         o_entries = json_object_object_get(o, "entries");
89
90         if (!o_entries)
91                 return NULL;
92
93         n = json_object_array_length(o_entries);
94
95         entries = malloc
96                 (sizeof(struct binary_package_publishing_history *)*(n+1));
97
98         for (i = 0; i < n; i++) {
99                 entries[i] = json_object_to_binary_package_publishing_history
100                         (json_object_array_get_idx(o_entries, i));
101         }
102         entries[n] = NULL;
103
104         return entries;
105 }
106
107 struct daily_download_total *
108 json_object_to_daily_download_total(const char *d, json_object *o_c)
109 {
110         struct daily_download_total *result;
111
112         result = malloc(sizeof(struct daily_download_total));
113         result->count = json_object_get_int(o_c);
114
115         strptime(d, "%FT%T%z", &result->date);
116
117         return result;
118 }
119
120 static int json_object_get_fields_count(json_object *o)
121 {
122         int n = 0;
123
124         json_object_object_foreach(o, key, val)
125                 n++;
126
127         return n;
128 }
129
130 struct daily_download_total * *
131 json_object_to_daily_download_totals(json_object *o)
132 {
133         int n, i;
134         struct daily_download_total **result;
135
136         n = json_object_get_fields_count(o);
137
138         result = malloc
139                 (sizeof(struct daily_download_total *)*(n+1));
140
141         i = 0;
142         json_object_object_foreach(o, key, val) {
143                 result[i] = json_object_to_daily_download_total(key, val);
144                 i++;
145         }
146
147         result[n] = NULL;
148
149         return result;
150 }