From 657edb8eb9ab2fdef60d9c5d23a4c3093a64d859 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Aug 2019 18:17:00 +0200 Subject: Add manga chapter viewing --- src/plugins/Manganelo.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++++ src/plugins/Youtube.cpp | 120 +++++++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 src/plugins/Manganelo.cpp create mode 100644 src/plugins/Youtube.cpp (limited to 'src/plugins') diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp new file mode 100644 index 0000000..bc99387 --- /dev/null +++ b/src/plugins/Manganelo.cpp @@ -0,0 +1,146 @@ +#include "../../plugins/Manganelo.hpp" +#include +#include + +namespace QuickMedia { + SearchResult Manganelo::search(const std::string &url, std::vector> &result_items, Page &next_page) { + next_page = Page::EPISODE_LIST; + + std::string website_data; + if(download_to_string(url, website_data) != DownloadResult::OK) + return SearchResult::NET_ERR; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//div[class='chapter-list']/div[class='row']//a", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *item_data = (std::vector>*)userdata; + const char *href = quickmedia_html_node_get_attribute_value(node, "href"); + const char *text = quickmedia_html_node_get_text(node); + if(href && text) { + auto item = std::make_unique(text); + item->url = href; + item_data->push_back(std::move(item)); + } + }, &result_items); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return result == 0 ? SearchResult::OK : SearchResult::ERR; + } + + // Returns true if changed + static bool remove_html_span(std::string &str) { + size_t open_tag_start = str.find("', open_tag_start + 5); + if(open_tag_end == std::string::npos) + return false; + + str.erase(open_tag_start, open_tag_end - open_tag_start + 1); + + size_t close_tag = str.find(""); + if(close_tag == std::string::npos) + return true; + + str.erase(close_tag, 7); + return true; + } + + SuggestionResult Manganelo::update_search_suggestions(const std::string &text, std::vector> &result_items) { + std::string url = "https://manganelo.com/home_json_search"; + std::string search_term = "searchword="; + search_term += url_param_encode(text); + CommandArg data_arg = { "--data", std::move(search_term) }; + + std::string server_response; + if(download_to_string(url, server_response, {data_arg}) != DownloadResult::OK) + return SuggestionResult::NET_ERR; + + if(server_response.empty()) + return SuggestionResult::OK; + + Json::Value json_root; + Json::CharReaderBuilder json_builder; + std::unique_ptr json_reader(json_builder.newCharReader()); + std::string json_errors; + if(json_reader->parse(&server_response.front(), &server_response.back(), &json_root, &json_errors)) { + fprintf(stderr, "Manganelo suggestions json error: %s\n", json_errors.c_str()); + return SuggestionResult::ERR; + } + + if(json_root.isArray()) { + for(const Json::Value &child : json_root) { + if(child.isObject()) { + Json::Value name = child.get("name", ""); + Json::Value nameunsigned = child.get("nameunsigned", ""); + if(name.isString() && name.asCString()[0] != '\0' && nameunsigned.isString() && nameunsigned.asCString()[0] != '\0') { + std::string name_str = name.asString(); + while(remove_html_span(name_str)) {} + if(name_str != text) { + auto item = std::make_unique(name_str); + item->url = "https://manganelo.com/manga/" + url_param_encode(nameunsigned.asString()); + result_items.push_back(std::move(item)); + } + } + } + } + } + return SuggestionResult::OK; + } + + ImageResult Manganelo::get_image_by_index(const std::string &url, int index, std::string &image_data) { + if(url != last_chapter_url) { + printf("Get list of image urls for chapter: %s\n", url.c_str()); + last_chapter_image_urls.clear(); + ImageResult image_result = get_image_urls_for_chapter(url, last_chapter_image_urls); + if(image_result != ImageResult::OK) + return image_result; + last_chapter_url = url; + } + + int num_images = last_chapter_image_urls.size(); + if(index < 0 || index >= num_images) + return ImageResult::END; + + // TODO: Cache image in file/memory + switch(download_to_string(last_chapter_image_urls[index], image_data)) { + case DownloadResult::OK: + return ImageResult::OK; + case DownloadResult::ERR: + return ImageResult::ERR; + case DownloadResult::NET_ERR: + return ImageResult::NET_ERR; + default: + return ImageResult::ERR; + } + } + + ImageResult Manganelo::get_image_urls_for_chapter(const std::string &url, std::vector &urls) { + std::string website_data; + if(download_to_string(url, website_data) != DownloadResult::OK) + return ImageResult::NET_ERR; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//div[id='vungdoc']/img", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *urls = (std::vector*)userdata; + const char *src = quickmedia_html_node_get_attribute_value(node, "src"); + if(src) + urls->push_back(src); + }, &urls); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return result == 0 ? ImageResult::OK : ImageResult::ERR; + } +} \ No newline at end of file diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp new file mode 100644 index 0000000..6cc4ac6 --- /dev/null +++ b/src/plugins/Youtube.cpp @@ -0,0 +1,120 @@ +#include "../../plugins/Youtube.hpp" +#include +#include +#include + +namespace QuickMedia { + static bool begins_with(const char *str, const char *begin_with) { + return strncmp(str, begin_with, strlen(begin_with)) == 0; + } + + SearchResult Youtube::search(const std::string &text, std::vector> &result_items, Page &next_page) { + next_page = Page::SEARCH_RESULT; + std::string url = "https://youtube.com/results?search_query="; + url += url_param_encode(text); + + std::string website_data; + if(download_to_string(url, website_data) != DownloadResult::OK) + return SearchResult::NET_ERR; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"yt-lockup-title\"]/a", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *result_items = (std::vector>*)userdata; + const char *href = quickmedia_html_node_get_attribute_value(node, "href"); + const char *title = quickmedia_html_node_get_attribute_value(node, "title"); + // Checking for watch?v helps skipping ads + if(href && title && begins_with(href, "/watch?v=")) { + auto item = std::make_unique(title); + item->url = std::string("https://www.youtube.com") + href; + result_items->push_back(std::move(item)); + } + }, &result_items); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return result == 0 ? SearchResult::OK : SearchResult::ERR; + } + + static void iterate_suggestion_result(const Json::Value &value, const std::string &search_text, std::vector> &result_items) { + if(value.isArray()) { + for(const Json::Value &child : value) { + iterate_suggestion_result(child, search_text, result_items); + } + } else if(value.isString()) { + std::string title = value.asString(); + if(title != search_text) { + auto item = std::make_unique(title); + result_items.push_back(std::move(item)); + } + } + } + + SuggestionResult Youtube::update_search_suggestions(const std::string &text, std::vector> &result_items) { + result_items.push_back(std::make_unique(text)); + std::string url = "https://clients1.google.com/complete/search?client=youtube&hl=en&gl=us&q="; + url += url_param_encode(text); + + std::string server_response; + if(download_to_string(url, server_response) != DownloadResult::OK) + return SuggestionResult::NET_ERR; + + size_t json_start = server_response.find_first_of('('); + if(json_start == std::string::npos) + return SuggestionResult::ERR; + ++json_start; + + size_t json_end = server_response.find_last_of(')'); + if(json_end == std::string::npos) + return SuggestionResult::ERR; + + if(json_end == 0 || json_start >= json_end) + return SuggestionResult::ERR; + --json_end; + + Json::Value json_root; + Json::CharReaderBuilder json_builder; + std::unique_ptr json_reader(json_builder.newCharReader()); + std::string json_errors; + if(json_reader->parse(&server_response[json_start], &server_response[json_end], &json_root, &json_errors)) { + fprintf(stderr, "Youtube suggestions json error: %s\n", json_errors.c_str()); + return SuggestionResult::ERR; + } + + iterate_suggestion_result(json_root, text, result_items); + return SuggestionResult::OK; + } + + std::vector> Youtube::get_related_media(const std::string &url) { + std::vector> result_items; + + std::string website_data; + if(download_to_string(url, website_data) != DownloadResult::OK) + return result_items; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//ul[class=\"video-list\"]//div[class=\"content-wrapper\"]/a", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *result_items = (std::vector>*)userdata; + const char *href = quickmedia_html_node_get_attribute_value(node, "href"); + // TODO: Also add title for related media + if(href && begins_with(href, "/watch?v=")) { + auto item = std::make_unique(""); + item->url = std::string("https://www.youtube.com") + href; + result_items->push_back(std::move(item)); + } + }, &result_items); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return result_items; + } +} \ No newline at end of file -- cgit v1.2.3