Skip to content

Commit

Permalink
Extracting property cleanup into a separate cmd that runs on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
boggydigital committed Aug 17, 2024
1 parent c5a661f commit b6e476f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 37 deletions.
4 changes: 3 additions & 1 deletion cli-commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ add-video

backup

cleanup-ended
cleanup-ended-videos
now

cleanup-properties

download-video
video-id^*
prefer-single-format
Expand Down
39 changes: 5 additions & 34 deletions cli/cleanup_ended.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions cli/cleanup_properties.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions rest/view_models/list_view_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b6e476f

Please sign in to comment.