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 | |
parent | 85a1edf8d9c21b830f7ec4a2d42b8e5c1d044845 (diff) |
Refactor: move download->json parsing to a one function
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/Fourchan.cpp | 38 | ||||
-rw-r--r-- | src/plugins/Mangadex.cpp | 30 | ||||
-rw-r--r-- | src/plugins/Manganelo.cpp | 17 | ||||
-rw-r--r-- | src/plugins/Mangatown.cpp | 1 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 195 | ||||
-rw-r--r-- | src/plugins/Plugin.cpp | 22 | ||||
-rw-r--r-- | src/plugins/Pornhub.cpp | 1 | ||||
-rw-r--r-- | src/plugins/Youtube.cpp | 52 |
8 files changed, 64 insertions, 292 deletions
diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index 757a0e1..1cecc2b 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -211,18 +211,9 @@ namespace QuickMedia { } PluginResult Fourchan::get_threads_internal(const std::string &url, BodyItems &result_items) { - std::string server_response; - if(download_to_string(fourchan_url + url + "/catalog.json", server_response, {}, use_tor) != DownloadResult::OK) - return PluginResult::NET_ERR; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(server_response.data(), server_response.data() + server_response.size(), &json_root, &json_errors)) { - fprintf(stderr, "4chan catalog json error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult result = download_json(json_root, fourchan_url + url + "/catalog.json", {}, true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); if(json_root.isArray()) { for(const Json::Value &page_data : json_root) { @@ -402,18 +393,10 @@ namespace QuickMedia { // TODO: Merge with get_threads_internal PluginResult Fourchan::get_thread_comments(const std::string &list_url, const std::string &url, BodyItems &result_items) { cached_media_urls.clear(); - std::string server_response; - if(download_to_string(fourchan_url + list_url + "/thread/" + url + ".json", server_response, {}, use_tor) != DownloadResult::OK) - return PluginResult::NET_ERR; Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(server_response.data(), server_response.data() + server_response.size(), &json_root, &json_errors)) { - fprintf(stderr, "4chan thread json error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult result = download_json(json_root, fourchan_url + list_url + "/thread/" + url + ".json", {}, true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); if(!json_root.isObject()) return PluginResult::ERR; @@ -618,18 +601,9 @@ namespace QuickMedia { CommandArg{"-c", cookies_filepath.data} }; - std::string response; - if(download_to_string("https://sys.4chan.org/auth", response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(&response[0], &response[response.size()], &json_root, &json_errors) || !json_root.isObject()) { - fprintf(stderr, "Youtube get front page error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult result = download_json(json_root, "https://sys.4chan.org/auth", std::move(additional_args), true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); if(!json_root.isObject()) return PluginResult::ERR; diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index bf493d3..9808654 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -36,21 +36,10 @@ namespace QuickMedia { SearchResult Mangadex::search(const std::string &url, BodyItems &result_items) { std::string manga_id = title_url_extract_manga_id(url); std::string request_url = "https://mangadex.org/api/?id=" + manga_id + "&type=manga"; - std::string server_response; - if(download_to_string(request_url, server_response, {}, use_tor, true) != DownloadResult::OK) - return SearchResult::NET_ERR; - if(server_response.empty()) - return SearchResult::OK; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Mangadex search json error: %s\n", json_errors.c_str()); - return SearchResult::ERR; - } + DownloadResult result = download_json(json_root, request_url, {}, true); + if(result != DownloadResult::OK) return download_result_to_search_result(result); if(!json_root.isObject()) return SearchResult::ERR; @@ -261,21 +250,10 @@ namespace QuickMedia { CommandArg cookie_arg = { "-b", std::move(cookie_filepath) }; std::string manga_id = chapter_url_extract_manga_id(url); std::string request_url = mangadex_url + "/api/?id=" + manga_id + "&server=null&type=chapter"; - std::string server_response; - if(download_to_string(request_url, server_response, {std::move(cookie_arg)}, use_tor, true) != DownloadResult::OK) - return ImageResult::NET_ERR; - - if(server_response.empty()) - return ImageResult::OK; Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Mangadex image urls json error: %s\n", json_errors.c_str()); - return ImageResult::ERR; - } + DownloadResult result = download_json(json_root, request_url, {std::move(cookie_arg)}, true); + if(result != DownloadResult::OK) return download_result_to_image_result(result); if(!json_root.isObject()) return ImageResult::ERR; diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp index 5be64ca..52b9ebd 100644 --- a/src/plugins/Manganelo.cpp +++ b/src/plugins/Manganelo.cpp @@ -1,7 +1,6 @@ #include "../../plugins/Manganelo.hpp" #include "../../include/Notification.hpp" #include <quickmedia/HtmlSearch.h> -#include <json/reader.h> namespace QuickMedia { struct BodyItemImageContext { @@ -77,21 +76,9 @@ namespace QuickMedia { search_term += url_param_encode(text); CommandArg data_arg = { "--data", std::move(search_term) }; - std::string server_response; - if(download_to_string(url, server_response, {data_arg}, use_tor) != DownloadResult::OK) - return SuggestionResult::NET_ERR; - - if(server_response.empty()) - return SuggestionResult::OK; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Manganelo suggestions json error: %s\n", json_errors.c_str()); - return SuggestionResult::ERR; - } + DownloadResult result = download_json(json_root, url, {data_arg}, true); + if(result != DownloadResult::OK) return download_result_to_suggestion_result(result); if(json_root.isArray()) { for(const Json::Value &child : json_root) { diff --git a/src/plugins/Mangatown.cpp b/src/plugins/Mangatown.cpp index 1f3bcae..400d1ef 100644 --- a/src/plugins/Mangatown.cpp +++ b/src/plugins/Mangatown.cpp @@ -1,7 +1,6 @@ #include "../../plugins/Mangatown.hpp" #include "../../include/Notification.hpp" #include <quickmedia/HtmlSearch.h> -#include <json/reader.h> static const std::string mangatown_url = "https://www.mangatown.com"; diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index fb7c00d..2107812 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -109,28 +109,15 @@ namespace QuickMedia { { "-m", "35" } }; - std::string server_response; - char url[512]; if(next_batch.empty()) snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?timeout=0", homeserver.c_str()); else snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?timeout=30000&since=%s", homeserver.c_str(), next_batch.c_str()); - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(server_response.empty()) - return PluginResult::ERR; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix sync response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); PluginResult result = sync_response_to_body_items(json_root, room_messages); if(result != PluginResult::OK) @@ -152,21 +139,9 @@ namespace QuickMedia { { "-H", "Authorization: Bearer " + access_token } }; - std::string server_response; - if(download_to_string(homeserver + "/_matrix/client/r0/joined_rooms", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(server_response.empty()) - return PluginResult::ERR; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix joined rooms response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, homeserver + "/_matrix/client/r0/joined_rooms", std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; @@ -819,23 +794,10 @@ namespace QuickMedia { char url[512]; snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/messages?from=%s&limit=20&dir=b&filter=%s", homeserver.c_str(), room_data->id.c_str(), from.c_str(), filter.c_str()); - fprintf(stderr, "load initial room data, url: |%s|\n", url); - - std::string server_response; - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - if(server_response.empty()) - return PluginResult::ERR; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix /rooms/<room_id>/messages/ response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; @@ -978,23 +940,10 @@ namespace QuickMedia { char request_url[512]; snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); - fprintf(stderr, "Post message to |%s|\n", request_url); - - std::string server_response; - if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(server_response.empty()) - return PluginResult::ERR; Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; @@ -1125,23 +1074,10 @@ namespace QuickMedia { char request_url[512]; snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); - fprintf(stderr, "Post message to |%s|\n", request_url); - - std::string server_response; - if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(server_response.empty()) - return PluginResult::ERR; Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; @@ -1230,23 +1166,10 @@ namespace QuickMedia { char request_url[512]; snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); - fprintf(stderr, "Post message to |%s|\n", request_url); - - std::string server_response; - if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(server_response.empty()) - return PluginResult::ERR; Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; @@ -1282,23 +1205,10 @@ namespace QuickMedia { char url[512]; snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/context/%s?limit=0&filter=%s", homeserver.c_str(), room_data->id.c_str(), message->event_id.c_str(), filter.c_str()); - fprintf(stderr, "get message context, url: |%s|\n", url); - std::string server_response; - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) - return nullptr; - - if(server_response.empty()) - return nullptr; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix /rooms/<room_id>/context/<event_id> response parse error: %s\n", json_errors.c_str()); - return nullptr; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return nullptr; if(!json_root.isObject()) return nullptr; @@ -1447,27 +1357,10 @@ namespace QuickMedia { char url[512]; snprintf(url, sizeof(url), "%s/_matrix/media/r0/upload?filename=%s", homeserver.c_str(), filename_escaped.c_str()); - fprintf(stderr, "Upload url: |%s|\n", url); - - std::string server_response; - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) { - err_msg = "Upload request failed, reason: " + server_response; - return PluginResult::NET_ERR; - } - - if(server_response.empty()) { - err_msg = "Got corrupt response from server"; - return PluginResult::ERR; - } Json::Value json_root; - 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()], &json_root, &json_errors)) { - err_msg = "Matrix upload response parse error: " + json_errors; - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) { err_msg = "Got corrupt response from server"; @@ -1512,23 +1405,9 @@ namespace QuickMedia { { "--data-binary", Json::writeString(builder, std::move(request_data)) } }; - std::string server_response; - if(download_to_string(homeserver + "/_matrix/client/r0/login", server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) { - err_msg = std::move(server_response); - return PluginResult::NET_ERR; - } - - if(server_response.empty()) - return PluginResult::ERR; - Json::Value json_root; - 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()], &json_root, &json_errors)) { - err_msg = "Matrix login response parse error: " + json_errors; - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, homeserver + "/_matrix/client/r0/login", std::move(additional_args), true, &err_msg); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) { err_msg = "Failed to parse matrix login response"; @@ -1626,25 +1505,10 @@ namespace QuickMedia { char url[512]; snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/redact/%s/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), message_typed->event_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); - fprintf(stderr, "load initial room data, url: |%s|\n", url); - - std::string server_response; - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) { - err_msg = std::move(server_response); - return PluginResult::NET_ERR; - } - - if(server_response.empty()) - return PluginResult::ERR; Json::Value json_root; - 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()], &json_root, &json_errors)) { - err_msg = "Matrix delete message response parse error: " + json_errors; - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) { err_msg = "Failed to parse matrix login response"; @@ -1819,25 +1683,10 @@ namespace QuickMedia { char url[512]; snprintf(url, sizeof(url), "%s/_matrix/media/r0/config", homeserver.c_str()); - fprintf(stderr, "load initial room data, url: |%s|\n", url); - - std::string server_response; - if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) { - fprintf(stderr, "Matrix /config failed\n"); - return PluginResult::NET_ERR; - } - - if(server_response.empty()) - return PluginResult::ERR; Json::Value json_root; - 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()], &json_root, &json_errors)) { - fprintf(stderr, "Matrix parsing /config response failed, error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); if(!json_root.isObject()) return PluginResult::ERR; 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 diff --git a/src/plugins/Pornhub.cpp b/src/plugins/Pornhub.cpp index b093c64..77d5594 100644 --- a/src/plugins/Pornhub.cpp +++ b/src/plugins/Pornhub.cpp @@ -1,6 +1,5 @@ #include "../../plugins/Pornhub.hpp" #include <quickmedia/HtmlSearch.h> -#include <json/reader.h> #include <string.h> namespace QuickMedia { diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 364f394..7e1fc63 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -106,18 +106,9 @@ namespace QuickMedia { //std::vector<CommandArg> cookies = get_cookies(); //additional_args.insert(additional_args.end(), cookies.begin(), cookies.end()); - std::string website_data; - if(download_to_string(url + "?pbj=1", website_data, additional_args, use_tor, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(&website_data[0], &website_data[website_data.size()], &json_root, &json_errors)) { - fprintf(stderr, "Youtube get front page error: %s\n", json_errors.c_str()); - return PluginResult::ERR; - } + DownloadResult result = download_json(json_root, url + "?pbj=1", std::move(additional_args), true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); if(!json_root.isArray()) return PluginResult::ERR; @@ -300,18 +291,9 @@ namespace QuickMedia { //std::vector<CommandArg> cookies = get_cookies(); //additional_args.insert(additional_args.end(), cookies.begin(), cookies.end()); - std::string website_data; - if(download_to_string(url + "&pbj=1", website_data, additional_args, use_tor, true) != DownloadResult::OK) - return SuggestionResult::NET_ERR; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(&website_data[0], &website_data[website_data.size()], &json_root, &json_errors)) { - fprintf(stderr, "Youtube search json error: %s\n", json_errors.c_str()); - return SuggestionResult::ERR; - } + DownloadResult result = download_json(json_root, url + "?pbj=1", std::move(additional_args), true); + if(result != DownloadResult::OK) return download_result_to_suggestion_result(result); if(!json_root.isArray()) return SuggestionResult::ERR; @@ -380,18 +362,9 @@ namespace QuickMedia { //std::vector<CommandArg> cookies = get_cookies(); //additional_args.insert(additional_args.end(), cookies.begin(), cookies.end()); - std::string website_data; - if(download_to_string(next_url, website_data, additional_args, use_tor, true) != DownloadResult::OK) - return; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(&website_data[0], &website_data[website_data.size()], &json_root, &json_errors)) { - fprintf(stderr, "Youtube search continuation json error: %s\n", json_errors.c_str()); - return; - } + DownloadResult result = download_json(json_root, next_url, std::move(additional_args), true); + if(result != DownloadResult::OK) return; if(!json_root.isArray()) return; @@ -521,18 +494,9 @@ namespace QuickMedia { //std::vector<CommandArg> cookies = get_cookies(); //additional_args.insert(additional_args.end(), cookies.begin(), cookies.end()); - std::string website_data; - if(download_to_string(modified_url + "&pbj=1", website_data, additional_args, use_tor, true) != DownloadResult::OK) - return result_items; - Json::Value json_root; - Json::CharReaderBuilder json_builder; - std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); - std::string json_errors; - if(!json_reader->parse(&website_data[0], &website_data[website_data.size()], &json_root, &json_errors)) { - fprintf(stderr, "Youtube related media error: %s\n", json_errors.c_str()); - return result_items; - } + DownloadResult result = download_json(json_root, modified_url + "&pbj=1", std::move(additional_args), true); + if(result != DownloadResult::OK) return result_items; if(!json_root.isArray()) return result_items; |