aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/NyaaSi.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/NyaaSi.cpp')
-rw-r--r--src/plugins/NyaaSi.cpp83
1 files changed, 81 insertions, 2 deletions
diff --git a/src/plugins/NyaaSi.cpp b/src/plugins/NyaaSi.cpp
index cd80997..739032b 100644
--- a/src/plugins/NyaaSi.cpp
+++ b/src/plugins/NyaaSi.cpp
@@ -5,9 +5,80 @@
#include "../../include/StringUtils.hpp"
#include "../../include/NetUtils.hpp"
#include "../../include/Utils.hpp"
+#include "../../plugins/utils/EpisodeNameParser.hpp"
#include <quickmedia/HtmlSearch.h>
namespace QuickMedia {
+ static std::string combine_strings(const std::initializer_list<std::string_view> items, char divider) {
+ std::string result;
+ for(const auto &item : items) {
+ if(item.empty())
+ continue;
+
+ if(!result.empty())
+ result += divider;
+
+ result += item;
+ }
+ return result;
+ }
+
+ static TrackResult track(const std::string &str) {
+ std::optional<EpisodeNameParts> episode_parts = episode_name_extract_parts(str);
+ if(!episode_parts) {
+ show_notification("QuickMedia", "Failed to extract episode name from torrent name. Please add the rss manually with automedia", Urgency::CRITICAL);
+ return TrackResult::ERR;
+ }
+
+ const std::string track_name = combine_strings({ episode_parts->group, episode_parts->anime, episode_parts->episode, episode_parts->season, episode_parts->resolution }, ' ');
+
+ std::string url = "https://nyaa.si/?page=rss&q=" + url_param_encode(track_name) + "&c=0_0&f=0";
+ if(!episode_parts->group.empty())
+ url += "&u=" + url_param_decode(std::string(episode_parts->group));
+
+ std::string website_data;
+ if(download_to_string(url, website_data, {}, true) != DownloadResult::OK) {
+ show_notification("QuickMedia", "Invalid rss. Please add the rss manually with automedia", Urgency::CRITICAL);
+ return TrackResult::ERR;
+ }
+ struct HtmlFindUserdata {
+ const std::string &str;
+ bool found_title = false;
+ };
+ HtmlFindUserdata userdata{ str, false };
+
+ QuickMediaHtmlSearch html_search;
+ int result = quickmedia_html_search_init(&html_search, website_data.c_str(), website_data.size());
+ if(result != 0)
+ goto cleanup;
+
+ result = quickmedia_html_find_nodes_xpath(&html_search, "//channel/item/title",
+ [](QuickMediaMatchNode *node, void *userdata) {
+ HtmlFindUserdata &userdata_typed = *(HtmlFindUserdata*)userdata;
+ QuickMediaStringView text = quickmedia_html_node_get_text(node);
+ if(text.data && text.size == userdata_typed.str.size() && memcmp(text.data, userdata_typed.str.data(), text.size) == 0) {
+ userdata_typed.found_title = true;
+ }
+ return 0;
+ }, &userdata);
+
+ cleanup:
+ quickmedia_html_search_deinit(&html_search);
+ if(result != 0 || !userdata.found_title) {
+ show_notification("QuickMedia", "The item you tried to track was not found in the rss feed. Please add the rss manually with automedia", Urgency::CRITICAL);
+ return TrackResult::ERR;
+ }
+
+ const char *args[] = { "automedia", "add", "rss", url.c_str(), "--start-after", str.c_str(), "--name", track_name.c_str(), nullptr };
+ if(exec_program(args, nullptr, nullptr) == 0) {
+ show_notification("QuickMedia", "You are now tracking \"" + track_name + "\" after \"" + str + "\"", Urgency::LOW);
+ return TrackResult::OK;
+ } else {
+ show_notification("QuickMedia", "Failed to track media \"" + track_name + "\", episode: \"" + str + "\"", Urgency::CRITICAL);
+ return TrackResult::ERR;
+ }
+ }
+
// Return end of td tag, or std::string::npos
static size_t find_td_get_value(const std::string &str, size_t start_index, size_t end_index, std::string &result) {
size_t td_begin = str.find("<td", start_index);
@@ -310,7 +381,7 @@ namespace QuickMedia {
}
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))
+ Page(program), TrackablePage("", ""), category_name(std::move(category_name)), category_id(std::move(category_id)), domain(std::move(domain))
{
set_sort_type(NyaaSiSortType::UPLOAD_DATE_DESC);
}
@@ -493,10 +564,14 @@ namespace QuickMedia {
auto body = create_body();
body->set_items(std::move(result_items));
- result_tabs.push_back(Tab{std::move(body), std::make_unique<NyaaSiTorrentPage>(program), nullptr});
+ result_tabs.push_back(Tab{std::move(body), std::make_unique<NyaaSiTorrentPage>(program, args.title), nullptr});
return PluginResult::OK;
}
+ TrackResult NyaaSiSearchPage::track(const std::string &str) {
+ return QuickMedia::track(str);
+ }
+
void NyaaSiSearchPage::set_sort_type(NyaaSiSortType sort_type) {
this->sort_type = sort_type;
title = category_name + " | " + sort_type_names[(size_t)sort_type];
@@ -524,4 +599,8 @@ namespace QuickMedia {
}
return PluginResult::OK;
}
+
+ TrackResult NyaaSiTorrentPage::track(const std::string&) {
+ return QuickMedia::track(content_title);
+ }
} \ No newline at end of file