3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "convert_json.h"
29 #include "httpfetch.h"
32 Json::Value fetchJsonValue(const std::string &url,
33 std::vector<std::string> *extra_headers)
35 HTTPFetchRequest fetch_request;
36 HTTPFetchResult fetch_result;
37 fetch_request.url = url;
38 fetch_request.caller = HTTPFETCH_SYNC;
40 if (extra_headers != NULL)
41 fetch_request.extra_headers = *extra_headers;
43 httpfetch_sync(fetch_request, fetch_result);
45 if (!fetch_result.succeeded) {
50 std::istringstream stream(fetch_result.data);
52 if (!reader.parse(stream, root)) {
53 errorstream << "URL: " << url << std::endl;
54 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
55 errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
62 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
63 std::vector<ModStoreMod> retval;
65 if (modlist.isArray()) {
66 for (unsigned int i = 0; i < modlist.size(); i++)
72 if (modlist[i]["id"].asString().size()) {
73 std::string id_raw = modlist[i]["id"].asString();
75 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
77 if ((id_raw != "") && (*endptr == 0)) {
78 toadd.id = numbervalue;
81 errorstream << "readModStoreList: missing id" << std::endl;
86 errorstream << "readModStoreList: missing id" << std::endl;
91 if (modlist[i]["title"].asString().size()) {
92 toadd.title = modlist[i]["title"].asString();
95 errorstream << "readModStoreList: missing title" << std::endl;
100 if (modlist[i]["basename"].asString().size()) {
101 toadd.basename = modlist[i]["basename"].asString();
104 errorstream << "readModStoreList: missing basename" << std::endl;
115 retval.push_back(toadd);
122 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
124 ModStoreModDetails retval;
129 if (details["version_set"].isArray()) {
130 for (unsigned int i = 0; i < details["version_set"].size(); i++)
132 ModStoreVersionEntry toadd;
134 if (details["version_set"][i]["id"].asString().size()) {
135 std::string id_raw = details["version_set"][i]["id"].asString();
137 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
139 if ((id_raw != "") && (*endptr == 0)) {
140 toadd.id = numbervalue;
144 errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
145 retval.valid = false;
149 if (details["version_set"][i]["date"].asString().size()) {
150 toadd.date = details["version_set"][i]["date"].asString();
154 if (details["version_set"][i]["file"].asString().size()) {
155 toadd.file = details["version_set"][i]["file"].asString();
158 errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
159 retval.valid = false;
167 retval.versions.push_back(toadd);
175 if (retval.versions.size() < 1) {
176 infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
177 retval.valid = false;
181 if (details["categories"].isObject()) {
182 for (unsigned int i = 0; i < details["categories"].size(); i++) {
183 ModStoreCategoryInfo toadd;
185 if (details["categories"][i]["id"].asString().size()) {
187 std::string id_raw = details["categories"][i]["id"].asString();
189 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
191 if ((id_raw != "") && (*endptr == 0)) {
192 toadd.id = numbervalue;
196 errorstream << "readModStoreModDetails: missing categories id" << std::endl;
197 retval.valid = false;
199 if (details["categories"][i]["title"].asString().size()) {
200 toadd.name = details["categories"][i]["title"].asString();
203 errorstream << "readModStoreModDetails: missing categories title" << std::endl;
204 retval.valid = false;
208 retval.categories.push_back(toadd);
217 if (details["author"].isObject()) {
218 if (details["author"]["id"].asString().size()) {
220 std::string id_raw = details["author"]["id"].asString();
222 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
224 if ((id_raw != "") && (*endptr == 0)) {
225 retval.author.id = numbervalue;
228 errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
229 retval.valid = false;
233 errorstream << "readModStoreModDetails: missing author id" << std::endl;
234 retval.valid = false;
237 if (details["author"]["username"].asString().size()) {
238 retval.author.username = details["author"]["username"].asString();
241 errorstream << "readModStoreModDetails: missing author username" << std::endl;
242 retval.valid = false;
246 errorstream << "readModStoreModDetails: missing author" << std::endl;
247 retval.valid = false;
251 if (details["license"].isObject()) {
252 if (details["license"]["id"].asString().size()) {
254 std::string id_raw = details["license"]["id"].asString();
256 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
258 if ((id_raw != "") && (*endptr == 0)) {
259 retval.license.id = numbervalue;
263 errorstream << "readModStoreModDetails: missing license id" << std::endl;
264 retval.valid = false;
267 if (details["license"]["short"].asString().size()) {
268 retval.license.shortinfo = details["license"]["short"].asString();
271 errorstream << "readModStoreModDetails: missing license short" << std::endl;
272 retval.valid = false;
275 if (details["license"]["link"].asString().size()) {
276 retval.license.url = details["license"]["link"].asString();
282 if (details["titlepic"].isObject()) {
283 if (details["titlepic"]["id"].asString().size()) {
285 std::string id_raw = details["titlepic"]["id"].asString();
287 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
289 if ((id_raw != "") && (*endptr == 0)) {
290 retval.titlepic.id = numbervalue;
294 if (details["titlepic"]["file"].asString().size()) {
295 retval.titlepic.file = details["titlepic"]["file"].asString();
298 if (details["titlepic"]["desc"].asString().size()) {
299 retval.titlepic.description = details["titlepic"]["desc"].asString();
302 if (details["titlepic"]["mod"].asString().size()) {
304 std::string mod_raw = details["titlepic"]["mod"].asString();
306 int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
308 if ((mod_raw != "") && (*endptr == 0)) {
309 retval.titlepic.mod = numbervalue;
315 if (details["id"].asString().size()) {
317 std::string id_raw = details["id"].asString();
319 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
321 if ((id_raw != "") && (*endptr == 0)) {
322 retval.id = numbervalue;
326 errorstream << "readModStoreModDetails: missing id" << std::endl;
327 retval.valid = false;
331 if (details["title"].asString().size()) {
332 retval.title = details["title"].asString();
335 errorstream << "readModStoreModDetails: missing title" << std::endl;
336 retval.valid = false;
340 if (details["basename"].asString().size()) {
341 retval.basename = details["basename"].asString();
344 errorstream << "readModStoreModDetails: missing basename" << std::endl;
345 retval.valid = false;
349 if (details["desc"].asString().size()) {
350 retval.description = details["desc"].asString();
354 if (details["replink"].asString().size()) {
355 retval.repository = details["replink"].asString();
359 if (details["value"].isInt()) {
360 retval.rating = details["value"].asInt();
366 if (details["depends"].isArray()) {
371 if (details["softdep"].isArray()) {
376 if (details["screenshot_url"].asString().size()) {
377 retval.screenshot_url = details["screenshot_url"].asString();