From 9d4879215bb7b3b89122a930f86248cc405536a1 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 13 Sep 2020 17:47:51 +0200 Subject: Add description to mangadex search result, add youtube timestamp --- src/QuickMedia.cpp | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index a8788d2..da280d8 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -101,7 +101,7 @@ namespace QuickMedia { if(!selected_item) return SearchResult::ERR; - selected_title = selected_item->title; + selected_title = selected_item->get_title(); selected_url = selected_item->url; if(!skip_search) { output_body->clear_items(); @@ -366,11 +366,34 @@ namespace QuickMedia { RECOMMENDED }; + // Returns relative time as a string (approximation) + static std::string timestamp_to_relative_time_str(time_t seconds) { + time_t minutes = seconds / 60; + time_t hours = minutes / 60; + time_t days = hours / 24; + time_t months = days / 30; + time_t years = days / 365; + + if(years >= 1) + return std::to_string(years) + " year" + (years == 1 ? "" : "s") + " ago"; + else if(months >= 1) + return std::to_string(months) + " month" + (months == 1 ? "" : "s") + " ago"; + else if(days >= 1) + return std::to_string(days) + " day" + (days == 1 ? "" : "s") + " ago"; + else if(hours >= 1) + return std::to_string(hours) + " hour" + (hours == 1 ? "" : "s") + " ago"; + else if(minutes >= 1) + return std::to_string(minutes) + " minute" + (minutes == 1 ? "" : "s") + " ago"; + else + return std::to_string(seconds) + " second" + (seconds == 1 ? "" : "s") + " ago"; + } + static void fill_history_items_from_json(const Json::Value &history_json, BodyItems &history_items) { assert(history_json.isArray()); BodyItems body_items; + time_t time_now = time(NULL); for(const Json::Value &item : history_json) { if(!item.isObject()) continue; @@ -385,13 +408,14 @@ namespace QuickMedia { continue; std::string title_str = title.asString(); - //const Json::Value ×tamp = item["timestamp"]; - //if(!timestamp.isNumeric()) - // continue; + const Json::Value ×tamp = item["timestamp"]; + if(!timestamp.isNumeric()) + continue; auto body_item = std::make_unique(std::move(title_str)); body_item->url = "https://www.youtube.com/watch?v=" + video_id_str; body_item->thumbnail_url = "https://img.youtube.com/vi/" + video_id_str + "/hqdefault.jpg"; + body_item->set_description(timestamp_to_relative_time_str(std::max(0l, time_now - timestamp.asInt64()))); body_items.push_back(std::move(body_item)); } @@ -1024,7 +1048,7 @@ namespace QuickMedia { existing_recommendation["recommended_timestamp"] = time_now; } else { Json::Value new_content_object(Json::objectValue); - new_content_object["title"] = body_item->title; + new_content_object["title"] = body_item->get_title(); new_content_object["recommended_timestamp"] = time_now; new_content_object["recommended_count"] = 1; recommended_json[recommended_video_id] = std::move(new_content_object); @@ -1155,7 +1179,7 @@ namespace QuickMedia { for(auto it = related_media_body->items.begin(), end = related_media_body->items.end(); it != end; ++it) { if(watched_videos.find((*it)->url) == watched_videos.end()) { new_video_url = (*it)->url; - new_video_title = (*it)->title; + new_video_title = (*it)->get_title(); break; } } @@ -1237,7 +1261,7 @@ namespace QuickMedia { video_player = std::make_unique(current_plugin->use_tor, use_system_mpv_config, video_event_callback, on_window_create); content_url = selected_item->url; - content_title = selected_item->title; + content_title = selected_item->get_title(); load_video_error_check(); } } @@ -1342,7 +1366,7 @@ namespace QuickMedia { void Program::select_episode(BodyItem *item, bool start_from_beginning) { images_url = item->url; - chapter_title = item->title; + chapter_title = item->get_title(); image_index = 0; switch(image_view_mode) { case ImageViewMode::SINGLE: @@ -1406,10 +1430,10 @@ namespace QuickMedia { else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::T && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) { BodyItem *selected_item = body->get_selected(); if(selected_item) { - if(track_media(TrackMediaType::HTML, content_title, selected_item->title, content_url) == 0) { - show_notification("Media tracker", "You are now tracking \"" + content_title + "\" after \"" + selected_item->title + "\"", Urgency::LOW); + if(track_media(TrackMediaType::HTML, content_title, selected_item->get_title(), content_url) == 0) { + show_notification("Media tracker", "You are now tracking \"" + content_title + "\" after \"" + selected_item->get_title() + "\"", Urgency::LOW); } else { - show_notification("Media tracker", "Failed to track media \"" + content_title + "\", chapter: \"" + selected_item->title + "\"", Urgency::CRITICAL); + show_notification("Media tracker", "Failed to track media \"" + content_title + "\", chapter: \"" + selected_item->get_title() + "\"", Urgency::CRITICAL); } } } @@ -1829,7 +1853,7 @@ namespace QuickMedia { if(!selected_item) return false; - content_episode = selected_item->title; + content_episode = selected_item->get_title(); content_url = selected_item->url; current_page = Page::CONTENT_DETAILS; body->clear_items(); @@ -1931,7 +1955,7 @@ namespace QuickMedia { if(!selected_item) return false; - content_episode = selected_item->title; + content_episode = selected_item->get_title(); content_url = selected_item->url; current_page = Page::IMAGE_BOARD_THREAD; body->clear_items(); -- cgit v1.2.3