Skip to content

Commit f557b71

Browse files
committed
Plex implementation
1 parent 7ee3aa6 commit f557b71

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,15 @@ func (cfg *Config) systemSetup() { // Verifies variables and does setup
190190
if cfg.PlaylistDir == "" {
191191
log.Fatal("PLAYLIST_DIR variable not set, exiting")
192192
}
193+
case "plex":
194+
if (cfg.Creds.User == "" && cfg.Creds.Password == "") {
195+
log.Fatal("USER and/or PASSWORD variable not set, exiting")
196+
}
197+
cfg.Creds.getPlexAuth()
198+
cfg.Creds.PlexHeader() // Adds client headers
199+
cfg.getPlexLibrary()
193200
default:
194-
log.Fatalf("system: %s not known, please use a supported system (jellyfin, mpd or subsonic)", cfg.System)
201+
log.Fatalf("system: %s not known, please use a supported system (jellyfin, mpd, subsonic or plex)", cfg.System)
195202
}
196203
}
197204

src/playlist.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ func checkTracks(cfg Config, tracks []Track) []Track { // Returns updated slice
4040
switch cfg.System {
4141
case "subsonic":
4242
ID, _ = searchTrack(cfg, track)
43+
4344
case "jellyfin":
4445
ID, _ = getJfSong(cfg, track)
46+
47+
case "plex":
48+
ID, _ = searchPlexSong(cfg, track)
4549
}
4650
if ID != "" {
4751
tracks[i].Present = true
@@ -83,12 +87,29 @@ func createPlaylist(cfg Config, tracks []Track) error {
8387
return fmt.Errorf("failed to create playlist: %s", err.Error())
8488
}
8589
return nil
90+
8691
case "mpd":
8792

8893
if err := createM3U(cfg, cfg.PlaylistName, tracks); err != nil {
8994
return fmt.Errorf("failed to create M3U playlist: %s", err.Error())
9095
}
9196
return nil
97+
98+
case "plex":
99+
if err := refreshPlexLibrary(cfg); err != nil {
100+
return fmt.Errorf("createPlaylist(): %s", err.Error())
101+
}
102+
log.Printf("sleeping for %d minutes, to allow scan to complete..", cfg.Sleep)
103+
time.Sleep(time.Duration(cfg.Sleep) * time.Minute)
104+
ID, err := getPlexServer(cfg)
105+
if err != nil {
106+
return fmt.Errorf("createPlaylist(): %s", err.Error())
107+
}
108+
key, err := createPlexPlaylist(cfg, ID)
109+
if err != nil {
110+
return fmt.Errorf("createPlaylist(): %s", err.Error())
111+
}
112+
addToPlexPlaylist(cfg, key, ID, tracks)
92113
}
93114
return fmt.Errorf("something very strange happened")
94115
}
@@ -120,6 +141,14 @@ func handlePlaylistDeletion(cfg Config) error {
120141
if err := os.Remove(cfg.PlaylistDir+cfg.PlaylistName+".m3u"); err != nil {
121142
return err
122143
}
144+
case "plex":
145+
key, err := searchPlexPlaylist(cfg)
146+
if err != nil {
147+
return err
148+
}
149+
if err := deletePlexPlaylist(cfg, key); err != nil {
150+
return err
151+
}
123152
}
124153
return nil
125154
}

src/plex.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"explo/debug"
67
"fmt"
78
"log"
89
"net/url"
@@ -221,21 +222,21 @@ func searchPlexPlaylist(cfg Config) (string, error) {
221222
return "", fmt.Errorf("searchPlexPlaylist(): failed to parse response: %s", err.Error())
222223
}
223224

224-
key, err := getPlexPlaylist(playlists, cfg.PlaylistName)
225-
if err != nil {
226-
return "", fmt.Errorf("getPlexPlaylist(): %s", err.Error())
225+
key := getPlexPlaylist(playlists, cfg.PlaylistName)
226+
if key == "" {
227+
debug.Debug("no playlist found")
227228
}
228229
return key, nil
229230
}
230231

231-
func getPlexPlaylist(playlists PlexPlaylist, playlistName string) (string, error) {
232+
func getPlexPlaylist(playlists PlexPlaylist, playlistName string) string {
232233

233234
for _, playlist := range playlists.MediaContainer.Metadata {
234235
if playlist.Title == playlistName {
235-
return playlist.Key, nil
236+
return playlist.Key
236237
}
237238
}
238-
return "", fmt.Errorf("failed to find playlist")
239+
return ""
239240
}
240241

241242
func getPlexServer(cfg Config) (string, error) {
@@ -255,8 +256,8 @@ func getPlexServer(cfg Config) (string, error) {
255256
return server.MediaContainer.MachineIdentifier, nil
256257
}
257258

258-
func createPlexPlaylist(cfg Config, libraryKey, machineID string) (string, error) {
259-
params := fmt.Sprintf("/playlists?title=%s&type=audio&smart=0&uri=server://%s/com.plexapp.plugins.library/%s&X-Plex-Token=%s", cfg.PlaylistName, machineID, libraryKey, cfg.Creds.APIKey)
259+
func createPlexPlaylist(cfg Config, machineID string) (string, error) {
260+
params := fmt.Sprintf("/playlists?title=%s&type=audio&smart=0&uri=server://%s/com.plexapp.plugins.library/%s&X-Plex-Token=%s", cfg.PlaylistName, machineID, cfg.Plex.LibraryID, cfg.Creds.APIKey)
260261

261262
body, err := makeRequest("POST", cfg.URL+params, nil, cfg.Creds.Headers)
262263
if err != nil {
@@ -273,16 +274,15 @@ func createPlexPlaylist(cfg Config, libraryKey, machineID string) (string, error
273274
return playlist.MediaContainer.Metadata[0].Key, nil
274275
}
275276

276-
func addToPlexPlaylist(cfg Config, playlistKey, machineID string, tracks []Track) error {
277+
func addToPlexPlaylist(cfg Config, playlistKey, machineID string, tracks []Track) {
277278
for _, track := range tracks {
278279
params := fmt.Sprintf("/playlists/%s?uri=server://%s/com.plexapp.plugins.library/%s&X-Plex-Token=%s", playlistKey, machineID, track.ID, cfg.Creds.APIKey)
279280

280281
_, err := makeRequest("PUT", cfg.URL+params, nil, cfg.Creds.Headers)
281282
if err != nil {
282-
return fmt.Errorf("addToPlexPlaylist(): failed to add %s to playlist: %s", track.Title, err.Error())
283+
log.Printf("addToPlexPlaylist(): failed to add %s to playlist: %s", track.Title, err.Error())
283284
}
284285
}
285-
return nil
286286
}
287287

288288
func deletePlexPlaylist(cfg Config, playlistKey string) error {

0 commit comments

Comments
 (0)