From a73eb41773d3f70867358bbef26563ca431b0908 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 13 Sep 2020 17:58:35 +0200 Subject: Add thumbnails to mangatown search results --- src/plugins/Mangatown.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/plugins/Mangatown.cpp') diff --git a/src/plugins/Mangatown.cpp b/src/plugins/Mangatown.cpp index cb3217b..67f4c29 100644 --- a/src/plugins/Mangatown.cpp +++ b/src/plugins/Mangatown.cpp @@ -40,7 +40,13 @@ namespace QuickMedia { return result == 0 ? SearchResult::OK : SearchResult::ERR; } + struct BodyItemImageContext { + BodyItems *body_items; + size_t index; + }; + SuggestionResult Mangatown::update_search_suggestions(const std::string &text, BodyItems &result_items) { +#if 0 std::string url = "https://www.mangatown.com/ajax/search/?query="; url += url_param_encode(text); @@ -95,6 +101,52 @@ namespace QuickMedia { } return SuggestionResult::OK; +#else + std::string url = "https://www.mangatown.com/search?name="; + url += url_param_encode(text); + + std::string website_data; + if(download_to_string(url, website_data, {}, use_tor, true) != DownloadResult::OK) + return SuggestionResult::NET_ERR; + + if(website_data.empty()) + return SuggestionResult::OK; + + 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, "//p[class='title']/a", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *item_data = (BodyItems*)userdata; + const char *href = quickmedia_html_node_get_attribute_value(node, "href"); + const char *title = quickmedia_html_node_get_attribute_value(node, "title"); + if(href && title && strncmp(href, "/manga/", 7) == 0) { + auto item = std::make_unique(strip(title)); + item->url = mangatown_url + href; + item_data->push_back(std::move(item)); + } + }, &result_items); + + BodyItemImageContext body_item_image_context; + body_item_image_context.body_items = &result_items; + body_item_image_context.index = 0; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//a[class='manga_cover']/img", + [](QuickMediaHtmlNode *node, void *userdata) { + auto *item_data = (BodyItemImageContext*)userdata; + const char *src = quickmedia_html_node_get_attribute_value(node, "src"); + if(src && item_data->index < item_data->body_items->size()) { + (*item_data->body_items)[item_data->index]->thumbnail_url = src; + item_data->index++; + } + }, &body_item_image_context); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return SuggestionResult::OK; +#endif } static bool is_number_with_zero_fill(const char *str) { -- cgit v1.2.3