diff options
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r-- | src/QuickMedia.cpp | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 8c3f5b6..3b059d9 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -187,16 +187,54 @@ namespace QuickMedia { Page *search_page; }; + static Path get_recommended_filepath(const char *plugin_name) { + Path video_history_dir = get_storage_dir().join("recommended"); + if(create_directory_recursive(video_history_dir) != 0) { + std::string err_msg = "Failed to create recommended directory "; + err_msg += video_history_dir.data; + show_notification("QuickMedia", err_msg.c_str(), Urgency::CRITICAL); + exit(1); + } + + Path video_history_filepath = video_history_dir; + return video_history_filepath.join(plugin_name).append(".json"); + } + // TODO: Make asynchronous - static void fill_recommended_items_from_json(const Json::Value &recommended_json, BodyItems &body_items) { + static void fill_recommended_items_from_json(const char *plugin_name, const Json::Value &recommended_json, BodyItems &body_items) { assert(recommended_json.isObject()); + const int64_t recommendations_autodelete_period = 60*60*24*20; // 20 days + time_t time_now = time(NULL); + int num_items_deleted = 0; + std::vector<std::pair<std::string, Json::Value>> recommended_items(recommended_json.size()); /* TODO: Optimize member access */ for(auto &member_name : recommended_json.getMemberNames()) { Json::Value recommended_item = recommended_json[member_name]; - if(recommended_item.isObject()) - recommended_items.push_back(std::make_pair(member_name, std::move(recommended_item))); + if(recommended_item.isObject()) { + Json::Value recommended_timestamp_json = recommended_item.get("recommended_timestamp", Json::Value::nullSingleton()); + Json::Value watched_timestamp_json = recommended_item.get("watched_timestamp", Json::Value::nullSingleton()); + if(watched_timestamp_json.isNumeric() && time_now - watched_timestamp_json.asInt64() >= recommendations_autodelete_period) { + ++num_items_deleted; + } else if(recommended_timestamp_json.isNumeric() && time_now - recommended_timestamp_json.asInt64() >= recommendations_autodelete_period) { + ++num_items_deleted; + } else if(recommended_timestamp_json.isNull() && watched_timestamp_json.isNull()) { + ++num_items_deleted; + } else { + recommended_items.push_back(std::make_pair(member_name, std::move(recommended_item))); + } + } + } + + if(num_items_deleted > 0) { + // TODO: Is there a better way? + Json::Value new_recommendations(Json::objectValue); + for(auto &recommended : recommended_items) { + new_recommendations[recommended.first] = recommended.second; + } + fprintf(stderr, "Number of old recommendations to delete: %d\n", num_items_deleted); + save_json_to_file_atomic(get_recommended_filepath(plugin_name), new_recommendations); } /* TODO: Better algorithm for recommendations */ @@ -567,7 +605,7 @@ namespace QuickMedia { auto recommended_body = create_body(); recommended_body->draw_thumbnails = true; - fill_recommended_items_from_json(load_recommended_json(), recommended_body->items); + fill_recommended_items_from_json(plugin_name, load_recommended_json(), recommended_body->items); tabs.push_back(Tab{std::move(recommended_body), std::make_unique<RecommendedPage>(this, tabs.front().page.get()), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "pornhub") == 0) { auto search_body = create_body(); @@ -727,19 +765,6 @@ namespace QuickMedia { return video_history_filepath.join(plugin_name).append(".json"); } - static Path get_recommended_filepath(const char *plugin_name) { - Path video_history_dir = get_storage_dir().join("recommended"); - if(create_directory_recursive(video_history_dir) != 0) { - std::string err_msg = "Failed to create recommended directory "; - err_msg += video_history_dir.data; - show_notification("QuickMedia", err_msg.c_str(), Urgency::CRITICAL); - exit(1); - } - - Path video_history_filepath = video_history_dir; - return video_history_filepath.join(plugin_name).append(".json"); - } - // This is not cached because we could have multiple instances of QuickMedia running the same plugin! // TODO: Find a way to optimize this Json::Value Program::load_video_history_json() { @@ -1705,7 +1730,7 @@ namespace QuickMedia { current_page = previous_page; break; } else if(update_err != VideoPlayer::Error::OK) { - show_notification("Video player", "The video player failed to play the video", Urgency::CRITICAL); + show_notification("Video player", "The video player failed to play the video (error code " + std::to_string((int)update_err) + ")", Urgency::CRITICAL); current_page = previous_page; break; } |