From 68a2660da9ea4c660feda5d8569a8f43108964ec Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 31 May 2020 16:39:48 +0200 Subject: Use cookies --- src/plugins/Mangadex.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'src/plugins/Mangadex.cpp') diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 0ef7e74..5f6a0a4 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -1,5 +1,6 @@ #include "../../plugins/Mangadex.hpp" #include "../../include/Storage.hpp" +#include "../../include/Notification.hpp" #include #include @@ -113,7 +114,22 @@ namespace QuickMedia { return SearchResult::OK; } - static bool get_rememberme_token(std::string &rememberme_token) { + static bool get_cookie_filepath(std::string &cookie_filepath) { + Path cookie_path = get_storage_dir().join("cookies").join("mangadex.txt"); + if(create_directory_recursive(cookie_path) != 0) { + show_notification("Storage", "Failed to create directory: " + cookie_path.data, Urgency::CRITICAL); + return false; + } + cookie_filepath = cookie_path.data; + return true; + } + + bool Mangadex::get_rememberme_token(std::string &rememberme_token_output) { + if(rememberme_token) { + rememberme_token_output = rememberme_token.value(); + return true; + } + Path mangadex_credentials_path = get_storage_dir().join("credentials").join("mangadex.json"); std::string mangadex_credentials; if(file_get_content(mangadex_credentials_path, mangadex_credentials) != 0) { @@ -132,11 +148,11 @@ namespace QuickMedia { if(json_root.isObject()) { Json::Value &rememberme_token_json = json_root["rememberme_token"]; - if(rememberme_token_json.isString()) { + if(rememberme_token_json.isString()) rememberme_token = rememberme_token_json.asString(); - return true; - } } + + rememberme_token = rememberme_token_output; return true; } @@ -209,17 +225,35 @@ namespace QuickMedia { return ImageResult::OK; } + bool Mangadex::save_mangadex_cookies(const std::string &url, const std::string &cookie_filepath) { + CommandArg user_agent_arg = { "-H", useragent_str }; + CommandArg cookie_arg = { "-c", std::move(cookie_filepath) }; + std::string server_response; + if(download_to_string(url, server_response, {std::move(user_agent_arg), std::move(cookie_arg)}, use_tor) != DownloadResult::OK) + return false; + + return true; + } + ImageResult Mangadex::get_image_urls_for_chapter(const std::string &url) { if(url == last_chapter_url) return ImageResult::OK; last_chapter_image_urls.clear(); + std::string cookie_filepath; + if(!get_cookie_filepath(cookie_filepath)) + return ImageResult::ERR; + + if(!save_mangadex_cookies(url, cookie_filepath)) + return ImageResult::ERR; + CommandArg user_agent_arg = { "-H", useragent_str }; + 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(user_agent_arg)}, use_tor) != DownloadResult::OK) + if(download_to_string(request_url, server_response, {std::move(user_agent_arg), std::move(cookie_arg)}, use_tor) != DownloadResult::OK) return ImageResult::NET_ERR; if(server_response.empty()) -- cgit v1.2.3