aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-05-31 16:39:48 +0200
committerdec05eba <dec05eba@protonmail.com>2020-05-31 16:39:48 +0200
commit68a2660da9ea4c660feda5d8569a8f43108964ec (patch)
tree10f0d0e3ac0e88af362a2e144e6a761b38a4ec42
parent89c1a9ef17f2eb49d09e5e2b1459e1c58668ab6b (diff)
Use cookies
-rw-r--r--plugins/Mangadex.hpp3
-rw-r--r--src/plugins/Mangadex.cpp44
2 files changed, 42 insertions, 5 deletions
diff --git a/plugins/Mangadex.hpp b/plugins/Mangadex.hpp
index 5feb7f8..aaca18d 100644
--- a/plugins/Mangadex.hpp
+++ b/plugins/Mangadex.hpp
@@ -22,9 +22,12 @@ namespace QuickMedia {
private:
// Caches url. If the same url is requested multiple times then the cache is used
ImageResult get_image_urls_for_chapter(const std::string &url);
+ bool get_rememberme_token(std::string &rememberme_token);
+ bool save_mangadex_cookies(const std::string &url, const std::string &cookie_filepath);
private:
std::string last_chapter_url;
std::vector<std::string> last_chapter_image_urls;
std::mutex image_urls_mutex;
+ std::optional<std::string> rememberme_token;
};
} \ No newline at end of file
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 <quickmedia/HtmlSearch.h>
#include <json/reader.h>
@@ -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())