diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-10-10 10:43:50 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-10-10 10:55:36 +0200 |
commit | da89ec98fb34757f0c46dc8cb2dd87ae78d317ce (patch) | |
tree | 653bd81b2125968c706f5552aaeb7d1b7fdc6d04 /src/plugins/Plugin.cpp | |
parent | 85a1edf8d9c21b830f7ec4a2d42b8e5c1d044845 (diff) |
Refactor: move download->json parsing to a one function
Diffstat (limited to 'src/plugins/Plugin.cpp')
-rw-r--r-- | src/plugins/Plugin.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/plugins/Plugin.cpp b/src/plugins/Plugin.cpp index c5726f1..ac60187 100644 --- a/src/plugins/Plugin.cpp +++ b/src/plugins/Plugin.cpp @@ -2,6 +2,7 @@ #include <sstream> #include <iomanip> #include <array> +#include <json/reader.h> namespace QuickMedia { SearchResult Plugin::search(const std::string &text, BodyItems &result_items) { @@ -84,4 +85,25 @@ namespace QuickMedia { return result.str(); } + + DownloadResult Plugin::download_json(Json::Value &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const { + std::string server_response; + if(download_to_string(url, server_response, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) { + if(err_msg) + *err_msg = server_response; + return DownloadResult::NET_ERR; + } + + Json::CharReaderBuilder json_builder; + std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); + std::string json_errors; + if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &result, &json_errors)) { + fprintf(stderr, "download_json error: %s\n", json_errors.c_str()); + if(err_msg) + *err_msg = std::move(json_errors); + return DownloadResult::ERR; + } + + return DownloadResult::OK; + } }
\ No newline at end of file |