From b09d1e70661226697e2441c18ea6ff59c387fb93 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 16 Jul 2021 07:31:02 +0200 Subject: Add sorting options to nyaa.si --- src/plugins/Matrix.cpp | 18 ++++++----- src/plugins/NyaaSi.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 17bc17b..7439bf6 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -81,13 +81,14 @@ namespace QuickMedia { } } - static void remove_body_item_by_url(BodyItems &body_items, const std::string &url) { - for(auto it = body_items.begin(); it != body_items.end();) { - if((*it)->url == url) - it = body_items.erase(it); - else - ++it; + static bool remove_body_item_by_url(BodyItems &body_items, const std::string &url) { + for(auto it = body_items.begin(); it != body_items.end(); ++it) { + if((*it)->url == url) { + body_items.erase(it); + return true; + } } + return false; } static int color_hash_code(const std::string &str) { @@ -873,7 +874,10 @@ namespace QuickMedia { } void MatrixInvitesPage::remove_body_item_by_room_id(const std::string &room_id) { - remove_body_item_by_url(body->items, room_id); + if(remove_body_item_by_url(body->items, room_id)) { + prev_invite_count = body->items.size(); + title = "Invites (" + std::to_string(body->items.size()) + ")"; + } } void MatrixInvitesPage::clear_data() { diff --git a/src/plugins/NyaaSi.cpp b/src/plugins/NyaaSi.cpp index d4667af..0202322 100644 --- a/src/plugins/NyaaSi.cpp +++ b/src/plugins/NyaaSi.cpp @@ -107,11 +107,55 @@ namespace QuickMedia { return time_str; } + static const std::array sort_type_names = { + "Size desc 🡇", + "Uploaded date desc 🡇", + "Seeders desc 🡇", + "Leechers desc 🡇", + "Downloads desc 🡇", + + "Size asc 🡅", + "Uploaded date asc 🡅", + "Seeders asc 🡅", + "Leechers asc 🡅", + "Downloads asc 🡅" + }; + + static const std::array sort_params = { + "s=size&o=desc", + "s=id&o=desc", + "s=seeders&o=desc", + "s=leechers&o=desc", + "s=downloads&o=desc", + + "s=size&o=asc", + "s=id&o=asc", + "s=seeders&o=asc", + "s=leechers&o=asc", + "s=downloads&o=asc", + }; + + static std::shared_ptr create_sort_body_item(const std::string &title, NyaaSiSortType sort_type) { + auto body_item = BodyItem::create(title); + body_item->userdata = (void*)sort_type; + return body_item; + } + + static void sort_page_create_body_items(BodyItems &body_items, NyaaSiSortType sort_type) { + for(size_t i = 0; i < sort_type_names.size(); ++i) { + std::string prefix = " "; + if((NyaaSiSortType)i == sort_type) + prefix = "* "; + body_items.push_back(create_sort_body_item(prefix + sort_type_names[i], (NyaaSiSortType)i)); + } + } + // TODO: Also show the number of comments for each torrent. TODO: Optimize? // TODO: Show each field as seperate columns instead of seperating by | - static SearchResult search_page(const std::string &domain, const std::string &list_url, const std::string &text, int page, BodyItems &result_items) { + static SearchResult search_page(const std::string &domain, const std::string &list_url, const std::string &text, int page, NyaaSiSortType sort_type, BodyItems &result_items) { std::string full_url = "https://" + domain + "/?c=" + list_url + "&f=0&p=" + std::to_string(page) + "&q="; full_url += url_param_encode(text); + full_url += std::string("&") + sort_params[(size_t)sort_type]; std::string website_data; if(download_to_string(full_url, website_data, {}, true) != DownloadResult::OK) @@ -251,21 +295,34 @@ namespace QuickMedia { std::string domain = is_sukebei ? "sukebei.nyaa.si" : "nyaa.si"; BodyItems result_items; - SearchResult search_result = search_page(domain, url, "", 1, result_items); + SearchResult search_result = search_page(domain, url, "", 1, NyaaSiSortType::UPLOAD_DATE_DESC, result_items); if(search_result != SearchResult::OK) return search_result_to_plugin_result(search_result); + auto search_page = std::make_unique(program, strip(title), url, std::move(domain)); + NyaaSiSearchPage *search_page_p = search_page.get(); auto body = create_body(); body->items = std::move(result_items); - result_tabs.push_back(Tab{std::move(body), std::make_unique(program, strip(title), url, std::move(domain)), create_search_bar("Search...", 500)}); + result_tabs.push_back(Tab{std::move(body), std::move(search_page), create_search_bar("Search...", 500)}); + + auto sort_order_page_body = create_body(); + Body *sort_order_page_body_p = sort_order_page_body.get(); + sort_page_create_body_items(sort_order_page_body->items, NyaaSiSortType::UPLOAD_DATE_DESC); + result_tabs.push_back(Tab{std::move(sort_order_page_body), std::make_unique(program, sort_order_page_body_p, search_page_p), nullptr}); return PluginResult::OK; } + NyaaSiSearchPage::NyaaSiSearchPage(Program *program, std::string category_name, std::string category_id, std::string domain) : + Page(program), category_name(std::move(category_name)), category_id(std::move(category_id)), domain(std::move(domain)) + { + set_sort_type(NyaaSiSortType::UPLOAD_DATE_DESC); + } + SearchResult NyaaSiSearchPage::search(const std::string &str, BodyItems &result_items) { - return search_page(domain, category_id, str, 1, result_items); + return search_page(domain, category_id, str, 1, sort_type, result_items); } PluginResult NyaaSiSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { - return search_result_to_plugin_result(search_page(domain, category_id, str, 1 + page, result_items)); + return search_result_to_plugin_result(search_page(domain, category_id, str, 1 + page, sort_type, result_items)); } struct ResultItemExtra { @@ -441,6 +498,21 @@ namespace QuickMedia { return PluginResult::OK; } + void NyaaSiSearchPage::set_sort_type(NyaaSiSortType sort_type) { + this->sort_type = sort_type; + title = category_name + " | " + sort_type_names[(size_t)sort_type]; + title.erase(title.end() - 5, title.end()); // Erase emoji character and space at the end. TODO: Remove this when tabs support emojis. + } + + PluginResult NyaaSiSortOrderPage::submit(const std::string&, const std::string&, std::vector&) { + const NyaaSiSortType sort_type = (NyaaSiSortType)(size_t)submit_body_item->userdata; + body->items.clear(); + sort_page_create_body_items(body->items, sort_type); + search_page->set_sort_type(sort_type); + search_page->needs_refresh = true; + return PluginResult::OK; + } + PluginResult NyaaSiTorrentPage::submit(const std::string &title, const std::string &url, std::vector &result_tabs) { (void)title; (void)result_tabs; -- cgit v1.2.3