diff options
Diffstat (limited to 'src/plugins/NyaaSi.cpp')
-rw-r--r-- | src/plugins/NyaaSi.cpp | 131 |
1 files changed, 70 insertions, 61 deletions
diff --git a/src/plugins/NyaaSi.cpp b/src/plugins/NyaaSi.cpp index e29b2b0..8d0679e 100644 --- a/src/plugins/NyaaSi.cpp +++ b/src/plugins/NyaaSi.cpp @@ -1,5 +1,8 @@ #include "../../plugins/NyaaSi.hpp" #include "../../include/Program.h" +#include "../../include/Storage.hpp" +#include "../../include/Notification.hpp" +#include "../../include/StringUtils.hpp" #include <quickmedia/HtmlSearch.h> namespace QuickMedia { @@ -29,60 +32,19 @@ namespace QuickMedia { return true; } - NyaaSi::NyaaSi() : Plugin("nyaa.si") { - - } - - NyaaSi::~NyaaSi() { - - } - static std::shared_ptr<BodyItem> create_front_page_item(const std::string &title, const std::string &category) { auto body_item = BodyItem::create(title); body_item->url = category; return body_item; } - PluginResult NyaaSi::get_front_page(BodyItems &result_items) { - result_items.push_back(create_front_page_item("All categories", "0_0")); - result_items.push_back(create_front_page_item("Anime", "1_0")); - result_items.push_back(create_front_page_item(" Anime - Music video", "1_1")); - result_items.push_back(create_front_page_item(" Anime - English translated", "1_2")); - result_items.push_back(create_front_page_item(" Anime - Non-english translated", "1_3")); - result_items.push_back(create_front_page_item(" Anime - Raw", "1_4")); - result_items.push_back(create_front_page_item("Audio", "2_0")); - result_items.push_back(create_front_page_item(" Audio - Lossless", "2_1")); - result_items.push_back(create_front_page_item(" Anime - Lossy", "2_2")); - result_items.push_back(create_front_page_item("Literature", "3_0")); - result_items.push_back(create_front_page_item(" Literature - English translated", "3_1")); - result_items.push_back(create_front_page_item(" Literature - Non-english translated", "3_1")); - result_items.push_back(create_front_page_item(" Literature - Raw", "3_3")); - result_items.push_back(create_front_page_item("Live Action", "4_0")); - result_items.push_back(create_front_page_item(" Live Action - English translated", "4_1")); - result_items.push_back(create_front_page_item(" Live Action - Non-english translated", "4_3")); - result_items.push_back(create_front_page_item(" Live Action - Idol/Promotional video", "4_2")); - result_items.push_back(create_front_page_item(" Live Action - Raw", "4_4")); - result_items.push_back(create_front_page_item("Pictures", "5_0")); - result_items.push_back(create_front_page_item(" Pictures - Graphics", "5_1")); - result_items.push_back(create_front_page_item(" Pictures - Photos", "5_2")); - result_items.push_back(create_front_page_item("Software", "6_0")); - result_items.push_back(create_front_page_item(" Software - Applications", "6_1")); - result_items.push_back(create_front_page_item(" Software - Games", "6_2")); - return PluginResult::OK; - } - - - SearchResult NyaaSi::content_list_search(const std::string &list_url, const std::string &text, BodyItems &result_items) { - return search_page(list_url, text, 1, result_items); - } - - SearchResult NyaaSi::content_list_search_page(const std::string &list_url, const std::string &text, int page, BodyItems &result_items) { - return search_page(list_url, text, 1 + page, result_items); + static PluginResult search_result_to_plugin_result(SearchResult search_result) { + return (PluginResult)search_result; } // TODO: Also show the number of comments for each torrent. TODO: Optimize? // TODO: Show each field as seperate columns instead of seperating by | - SearchResult NyaaSi::search_page(const std::string &list_url, const std::string &text, int page, BodyItems &result_items) { + static SearchResult search_page(const std::string &list_url, const std::string &text, int page, bool use_tor, BodyItems &result_items) { std::string full_url = "https://nyaa.si/?c=" + list_url + "&f=0&p=" + std::to_string(page) + "&q="; full_url += url_param_encode(text); @@ -217,34 +179,64 @@ namespace QuickMedia { return SearchResult::OK; } - static PluginResult search_result_to_plugin_result(SearchResult search_result) { - switch(search_result) { - case SearchResult::OK: return PluginResult::OK; - case SearchResult::ERR: return PluginResult::ERR; - case SearchResult::NET_ERR: return PluginResult::NET_ERR; - } - return PluginResult::ERR; + PluginResult NyaaSiCategoryPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { + BodyItems result_items; + SearchResult search_result = search_page(url, "", 1, is_tor_enabled(), result_items); + if(search_result != SearchResult::OK) return search_result_to_plugin_result(search_result); + + auto body = create_body(); + body->items = std::move(result_items); + body->draw_thumbnails = true; + result_tabs.push_back(Tab{std::move(body), std::make_unique<NyaaSiSearchPage>(program, strip(title), url), create_search_bar("Search...", 200)}); + return PluginResult::OK; } - PluginResult NyaaSi::get_content_list(const std::string &url, BodyItems &result_items) { - return search_result_to_plugin_result(search_page(url, "", 1, result_items)); + void NyaaSiCategoryPage::get_categories(BodyItems &result_items) { + result_items.push_back(create_front_page_item("All categories", "0_0")); + result_items.push_back(create_front_page_item("Anime", "1_0")); + result_items.push_back(create_front_page_item(" Anime - Music video", "1_1")); + result_items.push_back(create_front_page_item(" Anime - English translated", "1_2")); + result_items.push_back(create_front_page_item(" Anime - Non-english translated", "1_3")); + result_items.push_back(create_front_page_item(" Anime - Raw", "1_4")); + result_items.push_back(create_front_page_item("Audio", "2_0")); + result_items.push_back(create_front_page_item(" Audio - Lossless", "2_1")); + result_items.push_back(create_front_page_item(" Anime - Lossy", "2_2")); + result_items.push_back(create_front_page_item("Literature", "3_0")); + result_items.push_back(create_front_page_item(" Literature - English translated", "3_1")); + result_items.push_back(create_front_page_item(" Literature - Non-english translated", "3_1")); + result_items.push_back(create_front_page_item(" Literature - Raw", "3_3")); + result_items.push_back(create_front_page_item("Live Action", "4_0")); + result_items.push_back(create_front_page_item(" Live Action - English translated", "4_1")); + result_items.push_back(create_front_page_item(" Live Action - Non-english translated", "4_3")); + result_items.push_back(create_front_page_item(" Live Action - Idol/Promotional video", "4_2")); + result_items.push_back(create_front_page_item(" Live Action - Raw", "4_4")); + result_items.push_back(create_front_page_item("Pictures", "5_0")); + result_items.push_back(create_front_page_item(" Pictures - Graphics", "5_1")); + result_items.push_back(create_front_page_item(" Pictures - Photos", "5_2")); + result_items.push_back(create_front_page_item("Software", "6_0")); + result_items.push_back(create_front_page_item(" Software - Applications", "6_1")); + result_items.push_back(create_front_page_item(" Software - Games", "6_2")); } - struct BodyItemImageContext { - BodyItems *body_items; - size_t index; - }; + SearchResult NyaaSiSearchPage::search(const std::string &str, BodyItems &result_items) { + return search_page(category_id, str, 1, is_tor_enabled(), result_items); + } - PluginResult NyaaSi::get_content_details(const std::string&, const std::string &url, BodyItems &result_items) { + PluginResult NyaaSiSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { + return search_result_to_plugin_result(search_page(category_id, str, 1 + page, is_tor_enabled(), result_items)); + } + + PluginResult NyaaSiSearchPage::submit(const std::string&, const std::string &url, std::vector<Tab> &result_tabs) { size_t comments_start_index; std::string title; + BodyItems result_items; auto torrent_item = BodyItem::create("Download magnet"); std::string magnet_url; std::string description; std::string website_data; - if(download_to_string(url, website_data, {}, use_tor, true) != DownloadResult::OK) + if(download_to_string(url, website_data, {}, is_tor_enabled(), true) != DownloadResult::OK) return PluginResult::NET_ERR; QuickMediaHtmlSearch html_search; @@ -377,9 +369,26 @@ namespace QuickMedia { cleanup: quickmedia_html_search_deinit(&html_search); - if(result != 0) { - result_items.clear(); + if(result != 0) return PluginResult::ERR; + + auto body = create_body(); + body->items = std::move(result_items); + body->draw_thumbnails = true; + result_tabs.push_back(Tab{std::move(body), std::make_unique<NyaaSiTorrentPage>(program), nullptr}); + return PluginResult::OK; + } + + PluginResult NyaaSiTorrentPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { + (void)title; + (void)result_tabs; + if(strncmp(url.c_str(), "magnet:?", 8) == 0) { + if(!is_program_executable_by_name("xdg-open")) { + show_notification("Nyaa.si", "xdg-utils which provides xdg-open needs to be installed to download torrents", Urgency::CRITICAL); + return PluginResult::ERR; + } + const char *args[] = { "xdg-open", url.c_str(), nullptr }; + exec_program_async(args, nullptr); } return PluginResult::OK; } |