diff --git a/cli-commands.txt b/cli-commands.txt index 70a3515..9a8c780 100644 --- a/cli-commands.txt +++ b/cli-commands.txt @@ -36,9 +36,11 @@ add-video backup -cleanup-ended +cleanup-ended-videos now +cleanup-properties + download-video video-id^* prefer-single-format diff --git a/cli/cleanup_ended.go b/cli/cleanup_ended.go index 8eed66d..057454a 100644 --- a/cli/cleanup_ended.go +++ b/cli/cleanup_ended.go @@ -13,21 +13,20 @@ import ( "net/url" "os" "path/filepath" - "slices" "strings" "time" ) -func CleanupEndedHandler(u *url.URL) error { +func CleanupEndedVideosHandler(u *url.URL) error { now := u.Query().Has("now") - return CleanupEnded(now, nil) + return CleanupEndedVideos(now, nil) } -// CleanupEnded removes downloads for Ended videos that match the following conditions: +// CleanupEndedVideos removes downloads for Ended videos that match the following conditions: // - video download has not been downloaded earlier // - at least 48 hours have passed since the ended date (unless no-delay was set) -// Additionally CleanupEnded will remove video properties (except for title) for ended videos -func CleanupEnded(now bool, rdx kevlar.WriteableRedux) error { +// Additionally CleanupEndedVideos will remove video properties (except for title) for ended videos +func CleanupEndedVideos(now bool, rdx kevlar.WriteableRedux) error { cea := nod.NewProgress("cleaning up ended media...") defer cea.End() @@ -82,11 +81,6 @@ func CleanupEnded(now bool, rdx kevlar.WriteableRedux) error { for _, videoId := range cleanupVideoIds { - // cleanup properties - if err := cleanupProperties(videoId, rdx); err != nil { - return cea.EndWithError(err) - } - if err := removeVideoFile(videoId, absVideosDir, rdx); err != nil { return cea.EndWithError(err) } @@ -179,26 +173,3 @@ func removePosters(videoId string) error { } return nil } - -var preserveVideoPropertiesEnded = []string{ - data.VideoTitleProperty, // required for history - data.VideoOwnerChannelNameProperty, // required for checking empty videos directories - data.VideoEndedDateProperty, // required for cleanup - data.VideoEndedReasonProperty, // required for history - data.VideoFavoriteProperty, // required for cleanup - data.VideoDownloadCompletedProperty, // required for cleanup - data.VideoDownloadCleanedUpProperty, // required for cleanup -} - -func cleanupProperties(videoId string, rdx kevlar.WriteableRedux) error { - for _, vp := range data.VideoProperties() { - if slices.Contains(preserveVideoPropertiesEnded, vp) { - continue - } - - if err := rdx.CutKeys(vp, videoId); err != nil { - return err - } - } - return nil -} diff --git a/cli/cleanup_properties.go b/cli/cleanup_properties.go new file mode 100644 index 0000000..4e848c8 --- /dev/null +++ b/cli/cleanup_properties.go @@ -0,0 +1,58 @@ +package cli + +import ( + "github.com/boggydigital/kevlar" + "github.com/boggydigital/nod" + "github.com/boggydigital/yet/data" + "golang.org/x/exp/slices" + "net/url" +) + +var preserveVideoProperties = []string{ + data.VideoTitleProperty, // required for history + data.VideoOwnerChannelNameProperty, // required for cleanup (checking empty videos directories) + data.VideoEndedDateProperty, // required for cleanup + data.VideoEndedReasonProperty, // required for history + data.VideoFavoriteProperty, // required for cleanup + data.VideoDownloadCompletedProperty, // required for cleanup + data.VideoDownloadCleanedUpProperty, // required for cleanup +} + +func CleanupPropertiesHandler(u *url.URL) error { + return CleanupProperties(nil) +} + +func CleanupProperties(rdx kevlar.WriteableRedux) error { + + cpa := nod.NewProgress("cleaning up video properties...") + defer cpa.End() + + var err error + rdx, err = validateWritableRedux(rdx, data.VideoProperties()...) + if err != nil { + return cpa.EndWithError(err) + } + + endedVideos := rdx.Keys(data.VideoEndedDateProperty) + + cpa.TotalInt(len(endedVideos)) + + for _, videoId := range endedVideos { + + for _, vp := range data.VideoProperties() { + if slices.Contains(preserveVideoProperties, vp) { + continue + } + + if err := rdx.CutKeys(vp, videoId); err != nil { + return cpa.EndWithError(err) + } + } + + cpa.Increment() + } + + cpa.EndWithResult("done") + + return nil +} diff --git a/cli/sync.go b/cli/sync.go index 66ca824..ab32aac 100644 --- a/cli/sync.go +++ b/cli/sync.go @@ -53,7 +53,10 @@ func Sync(rdx kevlar.WriteableRedux, opt *VideoOptions) error { return sa.EndWithError(err) } - if err := CleanupEnded(false, rdx); err != nil { + if err := CleanupProperties(rdx); err != nil { + return sa.EndWithError(err) + } + if err := CleanupEndedVideos(false, rdx); err != nil { return sa.EndWithError(err) } diff --git a/main.go b/main.go index 5d71aa7..22e6dbc 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,8 @@ func main() { "add-playlist": cli.AddPlaylistHandler, "add-video": cli.AddVideoHandler, "backup": cli.BackupHandler, - "cleanup-ended": cli.CleanupEndedHandler, + "cleanup-ended-videos": cli.CleanupEndedVideosHandler, + "cleanup-properties": cli.CleanupPropertiesHandler, "download-video": cli.DownloadVideoHandler, "get-captions": cli.GetCaptionsHandler, "get-channels-metadata": cli.GetChannelsMetadataHandler, diff --git a/rest/view_models/list_view_model.go b/rest/view_models/list_view_model.go index e17958a..31ecefa 100644 --- a/rest/view_models/list_view_model.go +++ b/rest/view_models/list_view_model.go @@ -105,6 +105,7 @@ func GetListViewModel(rdx kevlar.ReadableRedux) (*ListViewModel, error) { } func getVideosProgress(rdx kevlar.ReadableRedux) ([]string, error) { + vpKeys := rdx.Keys(data.VideoProgressProperty) cvs := make([]string, 0) var err error