Add command line M3U exporter. Closes #1914

This commit is contained in:
Deluan 2022-12-21 14:37:08 -05:00
parent 5d8318f7b3
commit 28389fb05e
12 changed files with 188 additions and 63 deletions

View file

@ -22,7 +22,7 @@ import (
type Playlists interface {
ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error)
Update(ctx context.Context, playlistId string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error
Update(ctx context.Context, playlistID string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error
}
type playlists struct {
@ -33,11 +33,6 @@ func NewPlaylists(ds model.DataStore) Playlists {
return &playlists{ds: ds}
}
func IsPlaylist(filePath string) bool {
extension := strings.ToLower(filepath.Ext(filePath))
return extension == ".m3u" || extension == ".m3u8" || extension == ".nsp"
}
func (s *playlists) ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error) {
pls, err := s.parsePlaylist(ctx, fname, dir)
if err != nil {
@ -194,7 +189,7 @@ func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
return 0, nil, nil
}
func (s *playlists) Update(ctx context.Context, playlistId string,
func (s *playlists) Update(ctx context.Context, playlistID string,
name *string, comment *string, public *bool,
idsToAdd []string, idxToRemove []int) error {
needsInfoUpdate := name != nil || comment != nil || public != nil
@ -205,18 +200,18 @@ func (s *playlists) Update(ctx context.Context, playlistId string,
var err error
repo := tx.Playlist(ctx)
if needsTrackRefresh {
pls, err = repo.GetWithTracks(playlistId)
pls, err = repo.GetWithTracks(playlistID)
pls.RemoveTracks(idxToRemove)
pls.AddTracks(idsToAdd)
} else {
if len(idsToAdd) > 0 {
_, err = repo.Tracks(playlistId).Add(idsToAdd)
_, err = repo.Tracks(playlistID).Add(idsToAdd)
if err != nil {
return err
}
}
if needsInfoUpdate {
pls, err = repo.Get(playlistId)
pls, err = repo.Get(playlistID)
}
}
if err != nil {
@ -237,7 +232,7 @@ func (s *playlists) Update(ctx context.Context, playlistId string,
}
// Special case: The playlist is now empty
if len(idxToRemove) > 0 && len(pls.Tracks) == 0 {
if err = repo.Tracks(playlistId).DeleteAll(); err != nil {
if err = repo.Tracks(playlistID).DeleteAll(); err != nil {
return err
}
}