diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 27 | ||||
-rw-r--r-- | src/SearchBar.cpp | 3 | ||||
-rw-r--r-- | src/plugins/Manganelo.cpp | 4 | ||||
-rw-r--r-- | src/plugins/Plugin.cpp | 6 | ||||
-rw-r--r-- | src/plugins/Youtube.cpp | 117 |
5 files changed, 85 insertions, 72 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index d0a0ee7..856fc73 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -41,7 +41,7 @@ namespace QuickMedia { delete current_plugin; } - static SearchResult search_selected_suggestion(Body *body, Plugin *plugin, Page &next_page, std::string &selected_title, std::string &selected_url) { + static SearchResult search_selected_suggestion(Body *body, Plugin *plugin, std::string &selected_title, std::string &selected_url) { BodyItem *selected_item = body->get_selected(); if(!selected_item) return SearchResult::ERR; @@ -49,7 +49,7 @@ namespace QuickMedia { selected_title = selected_item->title; selected_url = selected_item->url; body->clear_items(); - SearchResult search_result = plugin->search(!selected_url.empty() ? selected_url : selected_title, body->items, next_page); + SearchResult search_result = plugin->search(!selected_url.empty() ? selected_url : selected_title, body->items); body->reset_selected(); return search_result; } @@ -84,6 +84,8 @@ namespace QuickMedia { return -1; } + search_bar->text_autosearch_delay = current_plugin->get_search_delay(); + while(window.isOpen()) { switch(current_page) { case Page::EXIT: @@ -93,10 +95,12 @@ namespace QuickMedia { body->draw_thumbnails = current_plugin->search_suggestions_has_thumbnails(); search_suggestion_page(); break; +#if 0 case Page::SEARCH_RESULT: body->draw_thumbnails = current_plugin->search_results_has_thumbnails(); search_result_page(); break; +#endif case Page::VIDEO_CONTENT: body->draw_thumbnails = false; video_content_page(); @@ -201,8 +205,8 @@ namespace QuickMedia { }; search_bar->onTextSubmitCallback = [this](const std::string &text) { - Page next_page; - if(search_selected_suggestion(body, current_plugin, next_page, content_title, content_url) == SearchResult::OK) { + Page next_page = current_plugin->get_page_after_search(); + if(search_selected_suggestion(body, current_plugin, content_title, content_url) == SearchResult::OK) { if(next_page == Page::EPISODE_LIST) { Path content_storage_dir = get_storage_dir().join("manga"); if(create_directory_recursive(content_storage_dir) != 0) { @@ -261,6 +265,7 @@ namespace QuickMedia { } void Program::search_result_page() { + #if 0 search_bar->onTextUpdateCallback = [this](const std::string &text) { body->filter_search_fuzzy(text); body->clamp_selection(); @@ -307,23 +312,24 @@ namespace QuickMedia { search_bar->draw(window); window.display(); } + #endif } void Program::video_content_page() { search_bar->onTextUpdateCallback = nullptr; search_bar->onTextSubmitCallback = nullptr; - watched_videos.insert(video_url); + watched_videos.insert(content_url); std::unique_ptr<VideoPlayer> video_player = nullptr; try { - printf("Play video: %s\n", video_url.c_str()); - video_player.reset(new VideoPlayer(window, window_size.x, window_size.y, video_url.c_str())); + printf("Play video: %s\n", content_url.c_str()); + video_player.reset(new VideoPlayer(window, window_size.x, window_size.y, content_url.c_str())); } catch(VideoInitializationException &e) { show_notification("Video player", "Failed to create video player", Urgency::CRITICAL); video_player = nullptr; } - std::vector<std::unique_ptr<BodyItem>> related_media = current_plugin->get_related_media(video_url); + std::vector<std::unique_ptr<BodyItem>> related_media = current_plugin->get_related_media(content_url); bool reload = false; if(video_player) { @@ -342,8 +348,8 @@ namespace QuickMedia { if(new_video_url.empty()) return; - video_url = std::move(new_video_url); - related_media = current_plugin->get_related_media(video_url); + content_url = std::move(new_video_url); + related_media = current_plugin->get_related_media(content_url); // TODO: This doesn't seem to work correctly right now, it causes video to become black when changing video (context reset bug). //video_player->load_file(video_url); reload = true; @@ -443,6 +449,7 @@ namespace QuickMedia { } } + // TODO: This code is duplicated in many places. Handle it in one place. if(resized) { search_bar->onWindowResize(window_size); diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index f7ac48d..1094883 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -12,6 +12,7 @@ namespace QuickMedia { SearchBar::SearchBar(sf::Font &font) : onTextUpdateCallback(nullptr), onTextSubmitCallback(nullptr), + text_autosearch_delay(0), text("Search...", font, 18), show_placeholder(true), updated_search(false) @@ -29,7 +30,7 @@ namespace QuickMedia { } void SearchBar::update() { - if(updated_search && time_since_search_update.getElapsedTime().asMilliseconds() >= 150) { + if(updated_search && time_since_search_update.getElapsedTime().asMilliseconds() >= text_autosearch_delay) { updated_search = false; sf::String str = text.getString(); if(show_placeholder) diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp index 3c6dd5b..b1f02a3 100644 --- a/src/plugins/Manganelo.cpp +++ b/src/plugins/Manganelo.cpp @@ -3,9 +3,7 @@ #include <json/reader.h> namespace QuickMedia { - SearchResult Manganelo::search(const std::string &url, std::vector<std::unique_ptr<BodyItem>> &result_items, Page &next_page) { - next_page = Page::EPISODE_LIST; - + SearchResult Manganelo::search(const std::string &url, std::vector<std::unique_ptr<BodyItem>> &result_items) { std::string website_data; if(download_to_string(url, website_data) != DownloadResult::OK) return SearchResult::NET_ERR; diff --git a/src/plugins/Plugin.cpp b/src/plugins/Plugin.cpp index 2367cb3..86f5d7d 100644 --- a/src/plugins/Plugin.cpp +++ b/src/plugins/Plugin.cpp @@ -10,6 +10,12 @@ static int accumulate_string(char *data, int size, void *userdata) { } namespace QuickMedia { + SearchResult Plugin::search(const std::string &text, std::vector<std::unique_ptr<BodyItem>> &result_items) { + (void)text; + (void)result_items; + return SearchResult::OK; + } + SuggestionResult Plugin::update_search_suggestions(const std::string &text, std::vector<std::unique_ptr<BodyItem>> &result_items) { (void)text; (void)result_items; diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 40d545c..a5670ec 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -12,64 +12,6 @@ namespace QuickMedia { return strstr(str, substr); } - SearchResult Youtube::search(const std::string &text, std::vector<std::unique_ptr<BodyItem>> &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; - - struct ItemData { - std::vector<std::unique_ptr<BodyItem>> *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) - 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<std::unique_ptr<BodyItem>>*)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<BodyItem>(strip(title)); - item->url = std::string("https://www.youtube.com") + href; - 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); - return result == 0 ? SearchResult::OK : SearchResult::ERR; - } - static void iterate_suggestion_result(const Json::Value &value, std::vector<std::unique_ptr<BodyItem>> &result_items, int &iterate_count) { ++iterate_count; if(value.isArray()) { @@ -83,7 +25,11 @@ namespace QuickMedia { } } + // TODO: Speed this up by using string.find instead of parsing html SuggestionResult Youtube::update_search_suggestions(const std::string &text, std::vector<std::unique_ptr<BodyItem>> &result_items) { + // Keep this for backup. This is using search suggestion the same way youtube does it, but the results + // are not as good as doing an actual search. + #if 0 std::string url = "https://clients1.google.com/complete/search?client=youtube&hl=en&gs_rn=64&gs_ri=youtube&ds=yt&cp=7&gs_id=x&q="; url += url_param_encode(text); @@ -125,6 +71,61 @@ namespace QuickMedia { if(!found_search_text) result_items.insert(result_items.begin(), std::make_unique<BodyItem>(text)); return SuggestionResult::OK; + #endif + 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 SuggestionResult::NET_ERR; + + struct ItemData { + std::vector<std::unique_ptr<BodyItem>> *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) + 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<std::unique_ptr<BodyItem>>*)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<BodyItem>(strip(title)); + item->url = std::string("https://www.youtube.com") + href; + 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); + return result == 0 ? SuggestionResult::OK : SuggestionResult::ERR; } std::vector<std::unique_ptr<BodyItem>> Youtube::get_related_media(const std::string &url) { |