@@ -52,6 +52,7 @@ type PlexSearch struct {
5252 Score float64 `json:"score"`
5353 Metadata struct {
5454 LibrarySectionTitle string `json:"librarySectionTitle"`
55+ RatingKey string `json:"ratingKey"`
5556 Key string `json:"key"`
5657 Type string `json:"type"`
5758 Title string `json:"title"` // Track
@@ -133,12 +134,15 @@ func (c *Plex) AddHeader() error {
133134
134135 if c .Cfg .Creds .APIKey != "" {
135136 c .Cfg .Creds .Headers ["X-Plex-Token" ] = c .Cfg .Creds .APIKey
137+ if err := c .getServer (); err != nil {
138+ return err
139+ }
136140 return nil
137141 }
138142 return fmt .Errorf ("couldn't get API key" )
139143}
140144
141- func (c * Plex ) GetAuth () error { // Get user token and server ID from plex
145+ func (c * Plex ) GetAuth () error { // Get user token from plex
142146 payload := LoginPayload {
143147 User : LoginUser {
144148 Login : c .Cfg .Creds .User ,
@@ -165,10 +169,6 @@ func (c *Plex) GetAuth() error { // Get user token and server ID from plex
165169
166170 c .Cfg .Creds .APIKey = auth .User .AuthToken
167171
168- err = c .getServer ()
169- if err != nil {
170- return fmt .Errorf ("%s" , err .Error ())
171- }
172172 return nil
173173}
174174
@@ -345,7 +345,7 @@ func getPlexSong(track *models.Track, searchResults PlexSearch) (string, error)
345345 artistMatch := strings .Contains (strings .ToLower (md .OriginalTitle ), loweredArtist ) || strings .Contains (strings .ToLower (md .GrandparentTitle ), loweredArtist )
346346
347347 if titleMatch && (albumMatch || artistMatch ) {
348- return md .Key , nil
348+ return md .RatingKey , nil
349349 }
350350
351351 if track .File == "" || len (md .Media ) == 0 || len (md .Media [0 ].Part ) == 0 {
@@ -357,7 +357,7 @@ func getPlexSong(track *models.Track, searchResults PlexSearch) (string, error)
357357 durationMatch := util .Abs (media .Duration - track .Duration ) < 10000 // duration within 10s
358358
359359 if durationMatch && pathMatch {
360- return md .Key , nil
360+ return md .RatingKey , nil
361361 }
362362 }
363363
@@ -366,14 +366,15 @@ func getPlexSong(track *models.Track, searchResults PlexSearch) (string, error)
366366}
367367
368368func (c * Plex ) addtoPlaylist (tracks []* models.Track ) {
369-
369+ var IDs [] string
370370 for _ , track := range tracks {
371371 if track .ID != "" {
372- params := fmt .Sprintf ("/playlists/%s/items?uri=server://%s/com.plexapp.plugins.library%s" , c .Cfg .PlaylistID , c .machineID , track .ID )
373-
374- if _ , err := c .HttpClient .MakeRequest ("PUT" , c .Cfg .URL + params , nil , c .Cfg .Creds .Headers ); err != nil {
375- log .Printf ("failed to add %s to playlist: %s" , track .Title , err .Error ())
376- }
372+ IDs = append (IDs , track .ID )
377373 }
378374 }
375+ params := fmt .Sprintf ("/playlists/%s/items?uri=server://%s/com.plexapp.plugins.library/library/metadata/%s/" , c .Cfg .PlaylistID , c .machineID , strings .Join (IDs ,"," ))
376+
377+ if _ , err := c .HttpClient .MakeRequest ("PUT" , c .Cfg .URL + params , nil , c .Cfg .Creds .Headers ); err != nil {
378+ log .Printf ("failed to add tracks to playlist: %s" , err .Error ())
379+ }
379380}
0 commit comments