From ecb76941fe38cb8016388f6b2185312432cc8122 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 6 May 2021 18:46:28 +0200 Subject: Upgrade mangadex api to the new one There is a bug in mangadex api where order[publishAt]=desc is ignored, so going to the next chapter is messed up. Automedia will be updated after this is fixed. Also manga cover images are missing. --- src/QuickMedia.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 9 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index c9789cf..9593e80 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -249,6 +249,14 @@ static sf::Color interpolate_colors(sf::Color source, sf::Color target, double p source.a + diff_a * progress); } +static std::string base64_encode(const std::string &data) { + return cppcodec::base64_url::encode(data); +} + +static std::string base64_decode(const std::string &data) { + return cppcodec::base64_url::decode(data); +} + namespace QuickMedia { enum class HistoryType { YOUTUBE, @@ -802,6 +810,80 @@ namespace QuickMedia { .related_media_thumbnail_handler({{"//img", "src", "/thumb-"}}); } + static PluginResult upgrade_legacy_mangadex_ids(Program *program, Page *page) { + Path content_storage_dir = get_storage_dir().join("mangadex"); + if(create_directory_recursive(content_storage_dir) != 0) { + show_notification("QuickMedia", "Failed to create directory: " + content_storage_dir.data, Urgency::CRITICAL); + abort(); + } + + Path mangadex_upgraded = get_storage_dir().join("mangadex-upgraded"); + if(get_file_type(mangadex_upgraded) == FileType::REGULAR) + return PluginResult::OK; + + show_notification("QuickMedia", "Upgrading mangadex ids", Urgency::LOW); + + std::vector legacy_manga_ids; + for_files_in_dir_sort_last_modified(content_storage_dir, [&legacy_manga_ids](const std::filesystem::path &filepath) { + if(filepath.extension() == ".tmp") + return true; + + std::string filename = filepath.filename(); + if(filename.size() > 18) // Ignore new manga ids + return true; + + std::string id_str = base64_decode(filename); + char *endptr = nullptr; + errno = 0; + long id = strtol(id_str.c_str(), &endptr, 10); + if(endptr != id_str.c_str() && errno == 0) + legacy_manga_ids.push_back(id); + return true; + }); + + if(legacy_manga_ids.empty()) + return PluginResult::OK; + + std::vector> new_manga_ids; + TaskResult task_result = program->run_task_with_loading_screen([page, &legacy_manga_ids, &new_manga_ids]() { + return legacy_mangadex_id_to_new_manga_id(page, legacy_manga_ids, new_manga_ids) == PluginResult::OK; + }); + + if(task_result == TaskResult::TRUE) { + if(new_manga_ids.size() != legacy_manga_ids.size()) { + show_notification("QuickMedia", "Failed to upgrade legacy mangadex ids", Urgency::CRITICAL); + abort(); + } + + for(const auto &it : new_manga_ids) { + Path old_path = content_storage_dir; + old_path.join(base64_encode(std::to_string(it.first))); + + Path new_path = content_storage_dir; + new_path.join(base64_encode(it.second)); + if(rename_atomic(old_path.data.c_str(), new_path.data.c_str()) != 0) { + show_notification("QuickMedia", "Failed to upgrade legacy mangadex ids", Urgency::CRITICAL); + abort(); + } + } + + if(file_overwrite_atomic(mangadex_upgraded, "1") != 0) { + show_notification("QuickMedia", "Failed to upgrade legacy mangadex ids", Urgency::CRITICAL); + abort(); + } + + show_notification("QuickMedia", "Mangadex ids upgraded", Urgency::LOW); + return PluginResult::OK; + } else if(task_result == TaskResult::CANCEL) { + exit(0); + } else if(task_result == TaskResult::FALSE) { + show_notification("QuickMedia", "Failed to upgrade legacy mangadex ids", Urgency::CRITICAL); + abort(); + } + + return PluginResult::OK; + } + void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir, int &start_tab_index, FileManagerMimeType fm_mime_type) { if(!plugin_name || plugin_name[0] == '\0') return; @@ -872,6 +954,7 @@ namespace QuickMedia { tabs.push_back(Tab{create_body(), std::move(history_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "mangadex") == 0) { tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 400)}); + upgrade_legacy_mangadex_ids(this, tabs.back().page.get()); auto history_page = std::make_unique(this, tabs.front().page.get(), HistoryType::MANGA); tabs.push_back(Tab{create_body(), std::move(history_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); @@ -1029,14 +1112,6 @@ namespace QuickMedia { } } - static std::string base64_encode(const std::string &data) { - return cppcodec::base64_url::encode(data); - } - - static std::string base64_decode(const std::string &data) { - return cppcodec::base64_url::decode(data); - } - enum class SearchSuggestionTab { ALL, HISTORY, @@ -1169,7 +1244,7 @@ namespace QuickMedia { else if(strcmp(plugin_name, "manganelos") == 0) body_item->url = "http://manganelos.com/manga/" + base64_decode(filename.string()); else if(strcmp(plugin_name, "mangadex") == 0) - body_item->url = "https://mangadex.org/title/" + base64_decode(filename.string()); + body_item->url = base64_decode(filename.string()); else if(strcmp(plugin_name, "mangatown") == 0) body_item->url = "https://mangatown.com/manga/" + base64_decode(filename.string()); else if(strcmp(plugin_name, "mangakatana") == 0) -- cgit v1.2.3