From 67618e51ed44effba871447255a5e7389969ccaa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 28 Apr 2021 22:50:42 +0200 Subject: Create generic media plugin --- src/QuickMedia.cpp | 79 ++++++++++---------- src/plugins/MangaGeneric.cpp | 13 +++- src/plugins/MediaGeneric.cpp | 172 +++++++++++++++++++++++++++++++++++++++++++ src/plugins/Pornhub.cpp | 164 ----------------------------------------- src/plugins/Spankbang.cpp | 157 --------------------------------------- 5 files changed, 222 insertions(+), 363 deletions(-) create mode 100644 src/plugins/MediaGeneric.cpp delete mode 100644 src/plugins/Pornhub.cpp delete mode 100644 src/plugins/Spankbang.cpp (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 4b645d4..fd679ff 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3,9 +3,8 @@ #include "../plugins/Mangadex.hpp" #include "../plugins/MangaGeneric.hpp" #include "../plugins/MangaCombined.hpp" +#include "../plugins/MediaGeneric.hpp" #include "../plugins/Youtube.hpp" -#include "../plugins/Pornhub.hpp" -#include "../plugins/Spankbang.hpp" #include "../plugins/Fourchan.hpp" #include "../plugins/NyaaSi.hpp" #include "../plugins/Matrix.hpp" @@ -789,6 +788,22 @@ namespace QuickMedia { .manga_id_handler("/manga/", nullptr); } + static void add_pornhub_handlers(MediaGenericSearchPage *media_generic_search_page) { + media_generic_search_page->search_handler("https://www.pornhub.com/video/search?search=%s&page=%p", 1) + .text_handler({{"//div[class='nf-videos']//div[class='phimage']//a", "title", "href", "/view_video.php"}}) + .thumbnail_handler({{"//div[class='nf-videos']//div[class='phimage']//img", "data-src", "/videos/"}}) + .related_media_text_handler({{"//div[class='phimage']//a", "title", "href", "/view_video.php"}}) + .related_media_thumbnail_handler({{"//div[class='phimage']//img", "data-src", nullptr}}); + } + + static void add_spankbang_handlers(MediaGenericSearchPage *media_generic_search_page) { + media_generic_search_page->search_handler("https://spankbang.com/s/%s/%p/", 1) + .text_handler({{"//div[class='main_results']//div[class='video-item']//a[class='n']", "text", "href", "/video/"}}) + .thumbnail_handler({{"//div[class='main_results']//div[class='video-item']//img", "data-src", nullptr}}) + .related_media_text_handler({{"//div[class='right']//div[class='video-item']//a[class='n']", "text", "href", "/video/"}}) + .related_media_thumbnail_handler({{"//div[class='right']//div[class='video-item']//img", "data-src", nullptr}}); + } + void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir) { if(!plugin_name || plugin_name[0] == '\0') return; @@ -831,58 +846,48 @@ namespace QuickMedia { pipe_body->items.push_back(create_launcher_body_item("YouTube (audio only)", "youtube-audio", resources_root + "icons/yt_launcher.png")); tabs.push_back(Tab{std::move(pipe_body), std::make_unique(this, "Select plugin to launch"), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "manganelo") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 400)}); + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 400)}); - auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto history_page = std::make_unique(this, tabs.front().page.get(), search_bar.get(), HistoryType::MANGA); - tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); + tabs.push_back(Tab{create_body(), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "manganelos") == 0) { - auto search_body = create_body(); auto search_page = std::make_unique(this, plugin_name, "http://manganelos.com/"); add_manganelos_handlers(search_page.get()); - tabs.push_back(Tab{std::move(search_body), std::move(search_page), create_search_bar("Search...", 400)}); + tabs.push_back(Tab{create_body(), std::move(search_page), create_search_bar("Search...", 400)}); - auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto history_page = std::make_unique(this, tabs.front().page.get(), search_bar.get(), HistoryType::MANGA); - tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); + tabs.push_back(Tab{create_body(), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "mangatown") == 0) { - auto search_body = create_body(); - auto search_page = std::make_unique(this, plugin_name, "https://www.mangatown.com"); + auto search_page = std::make_unique(this, plugin_name, "https://www.mangatown.com/"); add_mangatown_handlers(search_page.get()); - tabs.push_back(Tab{std::move(search_body), std::move(search_page), create_search_bar("Search...", 400)}); + tabs.push_back(Tab{create_body(), std::move(search_page), create_search_bar("Search...", 400)}); - auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto history_page = std::make_unique(this, tabs.front().page.get(), search_bar.get(), HistoryType::MANGA); - tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); + tabs.push_back(Tab{create_body(), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "mangakatana") == 0) { - auto search_body = create_body(); - auto search_page = std::make_unique(this, plugin_name, "https://mangakatana.com", false); + auto search_page = std::make_unique(this, plugin_name, "https://mangakatana.com/", false); add_mangakatana_handlers(search_page.get()); - tabs.push_back(Tab{std::move(search_body), std::move(search_page), create_search_bar("Search...", 400)}); + tabs.push_back(Tab{create_body(), std::move(search_page), create_search_bar("Search...", 400)}); - auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto history_page = std::make_unique(this, tabs.front().page.get(), search_bar.get(), HistoryType::MANGA); - tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); + tabs.push_back(Tab{create_body(), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "mangadex") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 400)}); + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 400)}); - auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto history_page = std::make_unique(this, tabs.front().page.get(), search_bar.get(), HistoryType::MANGA); - tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); + tabs.push_back(Tab{create_body(), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "manga") == 0) { auto manganelo = std::make_unique(this); auto manganelos = std::make_unique(this, "manganelos", "http://manganelos.com/"); add_manganelos_handlers(manganelos.get()); - auto mangatown = std::make_unique(this, "mangatown", "https://www.mangatown.com"); + auto mangatown = std::make_unique(this, "mangatown", "https://www.mangatown.com/"); add_mangatown_handlers(mangatown.get()); - auto mangakatana = std::make_unique(this, "mangakatana", "https://mangakatana.com", false); + auto mangakatana = std::make_unique(this, "mangakatana", "https://mangakatana.com/", false); add_mangakatana_handlers(mangakatana.get()); std::vector pages; @@ -918,8 +923,7 @@ namespace QuickMedia { PipePage::load_body_items_from_stdin(pipe_body->items); tabs.push_back(Tab{std::move(pipe_body), std::make_unique(this), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "youtube") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 350)}); + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 350)}); auto history_body = create_body(); auto history_search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); @@ -931,23 +935,22 @@ namespace QuickMedia { tabs.push_back(Tab{create_body(), std::move(recommended_page), std::move(recommended_search_bar)}); tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "pornhub") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 500)}); + auto search_page = std::make_unique(this, "https://www.pornhub.com/", sf::Vector2i(320, 180)); + add_pornhub_handlers(search_page.get()); + tabs.push_back(Tab{create_body(), std::move(search_page), create_search_bar("Search...", 500)}); } else if(strcmp(plugin_name, "spankbang") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 500)}); + auto search_page = std::make_unique(this, "https://spankbang.com/", sf::Vector2i(500/2, 281/2)); + add_spankbang_handlers(search_page.get()); + tabs.push_back(Tab{create_body(), std::move(search_page), create_search_bar("Search...", 500)}); } else if(strcmp(plugin_name, "spotify") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 350)}); + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 350)}); no_video = true; } else if(strcmp(plugin_name, "soundcloud") == 0) { - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 500)}); + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 500)}); no_video = true; } else if(strcmp(plugin_name, "mastodon") == 0 || strcmp(plugin_name, "pleroma") == 0) { auto pleroma = std::make_shared(); - auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this, pleroma), create_search_bar("Search...", 350)}); + tabs.push_back(Tab{create_body(), std::make_unique(this, pleroma), create_search_bar("Search...", 350)}); } else if(strcmp(plugin_name, "matrix") == 0) { assert(!matrix); matrix = new Matrix(); diff --git a/src/plugins/MangaGeneric.cpp b/src/plugins/MangaGeneric.cpp index bf8a4c4..b3326fc 100644 --- a/src/plugins/MangaGeneric.cpp +++ b/src/plugins/MangaGeneric.cpp @@ -57,13 +57,17 @@ namespace QuickMedia { if(search_userdata->field2) { const char *field2_value = html_attr_or_inner_text(node, search_userdata->field2); if(field1_value && field2_value && (!search_userdata->field2_contains || strstr(field2_value, search_userdata->field2_contains))) { - auto item = BodyItem::create(strip(field1_value)); + std::string field1_fixed = strip(field1_value); + html_unescape_sequences(field1_fixed); + auto item = BodyItem::create(std::move(field1_fixed)); item->url = strip(field2_value); search_userdata->body_items->push_back(std::move(item)); } } else { if(field1_value) { - auto item = BodyItem::create(strip(field1_value)); + std::string field1_fixed = strip(field1_value); + html_unescape_sequences(field1_fixed); + auto item = BodyItem::create(std::move(field1_fixed)); search_userdata->body_items->push_back(std::move(item)); } } @@ -163,9 +167,10 @@ namespace QuickMedia { goto cleanup; for(const TextQuery &text_query : text_queries) { - if(!search_query.search_template || !text_query.html_query || !text_query.title_field) { + if(!text_query.html_query || !text_query.title_field) { assert(false); - return PluginResult::ERR; + result = -1; + goto cleanup; } BodyItems new_result_items; diff --git a/src/plugins/MediaGeneric.cpp b/src/plugins/MediaGeneric.cpp new file mode 100644 index 0000000..c911e6b --- /dev/null +++ b/src/plugins/MediaGeneric.cpp @@ -0,0 +1,172 @@ +#include "../../plugins/MediaGeneric.hpp" +#include "../../include/StringUtils.hpp" +#include + +namespace QuickMedia { + using HtmlPathCallback = std::function; + static int quickmedia_html_find_nodes_xpath(QuickMediaHtmlSearch *self, const char *xpath, HtmlPathCallback callback) { + return quickmedia_html_find_nodes_xpath(self, xpath, [](QuickMediaHtmlNode *node, void *userdata) { + HtmlPathCallback *callback = (HtmlPathCallback*)userdata; + (*callback)(node); + }, &callback); + } + + static const char* html_attr_or_inner_text(QuickMediaHtmlNode *node, const char *field_name) { + if(strcmp(field_name, "text") == 0) + return quickmedia_html_node_get_text(node); + else + return quickmedia_html_node_get_attribute_value(node, field_name); + } + + static PluginResult fetch_page_results(const std::string &url, const std::string &website_url, const std::vector &text_queries, const std::vector &thumbnail_queries, BodyItems &result_items) { + std::vector args; + if(!website_url.empty()) + args.push_back({ "-H", "referer: " + website_url }); + + std::string website_data; + if(download_to_string(url, website_data, args, true) != DownloadResult::OK) + return PluginResult::NET_ERR; + + if(website_data.empty()) + return PluginResult::OK; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + for(const MediaTextQuery &text_query : text_queries) { + if(!text_query.html_query || !text_query.title_field) { + assert(false); + result = -1; + goto cleanup; + } + + result = quickmedia_html_find_nodes_xpath(&html_search, text_query.html_query, [&text_query, &result_items](QuickMediaHtmlNode *node) { + const char *title_value = html_attr_or_inner_text(node, text_query.title_field); + const char *url_value = html_attr_or_inner_text(node, text_query.url_field); + if(title_value && url_value && (!text_query.url_contains || strstr(url_value, text_query.url_contains))) { + std::string field1_fixed = strip(title_value); + html_unescape_sequences(field1_fixed); + auto item = BodyItem::create(std::move(field1_fixed)); + item->url = strip(url_value); + result_items.push_back(std::move(item)); + } + }); + if(result != 0) + goto cleanup; + } + + for(const MediaThumbnailQuery &thumbnail_query : thumbnail_queries) { + assert(thumbnail_query.html_query && thumbnail_query.field_name); + if(thumbnail_query.html_query && thumbnail_query.field_name) { + size_t index = 0; + result = quickmedia_html_find_nodes_xpath(&html_search, thumbnail_query.html_query, [&thumbnail_query, &result_items, &index](QuickMediaHtmlNode *node) { + const char *field_value = html_attr_or_inner_text(node, thumbnail_query.field_name); + if(index < result_items.size() && field_value && (!thumbnail_query.field_contains || strstr(field_value, thumbnail_query.field_contains))) { + result_items[index]->thumbnail_url = strip(field_value); + ++index; + } + }); + if(result != 0) + goto cleanup; + } + } + + for(auto &body_item : result_items) { + if(string_starts_with(body_item->url, "//")) + body_item->url = "https://" + body_item->url.substr(2); + else if(string_starts_with(body_item->url, "/")) + body_item->url = website_url + body_item->url.substr(1); + + if(string_starts_with(body_item->thumbnail_url, "//")) + body_item->thumbnail_url = "https://" + body_item->thumbnail_url.substr(2); + else if(string_starts_with(body_item->thumbnail_url, "/")) + body_item->thumbnail_url = website_url + body_item->thumbnail_url.substr(1); + } + + cleanup: + quickmedia_html_search_deinit(&html_search); + if(result == 0) { + return PluginResult::OK; + } else { + result_items.clear(); + return PluginResult::ERR; + } + } + + MediaGenericSearchPage::MediaGenericSearchPage(Program *program, const char *website_url, sf::Vector2i thumbnail_max_size) : + Page(program), website_url(website_url ? website_url : ""), thumbnail_max_size(thumbnail_max_size) + { + if(!this->website_url.empty()) { + if(this->website_url.back() != '/') + this->website_url.push_back('/'); + } + } + + SearchResult MediaGenericSearchPage::search(const std::string &str, BodyItems &result_items) { + return plugin_result_to_search_result(get_page(str, 0, result_items)); + } + + PluginResult MediaGenericSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { + std::string url = search_query.search_template; + string_replace_all(url, "%s", url_param_encode(str)); + string_replace_all(url, "%p", std::to_string(search_query.page_start + page)); + return fetch_page_results(url, website_url, text_queries, thumbnail_queries, result_items); + } + + PluginResult MediaGenericSearchPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { + result_tabs.push_back(Tab{nullptr, std::make_unique(program, this, url), nullptr}); + return PluginResult::OK; + } + + PluginResult MediaGenericSearchPage::get_related_media(const std::string &url, BodyItems &result_items) { + return fetch_page_results(url, website_url, related_media_text_queries, related_media_thumbnail_queries, result_items); + } + + MediaGenericSearchPage& MediaGenericSearchPage::search_handler(const char *search_template, int page_start) { + search_query.search_template = search_template; + search_query.page_start = page_start; + return *this; + } + + MediaGenericSearchPage& MediaGenericSearchPage::text_handler(std::vector queries) { + text_queries = std::move(queries); + return *this; + } + + MediaGenericSearchPage& MediaGenericSearchPage::thumbnail_handler(std::vector queries) { + thumbnail_queries = std::move(queries); + return *this; + } + + MediaGenericSearchPage& MediaGenericSearchPage::related_media_text_handler(std::vector queries) { + related_media_text_queries = std::move(queries); + return *this; + } + + MediaGenericSearchPage& MediaGenericSearchPage::related_media_thumbnail_handler(std::vector queries) { + related_media_thumbnail_queries = std::move(queries); + return *this; + } + + PluginResult MediaGenericRelatedPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { + result_tabs.push_back(Tab{nullptr, std::make_unique(program, search_page, url), nullptr}); + return PluginResult::OK; + } + + BodyItems MediaGenericVideoPage::get_related_media(const std::string &url, std::string&) { + BodyItems result_items; + search_page->get_related_media(url, result_items); + return result_items; + } + + std::unique_ptr MediaGenericVideoPage::create_search_page(Program*, int &search_delay) { + search_delay = 500; // TODO: Make configurable? + return std::make_unique(*search_page); + } + + std::unique_ptr MediaGenericVideoPage::create_related_videos_page(Program *program, const std::string&, const std::string&) { + return std::make_unique(program, search_page); + } +} \ No newline at end of file diff --git a/src/plugins/Pornhub.cpp b/src/plugins/Pornhub.cpp deleted file mode 100644 index 1aa0851..0000000 --- a/src/plugins/Pornhub.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "../../plugins/Pornhub.hpp" -#include "../../include/StringUtils.hpp" -#include "../../include/NetUtils.hpp" -extern "C" { -#include -} -#include - -namespace { - // TODO: Optimize by using HtmlStringView instead of std::string - struct HtmlElement { - std::string tag_name; - std::map attributes; - std::vector children; - HtmlElement *parent = nullptr; // ref - }; - - struct HtmlParseUserdata { - HtmlElement *current_html_element; - }; -} - -namespace QuickMedia { - static bool begins_with(const char *str, const char *begin_with) { - return strncmp(str, begin_with, strlen(begin_with)) == 0; - } - - static void html_cleanup(HtmlElement *html_element_root) { - for(HtmlElement *child_html_element : html_element_root->children) { - html_cleanup(child_html_element); - } - delete html_element_root; - } - - static const std::string& html_get_attribute_or(HtmlElement *html_element, const std::string &attr_key, const std::string &default_value) { - auto it = html_element->attributes.find(attr_key); - if(it != html_element->attributes.end()) - return it->second; - else - return default_value; - } - - static void html_page_callback(HtmlParser *html_parser, HtmlParseType parse_type, void *userdata) { - HtmlParseUserdata *parse_userdata = (HtmlParseUserdata*)userdata; - if(parse_type == HTML_PARSE_TAG_START) { - auto new_html_element = new HtmlElement(); - new_html_element->tag_name.assign(html_parser->tag_name.data, html_parser->tag_name.size); - new_html_element->parent = parse_userdata->current_html_element; - - parse_userdata->current_html_element->children.push_back(new_html_element); - parse_userdata->current_html_element = new_html_element; - } else if(parse_type == HTML_PARSE_TAG_END) { - if(parse_userdata->current_html_element->parent) - parse_userdata->current_html_element = parse_userdata->current_html_element->parent; - } else if(parse_type == HTML_PARSE_ATTRIBUTE) { - std::string attr_key(html_parser->attribute_key.data, html_parser->attribute_key.size); - std::string attr_value(html_parser->attribute_value.data, html_parser->attribute_value.size); - parse_userdata->current_html_element->attributes.insert(std::make_pair(std::move(attr_key), std::move(attr_value))); - } - } - - static HtmlElement* html_parse(const char *source, size_t size) { - HtmlElement *html_element_root = new HtmlElement(); - HtmlParseUserdata parse_userdata; - parse_userdata.current_html_element = html_element_root; - html_parser_parse(source, size, html_page_callback, &parse_userdata); - return html_element_root; - } - - using HtmlFindTagsCallback = std::function; - static void html_find_tags_with_class(HtmlElement *html_element, const std::string &tag_name, const std::string &class_value, const HtmlFindTagsCallback &callback) { - if(html_element->tag_name == tag_name) { - if(html_get_attribute_or(html_element, "class", "") == class_value) - callback(html_element); - } - for(HtmlElement *child_html_element : html_element->children) { - html_find_tags_with_class(child_html_element, tag_name, class_value, callback); - } - } - - static void html_find_tags(HtmlElement *html_element, const std::string &tag_name, const HtmlFindTagsCallback &callback) { - if(html_element->tag_name == tag_name) - callback(html_element); - for(HtmlElement *child_html_element : html_element->children) { - html_find_tags(child_html_element, tag_name, callback); - } - } - - static SearchResult get_videos_in_page(const std::string &url, BodyItems &result_items) { - std::string website_data; - if(download_to_string(url, website_data, {}) != DownloadResult::OK) - return SearchResult::NET_ERR; - - HtmlElement *html_root = html_parse(website_data.data(), website_data.size()); - html_find_tags_with_class(html_root, "div", "phimage", [&result_items](HtmlElement *html_element) { - auto it = html_element->attributes.find("data-entrycode"); - if(it == html_element->attributes.end() || it->second != "VidPg-premVid-videoPage") { - html_find_tags(html_element, "a", [&result_items](HtmlElement *html_element) { - const std::string &href = html_get_attribute_or(html_element, "href", ""); - const std::string &title = html_get_attribute_or(html_element, "title", ""); - if(!href.empty() && !title.empty() && begins_with(href.c_str(), "/view_video.php?viewkey")) { - std::string title_fixed = strip(title); - html_unescape_sequences(title_fixed); - auto item = BodyItem::create(std::move(title_fixed)); - item->url = std::string("https://www.pornhub.com") + href; - item->thumbnail_size = sf::Vector2i(192, 108); - result_items.push_back(std::move(item)); - - html_find_tags(html_element, "img", [&result_items](HtmlElement *html_element) { - const std::string &src = html_get_attribute_or(html_element, "data-src", ""); - if(src.find("phncdn.com/videos") != std::string::npos) - result_items.back()->thumbnail_url = src; - }); - } - }); - } - }); - html_cleanup(html_root); - - // Attempt to skip promoted videos (that are not related to the search term) - if(result_items.size() >= 4) - result_items.erase(result_items.begin(), result_items.begin() + 4); - - return SearchResult::OK; - } - - SearchResult PornhubSearchPage::search(const std::string &str, BodyItems &result_items) { - std::string url = "https://www.pornhub.com/video/search?search="; - url += url_param_encode(str); - return get_videos_in_page(url, result_items); - } - - PluginResult PornhubSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { - std::string url = "https://www.pornhub.com/video/search?search="; - url += url_param_encode(str); - url += "&page=" + std::to_string(1 + page); - return search_result_to_plugin_result(get_videos_in_page(url, result_items)); - } - - PluginResult PornhubSearchPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { - result_tabs.push_back(Tab{nullptr, std::make_unique(program, url), nullptr}); - return PluginResult::OK; - } - - PluginResult PornhubRelatedVideosPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { - result_tabs.push_back(Tab{nullptr, std::make_unique(program, url), nullptr}); - return PluginResult::OK; - } - - BodyItems PornhubVideoPage::get_related_media(const std::string &url, std::string&) { - BodyItems result_items; - get_videos_in_page(url, result_items); - return result_items; - } - - std::unique_ptr PornhubVideoPage::create_search_page(Program *program, int &search_delay) { - search_delay = 500; - return std::make_unique(program); - } - - std::unique_ptr PornhubVideoPage::create_related_videos_page(Program *program, const std::string&, const std::string&) { - return std::make_unique(program); - } -} \ No newline at end of file diff --git a/src/plugins/Spankbang.cpp b/src/plugins/Spankbang.cpp deleted file mode 100644 index ac0e371..0000000 --- a/src/plugins/Spankbang.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include "../../plugins/Spankbang.hpp" -#include "../../include/StringUtils.hpp" -#include "../../include/NetUtils.hpp" -extern "C" { -#include -} -#include - -namespace { - // TODO: Optimize by using HtmlStringView instead of std::string - struct HtmlElement { - std::string tag_name; - std::map attributes; - std::vector children; - HtmlElement *parent = nullptr; // ref - }; - - struct HtmlParseUserdata { - HtmlElement *current_html_element; - }; -} - -namespace QuickMedia { - static void html_cleanup(HtmlElement *html_element_root) { - for(HtmlElement *child_html_element : html_element_root->children) { - html_cleanup(child_html_element); - } - delete html_element_root; - } - - static const std::string& html_get_attribute_or(HtmlElement *html_element, const std::string &attr_key, const std::string &default_value) { - auto it = html_element->attributes.find(attr_key); - if(it != html_element->attributes.end()) - return it->second; - else - return default_value; - } - - static void html_page_callback(HtmlParser *html_parser, HtmlParseType parse_type, void *userdata) { - HtmlParseUserdata *parse_userdata = (HtmlParseUserdata*)userdata; - if(parse_type == HTML_PARSE_TAG_START) { - auto new_html_element = new HtmlElement(); - new_html_element->tag_name.assign(html_parser->tag_name.data, html_parser->tag_name.size); - new_html_element->parent = parse_userdata->current_html_element; - - parse_userdata->current_html_element->children.push_back(new_html_element); - parse_userdata->current_html_element = new_html_element; - } else if(parse_type == HTML_PARSE_TAG_END) { - if(parse_userdata->current_html_element->parent) - parse_userdata->current_html_element = parse_userdata->current_html_element->parent; - } else if(parse_type == HTML_PARSE_ATTRIBUTE) { - std::string attr_key(html_parser->attribute_key.data, html_parser->attribute_key.size); - std::string attr_value(html_parser->attribute_value.data, html_parser->attribute_value.size); - parse_userdata->current_html_element->attributes.insert(std::make_pair(std::move(attr_key), std::move(attr_value))); - } - } - - static HtmlElement* html_parse(const char *source, size_t size) { - HtmlElement *html_element_root = new HtmlElement(); - HtmlParseUserdata parse_userdata; - parse_userdata.current_html_element = html_element_root; - html_parser_parse(source, size, html_page_callback, &parse_userdata); - return html_element_root; - } - - using HtmlFindTagsCallback = std::function; - static void html_find_tags_with_class(HtmlElement *html_element, const std::string &tag_name, const std::string &class_value, const HtmlFindTagsCallback &callback) { - if(html_element->tag_name == tag_name) { - if(html_get_attribute_or(html_element, "class", "") == class_value) - callback(html_element); - } - for(HtmlElement *child_html_element : html_element->children) { - html_find_tags_with_class(child_html_element, tag_name, class_value, callback); - } - } - - static void html_find_tags(HtmlElement *html_element, const std::string &tag_name, const HtmlFindTagsCallback &callback) { - if(html_element->tag_name == tag_name) - callback(html_element); - for(HtmlElement *child_html_element : html_element->children) { - html_find_tags(child_html_element, tag_name, callback); - } - } - - static SearchResult get_videos_in_page(const std::string &url, BodyItems &result_items) { - std::string website_data; - if(download_to_string(url, website_data, {}) != DownloadResult::OK) - return SearchResult::NET_ERR; - - HtmlElement *html_root = html_parse(website_data.data(), website_data.size()); - html_find_tags_with_class(html_root, "a", "thumb ", [&result_items](HtmlElement *html_element) { - const std::string &href = html_get_attribute_or(html_element, "href", ""); - auto item = BodyItem::create(""); - item->url = std::string("https://spankbang.com") + href; - result_items.push_back(std::move(item)); - html_find_tags(html_element, "img", [&result_items](HtmlElement *html_element) { - const std::string &title = html_get_attribute_or(html_element, "alt", ""); - if(!title.empty()) { - std::string title_fixed = strip(title); - html_unescape_sequences(title_fixed); - //result_items.back()->title_text = title_fixed; - result_items.back()->set_title(title_fixed); - result_items.back()->thumbnail_size = sf::Vector2i(800, 450); - - const std::string &src = html_get_attribute_or(html_element, "data-src", ""); - if(!src.empty()) - result_items.back()->thumbnail_url = src; - } - }); - }); - html_cleanup(html_root); - - // Attempt to skip promoted videos (that are not related to the search term) - //if(result_items.size() >= 4) - // result_items.erase(result_items.begin(), result_items.begin() + 4); - - return SearchResult::OK; - } - - SearchResult SpankbangSearchPage::search(const std::string &str, BodyItems &result_items) { - std::string url = "https://spankbang.com/s/"; - url += url_param_encode(str); - return get_videos_in_page(url, result_items); - } - - PluginResult SpankbangSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { - std::string url = "https://spankbang.com/s/"; - url += url_param_encode(str); - url += "/" + std::to_string(1 + page); - return search_result_to_plugin_result(get_videos_in_page(url, result_items)); - } - - PluginResult SpankbangSearchPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { - result_tabs.push_back(Tab{nullptr, std::make_unique(program, url), nullptr}); - return PluginResult::OK; - } - - PluginResult SpankbangRelatedVideosPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { - result_tabs.push_back(Tab{nullptr, std::make_unique(program, url), nullptr}); - return PluginResult::OK; - } - - BodyItems SpankbangVideoPage::get_related_media(const std::string &url, std::string&) { - BodyItems result_items; - get_videos_in_page(url, result_items); - return result_items; - } - - std::unique_ptr SpankbangVideoPage::create_search_page(Program *program, int &search_delay) { - search_delay = 500; - return std::make_unique(program); - } - - std::unique_ptr SpankbangVideoPage::create_related_videos_page(Program *program, const std::string&, const std::string&) { - return std::make_unique(program); - } -} \ No newline at end of file -- cgit v1.2.3