aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/LocalAnime.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-02-18 19:26:23 +0100
committerdec05eba <dec05eba@protonmail.com>2022-02-18 19:26:23 +0100
commitde5fa7773f0393bfab917f8bf8606cb38ba3930e (patch)
treea0c5ec51f87948181797ccd2d3934b4a86c29a00 /src/plugins/LocalAnime.cpp
parent9301f1da5cf779516131f6bd5ddfc22991601128 (diff)
Refactor watch progress (prepare for global watch progress)
Diffstat (limited to 'src/plugins/LocalAnime.cpp')
-rw-r--r--src/plugins/LocalAnime.cpp160
1 files changed, 39 insertions, 121 deletions
diff --git a/src/plugins/LocalAnime.cpp b/src/plugins/LocalAnime.cpp
index 9757be1..53b26f5 100644
--- a/src/plugins/LocalAnime.cpp
+++ b/src/plugins/LocalAnime.cpp
@@ -41,7 +41,7 @@ namespace QuickMedia {
}
LocalAnimeItem anime_item;
- LocalAnimeWatchProgress watch_progress;
+ WatchProgress watch_progress;
};
static std::vector<LocalAnimeItem> get_episodes_in_directory(const Path &directory) {
@@ -133,90 +133,6 @@ namespace QuickMedia {
}
}
- static LocalAnimeWatchProgress get_watch_progress(const Json::Value &watched_json, const LocalAnimeItem &item) {
- LocalAnimeWatchProgress progress;
- Path latest_anime_path = get_latest_anime_item(item)->path;
-
- std::string filename_relative_to_anime_dir = latest_anime_path.data.substr(get_config().local_anime.directory.size() + 1);
- const Json::Value *found_watched_item = watched_json.find(
- filename_relative_to_anime_dir.data(),
- filename_relative_to_anime_dir.data() + filename_relative_to_anime_dir.size());
- if(!found_watched_item || !found_watched_item->isObject())
- return progress;
-
- const Json::Value &time_json = (*found_watched_item)["time"];
- const Json::Value &duration_json = (*found_watched_item)["duration"];
- if(!time_json.isInt64() || !duration_json.isInt64() || duration_json.asInt64() == 0)
- return progress;
-
- // We consider having watched the anime if the user stopped watching 90% in, because they might skip the ending theme/credits
- progress.time = (double)time_json.asInt64();
- progress.duration = (double)duration_json.asInt64();
- return progress;
- }
-
- enum class WatchedStatus {
- WATCHED,
- NOT_WATCHED
- };
-
- static bool toggle_watched_save_to_file(const Path &filepath, WatchedStatus &watched_status) {
- Path local_anime_progress_path = get_storage_dir().join("watch-progress").join("local-anime");
-
- Json::Value json_root;
- if(!read_file_as_json(local_anime_progress_path, json_root) || !json_root.isObject())
- json_root = Json::Value(Json::objectValue);
-
- bool watched = false;
- std::string filename_relative_to_anime_dir = filepath.data.substr(get_config().local_anime.directory.size() + 1);
- Json::Value &watched_item = json_root[filename_relative_to_anime_dir];
- if(watched_item.isObject()) {
- watched = true;
- } else {
- watched_item = Json::Value(Json::objectValue);
- watched = false;
- }
-
- if(watched) {
- json_root.removeMember(filename_relative_to_anime_dir.c_str());
- } else {
- FileAnalyzer file_analyzer;
- if(!file_analyzer.load_file(filepath.data.c_str(), true)) {
- show_notification("QuickMedia", "Failed to mark " + filename_relative_to_anime_dir + " as watched", Urgency::CRITICAL);
- return false;
- }
-
- if(!file_analyzer.get_duration_seconds() || *file_analyzer.get_duration_seconds() == 0) {
- show_notification("QuickMedia", "Failed to mark " + filename_relative_to_anime_dir + " as watched", Urgency::CRITICAL);
- return false;
- }
-
- watched_item["time"] = (int64_t)*file_analyzer.get_duration_seconds();
- watched_item["duration"] = (int64_t)*file_analyzer.get_duration_seconds();
- watched_item["thumbnail_url"] = filename_relative_to_anime_dir;
- watched_item["timestamp"] = (int64_t)time(nullptr);
- }
-
- if(!save_json_to_file_atomic(local_anime_progress_path, json_root)) {
- show_notification("QuickMedia", "Failed to mark " + filename_relative_to_anime_dir + " as " + (watched ? "not watched" : "watched"), Urgency::CRITICAL);
- return false;
- }
-
- watched_status = watched ? WatchedStatus::NOT_WATCHED : WatchedStatus::WATCHED;
- return true;
- }
-
- double LocalAnimeWatchProgress::get_watch_ratio() const {
- if(duration == 0.0)
- return 0.0;
- return (double)time / (double)duration;
- }
-
- // We consider having watched the anime if the user stopped watching 90% in, because they might skip the ending theme/credits
- bool LocalAnimeWatchProgress::has_finished_watching() const {
- return get_watch_ratio() >= 0.9;
- }
-
PluginResult LocalAnimeSearchPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
LocalAnimeBodyItemData *item_data = static_cast<LocalAnimeBodyItemData*>(args.extra.get());
if(std::holds_alternative<LocalAnime>(item_data->anime_item)) {
@@ -236,9 +152,7 @@ namespace QuickMedia {
}
PluginResult LocalAnimeSearchPage::lazy_fetch(BodyItems &result_items) {
- Json::Value json_root;
- if(!read_file_as_json(get_storage_dir().join("watch-progress").join("local-anime"), json_root) || !json_root.isObject())
- json_root = Json::Value(Json::objectValue);
+ std::unordered_map<std::string, WatchProgress> watch_progress = get_watch_progress_for_plugin("local-anime");
std::vector<LocalAnimeItem> anime_items;
switch(type) {
@@ -253,12 +167,21 @@ namespace QuickMedia {
break;
}
+ fprintf(stderr, "num watched animu: %zu\n", watch_progress.size());
+ for(auto &it : watch_progress) {
+ fprintf(stderr, "watch progress: %s, time: %d, duration: %d\n", it.first.c_str(), (int)it.second.time_pos_sec, (int)it.second.duration_sec);
+ }
+
const time_t time_now = time(nullptr);
for(LocalAnimeItem &anime_item : anime_items) {
+ std::string filename_relative_to_anime_dir = get_latest_anime_item(anime_item)->path.data.substr(get_config().local_anime.directory.size() + 1);
+
auto body_item_data = std::make_shared<LocalAnimeBodyItemData>();
- body_item_data->watch_progress = get_watch_progress(json_root, anime_item);
+ body_item_data->watch_progress = watch_progress[filename_relative_to_anime_dir];
const bool has_finished_watching = body_item_data->watch_progress.has_finished_watching();
+ fprintf(stderr, "watch progress %s: time: %d, duration: %d\n", filename_relative_to_anime_dir.c_str(), (int)body_item_data->watch_progress.time_pos_sec, (int)body_item_data->watch_progress.duration_sec);
+
if(std::holds_alternative<LocalAnime>(anime_item)) {
const LocalAnime &anime = std::get<LocalAnime>(anime_item);
@@ -332,8 +255,23 @@ namespace QuickMedia {
return;
LocalAnimeBodyItemData *item_data = static_cast<LocalAnimeBodyItemData*>(selected_item->extra.get());
+
+ Path latest_anime_path = get_latest_anime_item(item_data->anime_item)->path;
+ std::string filename_relative_to_anime_dir = latest_anime_path.data.substr(get_config().local_anime.directory.size() + 1);
+
+ FileAnalyzer file_analyzer;
+ if(!file_analyzer.load_file(latest_anime_path.data.c_str(), true)) {
+ show_notification("QuickMedia", "Failed to load " + filename_relative_to_anime_dir + " to set watch progress", Urgency::CRITICAL);
+ return;
+ }
+
+ if(!file_analyzer.get_duration_seconds()) {
+ show_notification("QuickMedia", "Failed to get duration of " + filename_relative_to_anime_dir + " to set watch progress", Urgency::CRITICAL);
+ return;
+ }
+
WatchedStatus watch_status;
- if(!toggle_watched_save_to_file(get_latest_anime_item(item_data->anime_item)->path, watch_status))
+ if(!toggle_watched_for_plugin_save_to_file("local-anime", filename_relative_to_anime_dir, *file_analyzer.get_duration_seconds(), latest_anime_path.data, watch_status))
return;
mgl::Color color = get_theme().text_color;
@@ -341,6 +279,12 @@ namespace QuickMedia {
if(watch_status == WatchedStatus::WATCHED) {
title = "[Finished watching] ";
color = finished_watching_color;
+
+ item_data->watch_progress.time_pos_sec = *file_analyzer.get_duration_seconds();
+ item_data->watch_progress.duration_sec = *file_analyzer.get_duration_seconds();
+ } else {
+ item_data->watch_progress.time_pos_sec = 0;
+ item_data->watch_progress.duration_sec = *file_analyzer.get_duration_seconds();
}
title += Path(selected_item->url).filename();
@@ -358,13 +302,13 @@ namespace QuickMedia {
// If we are very close to the end then start from the beginning.
// This is the same behavior as mpv.
// This is better because we dont want the video player to stop immediately after we start playing and we dont get any chance to seek.
- if(watch_progress.time + 10.0 >= watch_progress.duration)
+ if(watch_progress.time_pos_sec + 10.0 >= watch_progress.duration_sec)
return "0";
else
- return std::to_string(watch_progress.time);
+ return std::to_string(watch_progress.time_pos_sec);
}
- void LocalAnimeVideoPage::set_watch_progress(double time_pos_sec) {
+ void LocalAnimeVideoPage::set_watch_progress(int64_t time_pos_sec) {
std::string filename_relative_to_anime_dir = url.substr(get_config().local_anime.directory.size() + 1);
FileAnalyzer file_analyzer;
@@ -378,33 +322,7 @@ namespace QuickMedia {
return;
}
- watch_progress.duration = *file_analyzer.get_duration_seconds();
-
- Path watch_progress_dir = get_storage_dir().join("watch-progress");
- if(create_directory_recursive(watch_progress_dir) != 0) {
- show_notification("QuickMedia", "Failed to create " + watch_progress_dir.data + " to set watch progress for " + filename_relative_to_anime_dir, Urgency::CRITICAL);
- return;
- }
-
- Path local_anime_progress_path = watch_progress_dir;
- local_anime_progress_path.join("local-anime");
-
- Json::Value json_root;
- if(!read_file_as_json(local_anime_progress_path, json_root) || !json_root.isObject())
- json_root = Json::Value(Json::objectValue);
-
- Json::Value watch_progress_json(Json::objectValue);
- watch_progress_json["time"] = (int64_t)time_pos_sec;
- watch_progress_json["duration"] = (int64_t)watch_progress.duration;
- watch_progress_json["thumbnail_url"] = filename_relative_to_anime_dir;
- watch_progress_json["timestamp"] = (int64_t)time(nullptr);
- json_root[filename_relative_to_anime_dir] = std::move(watch_progress_json);
-
- if(!save_json_to_file_atomic(local_anime_progress_path, json_root)) {
- show_notification("QuickMedia", "Failed to set watch progress for " + filename_relative_to_anime_dir, Urgency::CRITICAL);
- return;
- }
-
- fprintf(stderr, "Set watch progress for \"%s\" to %d/%d\n", filename_relative_to_anime_dir.c_str(), (int)time_pos_sec, (int)watch_progress.duration);
+ watch_progress.duration_sec = *file_analyzer.get_duration_seconds();
+ set_watch_progress_for_plugin("local-anime", filename_relative_to_anime_dir, time_pos_sec, *file_analyzer.get_duration_seconds(), url);
}
} \ No newline at end of file