aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/QuickMedia.cpp138
1 files changed, 84 insertions, 54 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 884f29f..cdd2176 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -959,12 +959,78 @@ namespace QuickMedia {
return true;
}
+ void Program::save_recommendations_from_related_videos() {
+ std::string video_id;
+ if(!youtube_url_extract_id(content_url, video_id)) {
+ std::string err_msg = "Failed to extract id of youtube url ";
+ err_msg += content_url;
+ err_msg + ", video wont be saved in recommendations";
+ show_notification("Video player", err_msg.c_str(), Urgency::LOW);
+ return;
+ }
+
+ Json::Value recommended_json = load_recommended_json(current_plugin);
+ time_t time_now = time(NULL);
+
+ Json::Value &existing_recommended_json = recommended_json[video_id];
+ if(existing_recommended_json.isObject()) {
+ int64_t watched_count = 0;
+ Json::Value &watched_count_json = existing_recommended_json["watched_count"];
+ if(watched_count_json.isNumeric())
+ watched_count = watched_count_json.asInt64();
+ existing_recommended_json["watched_count"] = watched_count + 1;
+ existing_recommended_json["watched_timestamp"] = time_now;
+ } else {
+ Json::Value new_content_object(Json::objectValue);
+ new_content_object["title"] = content_title;
+ new_content_object["recommended_timestamp"] = time_now;
+ new_content_object["recommended_count"] = 1;
+ new_content_object["watched_count"] = 1;
+ new_content_object["watched_timestamp"] = time_now;
+ recommended_json[video_id] = std::move(new_content_object);
+ }
+
+ int saved_recommendation_count = 0;
+ for(const auto &body_item : related_media) {
+ std::string recommended_video_id;
+ if(youtube_url_extract_id(body_item->url, recommended_video_id)) {
+ Json::Value &existing_recommendation = recommended_json[recommended_video_id];
+ if(existing_recommendation.isObject()) {
+ int64_t recommended_count = 0;
+ Json::Value &count_json = existing_recommendation["recommended_count"];
+ if(count_json.isNumeric())
+ recommended_count = count_json.asInt64();
+ existing_recommendation["recommended_count"] = recommended_count + 1;
+ existing_recommendation["recommended_timestamp"] = time_now;
+ } else {
+ Json::Value new_content_object(Json::objectValue);
+ new_content_object["title"] = body_item->title;
+ new_content_object["recommended_timestamp"] = time_now;
+ new_content_object["recommended_count"] = 1;
+ recommended_json[recommended_video_id] = std::move(new_content_object);
+ saved_recommendation_count++;
+ /* TODO: Save more than the first 3 video that hasn't been watched yet? */
+ if(saved_recommendation_count == 3)
+ break;
+ }
+ } else {
+ fprintf(stderr, "Failed to extract id of youtube url %s, video wont be saved in recommendations\n", content_url.c_str());
+ }
+ }
+
+ save_json_to_file_atomic(get_recommended_filepath(current_plugin), recommended_json);
+ }
+
#define CLEANMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask|Mod4Mask|Mod5Mask))
void Program::video_content_page() {
search_bar->onTextUpdateCallback = nullptr;
search_bar->onTextSubmitCallback = nullptr;
+ sf::Clock time_watched_timer;
+ bool added_recommendations = false;
+ bool video_loaded = false;
+
int fallback_count = 0;
const int max_fallbacks = 3;
/* itags from: https://gist.github.com/sidneys/7095afe4da4ae58694d128b1034e01e2 */
@@ -983,7 +1049,9 @@ namespace QuickMedia {
XSync(disp, False);
};
- auto load_video_error_check = [this, &video_player, previous_page]() mutable {
+ auto load_video_error_check = [this, &video_player, previous_page, &time_watched_timer, &added_recommendations]() mutable {
+ time_watched_timer.restart();
+ added_recommendations = false;
watched_videos.insert(content_url);
VideoPlayer::Error err = video_player->load_video(content_url.c_str(), window.getSystemHandle(), current_plugin->name);
if(err != VideoPlayer::Error::OK) {
@@ -1027,61 +1095,11 @@ namespace QuickMedia {
Path video_history_filepath = get_video_history_filepath(current_plugin);
save_json_to_file_atomic(video_history_filepath, video_history_json);
-
- Json::Value recommended_json = load_recommended_json(current_plugin);
-
- Json::Value &existing_recommended_json = recommended_json[video_id];
- if(existing_recommended_json.isObject()) {
- int64_t watched_count = 0;
- Json::Value &watched_count_json = existing_recommended_json["watched_count"];
- if(watched_count_json.isNumeric())
- watched_count = watched_count_json.asInt64();
- existing_recommended_json["watched_count"] = watched_count + 1;
- existing_recommended_json["watched_timestamp"] = time_now;
- } else {
- Json::Value new_content_object(Json::objectValue);
- new_content_object["title"] = content_title;
- new_content_object["recommended_timestamp"] = time_now;
- new_content_object["recommended_count"] = 1;
- new_content_object["watched_count"] = 1;
- new_content_object["watched_timestamp"] = time_now;
- recommended_json[video_id] = std::move(new_content_object);
- }
-
- int saved_recommendation_count = 0;
- for(const auto &body_item : related_media) {
- std::string recommended_video_id;
- if(youtube_url_extract_id(body_item->url, recommended_video_id)) {
- Json::Value &existing_recommendation = recommended_json[recommended_video_id];
- if(existing_recommendation.isObject()) {
- int64_t recommended_count = 0;
- Json::Value &count_json = existing_recommendation["recommended_count"];
- if(count_json.isNumeric())
- recommended_count = count_json.asInt64();
- existing_recommendation["recommended_count"] = recommended_count + 1;
- existing_recommendation["recommended_timestamp"] = time_now;
- } else {
- Json::Value new_content_object(Json::objectValue);
- new_content_object["title"] = body_item->title;
- new_content_object["recommended_timestamp"] = time_now;
- new_content_object["recommended_count"] = 1;
- recommended_json[recommended_video_id] = std::move(new_content_object);
- saved_recommendation_count++;
- /* TODO: Save more than the first 3 video that hasn't been watched yet? */
- if(saved_recommendation_count == 3)
- break;
- }
- } else {
- fprintf(stderr, "Failed to extract id of youtube url %s, video wont be saved in recommendations\n", content_url.c_str());
- }
- }
-
- save_json_to_file_atomic(get_recommended_filepath(current_plugin), recommended_json);
}
};
bool has_video_started = true;
- auto video_event_callback = [this, &video_player, &load_video_error_check, previous_page, &has_video_started, &fallback_count](const char *event_name) mutable {
+ auto video_event_callback = [this, &video_player, &load_video_error_check, previous_page, &has_video_started, &fallback_count, &time_watched_timer, &video_loaded](const char *event_name) mutable {
bool end_of_file = false;
if(strcmp(event_name, "pause") == 0) {
double time_remaining = 9999.0;
@@ -1089,9 +1107,13 @@ namespace QuickMedia {
end_of_file = true;
} else if(strcmp(event_name, "playback-restart") == 0) {
video_player->set_paused(false);
- } else if(strcmp(event_name, "start-file") == 0) {
+ } else if(strcmp(event_name, "file-loaded") == 0) {
has_video_started = true;
fallback_count = 0;
+ time_watched_timer.restart();
+ video_loaded = true;
+ } else if(strcmp(event_name, "end-file") == 0) {
+ video_loaded = false;
}
if(end_of_file && has_video_started) {
@@ -1137,6 +1159,8 @@ namespace QuickMedia {
bool cursor_visible = true;
sf::Clock cursor_hide_timer;
+ bool is_youtube = current_plugin->name == "youtube";
+
while (current_page == Page::VIDEO_CONTENT) {
while (window.pollEvent(event)) {
base_event_handler(event, previous_page, true, false, false);
@@ -1164,7 +1188,7 @@ namespace QuickMedia {
show_notification("Video player", "Failed to connect to mpv ipc after 10 seconds", Urgency::CRITICAL);
current_page = previous_page;
break;
- } else if(update_err == VideoPlayer::Error::EXITED && fallback_count < max_fallbacks) {
+ } else if(is_youtube && update_err == VideoPlayer::Error::EXITED && fallback_count < max_fallbacks) {
++fallback_count;
std::string youtube_video_id;
if(youtube_url_extract_id(content_url, youtube_video_id)) {
@@ -1200,6 +1224,12 @@ namespace QuickMedia {
//window.clear();
//window.display();
+ /* Only save recommendations for the video if we have been watching it for 15 seconds */
+ if(is_youtube && video_loaded && !added_recommendations && time_watched_timer.getElapsedTime().asSeconds() >= 15) {
+ added_recommendations = true;
+ save_recommendations_from_related_videos();
+ }
+
if(video_player_window) {
if(!cursor_visible) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));