Skip to content

Commit 8ae65a7

Browse files
committed
added helper function
1 parent 093f3ba commit 8ae65a7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/playlist.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,26 @@ func checkTracks(cfg Config, tracks []Track) []Track { // Returns updated slice
5757
}
5858

5959
func createPlaylist(cfg Config, tracks []Track) error {
60-
6160
if cfg.System == "" {
6261
return fmt.Errorf("could not get music system")
6362
}
6463

6564
description := "Created by Explo using recommendations from ListenBrainz" // Description to add to playlists
6665

66+
67+
// Helper func to sleep after refreshing library
68+
refreshLibrary := func() {
69+
log.Printf("[%s] Refreshing library...", cfg.System)
70+
time.Sleep(time.Duration(cfg.Sleep) * time.Minute)
71+
}
72+
6773
switch cfg.System {
6874
case "subsonic":
6975

7076
if err := subsonicScan(cfg); err != nil {
7177
return fmt.Errorf("failed to schedule a library scan: %s", err.Error())
7278
}
73-
log.Printf("sleeping for %d minutes, to allow scan to complete..", cfg.Sleep)
74-
time.Sleep(time.Duration(cfg.Sleep) * time.Minute)
79+
refreshLibrary()
7580

7681
ID, err := subsonicPlaylist(cfg, tracks)
7782
if err != nil {
@@ -88,8 +93,7 @@ func createPlaylist(cfg Config, tracks []Track) error {
8893
if err := refreshJfLibrary(cfg); err != nil {
8994
return fmt.Errorf("failed to refresh library: %s", err.Error())
9095
}
91-
log.Printf("sleeping for %d minutes, to allow scan to complete..", cfg.Sleep)
92-
time.Sleep(time.Duration(cfg.Sleep) * time.Minute)
96+
refreshLibrary()
9397

9498
ID, err := createJfPlaylist(cfg, tracks)
9599
if err != nil {
@@ -112,8 +116,8 @@ func createPlaylist(cfg Config, tracks []Track) error {
112116
if err := refreshPlexLibrary(cfg); err != nil {
113117
return fmt.Errorf("createPlaylist(): %s", err.Error())
114118
}
115-
log.Printf("sleeping for %d minutes, to allow scan to complete..", cfg.Sleep)
116-
time.Sleep(time.Duration(cfg.Sleep) * time.Minute)
119+
refreshLibrary()
120+
117121
serverID, err := getPlexServer(cfg)
118122
if err != nil {
119123
return fmt.Errorf("createPlaylist(): %s", err.Error())
@@ -130,7 +134,7 @@ func createPlaylist(cfg Config, tracks []Track) error {
130134

131135
return nil
132136
}
133-
return fmt.Errorf("something very strange happened")
137+
return fmt.Errorf("unsupported system: %s", cfg.System)
134138
}
135139

136140
func handlePlaylistDeletion(cfg Config) error {

0 commit comments

Comments
 (0)