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/QuickMedia.cpp | 13 +++++++- src/plugins/Matrix.cpp | 18 ++++++----- src/plugins/NyaaSi.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 252bb19..64833bd 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2070,6 +2070,17 @@ namespace QuickMedia { if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->update(); + if(tabs[selected_tab].page->needs_refresh && tab_associated_data[selected_tab].fetch_status == FetchStatus::NONE && !tab_associated_data[selected_tab].fetching_next_page_running) { + tabs[selected_tab].page->needs_refresh = false; + if(tabs[selected_tab].page->is_lazy_fetch_page()) { + tab_associated_data[selected_tab].lazy_fetch_finished = false; + tab_associated_data[selected_tab].fetched_page = 0; + } else if(!tabs[selected_tab].page->search_is_filter()) { + tab_associated_data[selected_tab].search_text_updated = true; + } + tabs[selected_tab].body->clear_items(); + } + if(tabs[selected_tab].page->is_ready() && tabs[selected_tab].page->is_lazy_fetch_page() && tab_associated_data[selected_tab].fetch_status == FetchStatus::NONE && !tab_associated_data[selected_tab].lazy_fetch_finished) { tab_associated_data[selected_tab].fetch_status = FetchStatus::LOADING; tab_associated_data[selected_tab].fetch_type = FetchType::LAZY; @@ -2165,7 +2176,7 @@ namespace QuickMedia { associated_data.lazy_fetch_finished = true; FetchResult fetch_result = associated_data.fetch_future.get(); tabs[i].body->items = std::move(fetch_result.body_items); - if(tabs[i].search_bar) tabs[i].body->filter_search_fuzzy(tabs[i].search_bar->get_text()); + if(tabs[i].search_bar && tabs[i].page->search_is_filter()) tabs[i].body->filter_search_fuzzy(tabs[i].search_bar->get_text()); if(tabs[i].body->attach_side == AttachSide::TOP) { tabs[i].body->select_first_item(); } 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