From 55cb9a1ffb4226e5771c45d294ae297fe0a1b94e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 7 Aug 2019 05:17:02 +0200 Subject: Add youtube thumbnails, dont draw rows outside window --- src/plugins/Youtube.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/plugins') diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 67a47e4..40d545c 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -8,6 +8,10 @@ namespace QuickMedia { return strncmp(str, begin_with, strlen(begin_with)) == 0; } + static bool contains(const char *str, const char *substr) { + return strstr(str, substr); + } + 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="; @@ -17,6 +21,12 @@ namespace QuickMedia { if(download_to_string(url, website_data) != DownloadResult::OK) return SearchResult::NET_ERR; + struct ItemData { + std::vector> *result_items; + size_t index; + }; + ItemData item_data = { &result_items, 0 }; + QuickMediaHtmlSearch html_search; int result = quickmedia_html_search_init(&html_search, website_data.c_str()); if(result != 0) @@ -34,6 +44,26 @@ namespace QuickMedia { result_items->push_back(std::move(item)); } }, &result_items); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//span[class=\"yt-thumb-simple\"]//img", + [](QuickMediaHtmlNode *node, void *userdata) { + ItemData *item_data = (ItemData*)userdata; + if(item_data->index >= item_data->result_items->size()) + return; + + const char *src = quickmedia_html_node_get_attribute_value(node, "src"); + const char *data_thumb = quickmedia_html_node_get_attribute_value(node, "data-thumb"); + + if(src && contains(src, "i.ytimg.com/")) { + (*item_data->result_items)[item_data->index]->thumbnail_url = src; + ++item_data->index; + } else if(data_thumb && contains(data_thumb, "i.ytimg.com/")) { + (*item_data->result_items)[item_data->index]->thumbnail_url = data_thumb; + ++item_data->index; + } + }, &item_data); cleanup: quickmedia_html_search_deinit(&html_search); -- cgit v1.2.3