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/Body.cpp | 12 ++++++++---- src/plugins/Youtube.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Body.cpp b/src/Body.cpp index 52f3105..c9fbc89 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -110,12 +110,11 @@ namespace QuickMedia { draw(window, pos, size, empty_object); } - // TODO: Skip drawing the rows that are outside the window. // TODO: Use a render target for the whole body so all images can be put into one. - // TODO: Only load images once they are visible on the screen. + // TODO: Unload thumbnails once they are no longer visible on the screen. // TODO: Load thumbnails with more than one thread. // TODO: Show chapters (rows) that have been read differently to make it easier to see what - // needs hasn't been read yet. + // hasn't been read yet. void Body::draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const Json::Value &content_progress) { const float font_height = title_text.getCharacterSize() + 8.0f; const float image_height = 100.0f; @@ -159,10 +158,15 @@ namespace QuickMedia { ++visible_rows; } + auto window_size = window.getSize(); + for(int i = first_visible_item + 1; i < num_items; ++i) { const auto &item = items[i]; - assert(items.size() == item_thumbnail_textures.size()); auto &item_thumbnail = item_thumbnail_textures[i]; + + if(pos.y >= window_size.y) + return; + if(!item->visible) continue; 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