Skip to content

Commit

Permalink
Sharing redux in sync cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
boggydigital committed May 14, 2024
1 parent 327bf64 commit 1878d99
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 78 deletions.
34 changes: 20 additions & 14 deletions cli/add_playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
}
Expand Down
28 changes: 15 additions & 13 deletions cli/cleanup_ended.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions cli/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 12 additions & 9 deletions cli/get_playlist_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions cli/queue_playlists_new_videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
24 changes: 19 additions & 5 deletions cli/sync.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}

Expand Down
22 changes: 12 additions & 10 deletions cli/update_playlists_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
21 changes: 12 additions & 9 deletions cli/update_playlists_new_videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1878d99

Please sign in to comment.