From 1878d9921c67f21d5b87e67bd9d36725083d8a98 Mon Sep 17 00:00:00 2001 From: Bogdan Brinza Date: Mon, 13 May 2024 21:35:20 -0700 Subject: [PATCH] Sharing redux in sync cmds --- cli/add_playlists.go | 34 ++++++++++++++++++------------ cli/cleanup_ended.go | 28 ++++++++++++------------ cli/download.go | 21 ++++++++++-------- cli/get_playlist_metadata.go | 21 ++++++++++-------- cli/queue_playlists_new_videos.go | 20 ++++++++++-------- cli/sync.go | 24 ++++++++++++++++----- cli/update_playlists_metadata.go | 22 ++++++++++--------- cli/update_playlists_new_videos.go | 21 ++++++++++-------- 8 files changed, 113 insertions(+), 78 deletions(-) diff --git a/cli/add_playlists.go b/cli/add_playlists.go index 473a07b..4ae2737 100644 --- a/cli/add_playlists.go +++ b/cli/add_playlists.go @@ -20,26 +20,32 @@ func AddPlaylistsHandler(u *url.URL) error { singleFormat := strings.Split(q.Get("single-format"), ",") allVideos := q.Has("all-videos") - return AddPlaylists(allVideos, map[string][]string{ - data.PlaylistWatchlistProperty: watchlist, - data.PlaylistDownloadQueueProperty: downloadQueue, - data.PlaylistSingleFormatDownloadProperty: singleFormat, - }) + return AddPlaylists( + nil, + allVideos, + map[string][]string{ + data.PlaylistWatchlistProperty: watchlist, + data.PlaylistDownloadQueueProperty: downloadQueue, + data.PlaylistSingleFormatDownloadProperty: singleFormat, + }) } -func AddPlaylists(allVideos bool, propertyValues map[string][]string) error { +func AddPlaylists(rdx kvas.WriteableRedux, allVideos bool, propertyValues map[string][]string) error { apa := nod.NewProgress("adding playlists...") defer apa.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return apa.EndWithError(err) - } + if rdx == nil { + + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return apa.EndWithError(err) + } - rdx, err := kvas.NewReduxWriter(metadataDir, maps.Keys(propertyValues)...) - if err != nil { - return apa.EndWithError(err) + rdx, err = kvas.NewReduxWriter(metadataDir, maps.Keys(propertyValues)...) + if err != nil { + return apa.EndWithError(err) + } } apa.TotalInt(len(propertyValues)) @@ -61,7 +67,7 @@ func AddPlaylists(allVideos bool, propertyValues map[string][]string) error { } if len(uniquePlaylists) > 0 { - if err := GetPlaylistMetadata(allVideos, false, maps.Keys(uniquePlaylists)...); err != nil { + if err := GetPlaylistMetadata(rdx, allVideos, false, maps.Keys(uniquePlaylists)...); err != nil { return apa.EndWithError(err) } } diff --git a/cli/cleanup_ended.go b/cli/cleanup_ended.go index 0421d7a..72f2624 100644 --- a/cli/cleanup_ended.go +++ b/cli/cleanup_ended.go @@ -16,30 +16,32 @@ import ( ) func CleanupEndedHandler(u *url.URL) error { - return CleanupEnded() + return CleanupEnded(nil) } -func CleanupEnded() error { +func CleanupEnded(rdx kvas.ReadableRedux) error { cea := nod.NewProgress("cleaning up ended media...") defer cea.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return cea.EndWithError(err) - } - absVideosDir, err := pasu.GetAbsDir(paths.Videos) if err != nil { return cea.EndWithError(err) } - rdx, err := kvas.NewReduxReader(metadataDir, - data.VideoEndedProperty, - data.VideoTitleProperty, - data.VideoOwnerChannelNameProperty) - if err != nil { - return cea.EndWithError(err) + if rdx == nil { + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return cea.EndWithError(err) + } + + rdx, err = kvas.NewReduxReader(metadataDir, + data.VideoEndedProperty, + data.VideoTitleProperty, + data.VideoOwnerChannelNameProperty) + if err != nil { + return cea.EndWithError(err) + } } videoIds := rdx.Keys(data.VideoEndedProperty) diff --git a/cli/download.go b/cli/download.go index 311d22d..f82a8df 100644 --- a/cli/download.go +++ b/cli/download.go @@ -21,22 +21,25 @@ func DownloadHandler(u *url.URL) error { queue := u.Query().Has("queue") force := u.Query().Has("force") singleFormat := u.Query().Has("single-format") - return Download(ids, queue, force, singleFormat) + return Download(nil, queue, force, singleFormat, ids...) } -func Download(ids []string, queue, force, singleFormat bool) error { +func Download(rdx kvas.WriteableRedux, queue, force, singleFormat bool, ids ...string) error { da := nod.NewProgress("downloading videos...") defer da.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return da.EndWithError(err) - } + if rdx == nil { - rdx, err := kvas.NewReduxWriter(metadataDir, data.AllProperties()...) - if err != nil { - return da.EndWithError(err) + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return da.EndWithError(err) + } + + rdx, err = kvas.NewReduxWriter(metadataDir, data.AllProperties()...) + if err != nil { + return da.EndWithError(err) + } } if queue { diff --git a/cli/get_playlist_metadata.go b/cli/get_playlist_metadata.go index 500a1eb..8712bc1 100644 --- a/cli/get_playlist_metadata.go +++ b/cli/get_playlist_metadata.go @@ -16,10 +16,10 @@ func GetPlaylistMetadataHandler(u *url.URL) error { ids := strings.Split(q.Get("id"), ",") allVideos := q.Has("all-videos") force := q.Has("force") - return GetPlaylistMetadata(allVideos, force, ids...) + return GetPlaylistMetadata(nil, allVideos, force, ids...) } -func GetPlaylistMetadata(allVideos, force bool, ids ...string) error { +func GetPlaylistMetadata(rdx kvas.WriteableRedux, allVideos, force bool, ids ...string) error { gpma := nod.NewProgress("getting playlist metadata...") defer gpma.End() @@ -30,14 +30,17 @@ func GetPlaylistMetadata(allVideos, force bool, ids ...string) error { gpma.TotalInt(len(playlistIds)) - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return gpma.EndWithError(err) - } + if rdx == nil { - rdx, err := kvas.NewReduxWriter(metadataDir, data.AllProperties()...) - if err != nil { - return gpma.EndWithError(err) + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return gpma.EndWithError(err) + } + + rdx, err = kvas.NewReduxWriter(metadataDir, data.AllProperties()...) + if err != nil { + return gpma.EndWithError(err) + } } for _, playlistId := range playlistIds { diff --git a/cli/queue_playlists_new_videos.go b/cli/queue_playlists_new_videos.go index 6639ef4..e806c93 100644 --- a/cli/queue_playlists_new_videos.go +++ b/cli/queue_playlists_new_videos.go @@ -10,22 +10,24 @@ import ( ) func QueuePlaylistsNewVideosHandler(u *url.URL) error { - return QueuePlaylistsNewVideos() + return QueuePlaylistsNewVideos(nil) } -func QueuePlaylistsNewVideos() error { +func QueuePlaylistsNewVideos(rdx kvas.WriteableRedux) error { qpnva := nod.NewProgress("queueing playlists new videos...") defer qpnva.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return qpnva.EndWithError(err) - } + if rdx == nil { + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return qpnva.EndWithError(err) + } - rdx, err := kvas.NewReduxWriter(metadataDir, data.AllProperties()...) - if err != nil { - return qpnva.EndWithError(err) + rdx, err = kvas.NewReduxWriter(metadataDir, data.AllProperties()...) + if err != nil { + return qpnva.EndWithError(err) + } } notNewIndicatorProperties := []string{ diff --git a/cli/sync.go b/cli/sync.go index 81d4a50..18e27ba 100644 --- a/cli/sync.go +++ b/cli/sync.go @@ -1,7 +1,11 @@ package cli import ( + "github.com/boggydigital/kvas" "github.com/boggydigital/nod" + "github.com/boggydigital/pasu" + "github.com/boggydigital/yet/data" + "github.com/boggydigital/yet/paths" "net/url" ) @@ -16,23 +20,33 @@ func Sync(force, singleFormat bool) error { sa := nod.Begin("syncing playlists subscriptions...") defer sa.End() - if err := UpdatePlaylistsMetadata(); err != nil { + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { return sa.EndWithError(err) } - if err := UpdatePlaylistsNewVideos(); err != nil { + rdx, err := kvas.NewReduxWriter(metadataDir, data.AllProperties()...) + if err != nil { return sa.EndWithError(err) } - if err := QueuePlaylistsNewVideos(); err != nil { + if err := UpdatePlaylistsMetadata(rdx); err != nil { return sa.EndWithError(err) } - if err := Download(nil, true, force, singleFormat); err != nil { + if err := UpdatePlaylistsNewVideos(rdx); err != nil { return sa.EndWithError(err) } - if err := CleanupEnded(); err != nil { + if err := QueuePlaylistsNewVideos(rdx); err != nil { + return sa.EndWithError(err) + } + + if err := Download(rdx, true, force, singleFormat); err != nil { + return sa.EndWithError(err) + } + + if err := CleanupEnded(rdx); err != nil { return sa.EndWithError(err) } diff --git a/cli/update_playlists_metadata.go b/cli/update_playlists_metadata.go index 415169a..e61d944 100644 --- a/cli/update_playlists_metadata.go +++ b/cli/update_playlists_metadata.go @@ -10,25 +10,27 @@ import ( ) func UpdatePlaylistsMetadataHandler(u *url.URL) error { - return UpdatePlaylistsMetadata() + return UpdatePlaylistsMetadata(nil) } -func UpdatePlaylistsMetadata() error { +func UpdatePlaylistsMetadata(rdx kvas.WriteableRedux) error { upma := nod.NewProgress("updating all playlists metadata...") defer upma.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return upma.EndWithError(err) - } + if rdx == nil { + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return upma.EndWithError(err) + } - rdx, err := kvas.NewReduxWriter(metadataDir, data.PlaylistWatchlistProperty) - if err != nil { - return upma.EndWithError(err) + rdx, err = kvas.NewReduxWriter(metadataDir, data.PlaylistWatchlistProperty) + if err != nil { + return upma.EndWithError(err) + } } - if err := GetPlaylistMetadata(false, true, rdx.Keys(data.PlaylistWatchlistProperty)...); err != nil { + if err := GetPlaylistMetadata(rdx, false, true, rdx.Keys(data.PlaylistWatchlistProperty)...); err != nil { return upma.EndWithError(err) } diff --git a/cli/update_playlists_new_videos.go b/cli/update_playlists_new_videos.go index 0f653fb..20f422d 100644 --- a/cli/update_playlists_new_videos.go +++ b/cli/update_playlists_new_videos.go @@ -10,22 +10,25 @@ import ( ) func UpdatePlaylistsNewVideosHandler(u *url.URL) error { - return UpdatePlaylistsNewVideos() + return UpdatePlaylistsNewVideos(nil) } -func UpdatePlaylistsNewVideos() error { +func UpdatePlaylistsNewVideos(rdx kvas.WriteableRedux) error { upmnva := nod.NewProgress("updating playlists new videos (new since last ended)...") defer upmnva.End() - metadataDir, err := pasu.GetAbsDir(paths.Metadata) - if err != nil { - return upmnva.EndWithError(err) - } + if rdx == nil { - rdx, err := kvas.NewReduxWriter(metadataDir, data.AllProperties()...) - if err != nil { - return upmnva.EndWithError(err) + metadataDir, err := pasu.GetAbsDir(paths.Metadata) + if err != nil { + return upmnva.EndWithError(err) + } + + rdx, err = kvas.NewReduxWriter(metadataDir, data.AllProperties()...) + if err != nil { + return upmnva.EndWithError(err) + } } playlistIds := rdx.Keys(data.PlaylistWatchlistProperty)