From da89ec98fb34757f0c46dc8cb2dd87ae78d317ce Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 10 Oct 2020 10:43:50 +0200 Subject: Refactor: move download->json parsing to a one function --- src/plugins/Plugin.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/plugins/Plugin.cpp') 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 #include #include +#include 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 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_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 -- cgit v1.2.3