How can I merge playlists via API?

NOTE: Since Tidal integration with Plex is ending, this script will cease to function at some point on or before the end of October 2024.

I’m trying to create playlists in Plex by reading a list of tracks (ARTIST - SONGNAME) from a file, and finding the local and Tidal versions (if no local version exists) to add to the playlist.

Solved! See below.

This was solved by a kind person on Reddit who pointed out that the format of the add_to_playlist_url was not correct (all other code is mine). It seems the solution was using a PUT request to a URL in this format below. So I create the playlist of Local tracks, then pull the Key for that playlist, and then write the Tidal tracks to the playlist like so:

add_to_playlist_url = f"{PLEX_SERVER_URL}/playlists/{EXISTING_PLAYLIST_RATINGKEY}/items?uri=provider%3A%2F%2Ftv.plex.provider.music%2Flibrary%2Fmetadata%2F{tidal_id_str}"

Here’s the final script which I posted on my Github. It may not be the most elegant code ever written, but it works. Since I have a lot of bootlegs in my collection, I exclude tracks and albums with a date format (e.g. YYYY-MM-DD) or with the words “live”, “concert”, or “sbd”. It also prompts which Plex user account to add the playlist to, but I include a modified function that adds it to the admin account by default if you don’t want the ability to select a user.

@elan I figured it out. Maybe you guys would find this useful.

1 Like

Hey, first of all thanks for the script, actually what I was searching for. It took me some time to get to the final steps, and now I am stuck.

“Enter the account to add the playlist to: ACCOUNTNAME
Input filename (or Enter for ‘playlist.txt’): playlist.The Sound of New Wave
Error: ‘playlist.The Sound of New Wave’ not found.”

I am missing something? Does it need writing permissions?

Oh and something else. I used my external ddns address in the script as I was getting a certificate warning for my internal address.

“Caused by SSLError(SSLCertVerificationError(1, “[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: IP address mismatch, certificate is not valid for ‘MY INTERNAL ADDRESS’. (_ssl.c:1000)”)))”

The account you’re adding the playlist to is either your main Plex Server admin account, or a user account that you added.

The input filename is a text file with the list of tracks that you want added to the playlist. It must be in the format like this:

Artist - Track

e.g…

Madonna - Get Into The Groove
Prince - Purple Rain
Duran Duran - Rio

(notice it’s Purple Rain, not “Purple Rain” – don’t use quotes)

For the Plex server address, I think you have to use an internal IP like 192.168.X.XXX or the script might not be able to contact the Plex server. The PC running the script should be on the same internal network as the PC hosting the Plex server (it can be the same PC).

Let me know if you run into any more issues. I will try to help.

Thank you for getting back to me.

I came one step further, however now I am getting this message:

" File “/Users/xxx/Documents/Scripte/plexplaylist/plex-playlist-chatgpt-prompt-user.py”, line 335, in
main()
File “/Users/xxx/Documents/Scripte/plexplaylist/plex-playlist-chatgpt-prompt-user.py”, line 228, in main
existing_playlists = [playlist.title for playlist in plex.playlists()]
^^^^^^^^^^^^^^
AttributeError: ‘Tag’ object has no attribute ‘title’"

Hmmm, I downloaded the script from my github and tested it and it works fine.

What does your ppg_config.py look like? Here’s mine with some details changed for privacy reasons:

PLEX_URL = ‘http://192.168.0.214:32400
PLEX_TOKEN = ‘KjfeRjheiry34782984kJFDK’
SECTION_TITLE = ‘Music’
ADMIN_NAME = ‘armandhammer’
ADMIN_PASS = ‘EKFJOWEKTJLDJF’
FUZZ_AMT = 60
BATCH_SIZE = 20
PLACEHOLDER_ARTIST = ‘LCD Soundsystem’
PLACEHOLDER_TITLE = ‘Dance Yrself Clean’

It looks the same. I managed to use the local address (with http, but I had to change “Secure connections” from required to preferred. I am still getting the same error message.

Here is what I did:
“cd ‘/Users/xxx/Documents/Scripte/plexplaylist/’ && ‘/usr/local/bin/python3’ ‘/Users/xxx/Documents/Scripte/plexplaylist/plex-playlist-chatgpt-prompt-user.py’ && echo Exit status: $? && exit 1
Enter the account to add the playlist to: USERNAME
Input filename (or Enter for ‘playlist.txt’):
Traceback (most recent call last):
File “/Users/xxx/Documents/Scripte/plexplaylist/plex-playlist-chatgpt-prompt-user.py”, line 335, in
main()
File “/Users/xxx/Documents/Scripte/plexplaylist/plex-playlist-chatgpt-prompt-user.py”, line 228, in main
existing_playlists = [playlist.title for playlist in plex.playlists()]
^^^^^^^^^^^^^^
AttributeError: ‘Tag’ object has no attribute ‘title’”

And something else, when I run ‘alternate-main-func-for-noprompt.py’ I get the following error:
cd ‘/Users/xxx/Documents/Scripte/plexplaylist/’ && ‘/usr/local/bin/python3’ ‘/Users/xxx/Documents/Scripte/plexplaylist/alternate-main-func-for-noprompt.py’ && echo Exit status: $? && exit 1
Traceback (most recent call last):
File “/Users/xxx/Documents/Scripte/plexplaylist/alternate-main-func-for-noprompt.py”, line 121, in
main()
File “/Users/xxx/Documents/Scripte/plexplaylist/alternate-main-func-for-noprompt.py”, line 2, in main
plex = initialize_plex_server(PLEX_URL, PLEX_TOKEN)
^^^^^^^^^^^^^^^^^^^^^^
NameError: name ‘initialize_plex_server’ is not defined

Are you copy-pasting the code from alternate-main-func-for-noprompt.py to replace the main function in plex-playlist-chatgpt-prompt-user.py? This would be about from line 215-330 of the script. The alternate-main-func-for-noprompt.py can’t run by itself, it’s just alternate code for the def main(): part of the original script.

I see, no I did not try that.

Actually I would love to get the original script (where I could choose the user) to work. Any idea what could be the problem?

Did you create a file named playlist.txt in the same directory where you’re running the script? It should have a list of songs in the format Artist - Track like I showed in a previous comment.

Yes I did. I tried both creating the file myself or let ChatGPT doing the job. Still, the same result.

If you go to your Plex Server web interface > Settings > Remote Access, do you see the same local IP address and port that you’re trying to use in the script? Also under the Network settings, I have Secure Connections set to Preferred. I don’t think I have changed any other settings.

Can you check this, run it again, and post any errors?

I did all of that and the address is also the same.

I was wondering, do you type something here?: “Input filename (or Enter for ‘playlist.txt’):”

If your playlist text file is named playlist.txt then you can just hit Enter without typing anything. If it’s named something else, like otherplaylist.txt then you need to type otherplaylist.txt and then hit Enter. The playlist must be in the same directory where you’re running the script.

The playlist.txt file isn’t the problem. So far two things are a bit of a problem

  1. Two-Factor Authenfication: If I don’t turn it off I won’t get any further
  2. I can add the list to a managed account, but not my own.

And something else, I got it to work by changing the lines 228-32 to the following:
# existing_playlists = [playlist.title for playlist in plex.playlists()]
# playlist_name = input("Please enter the name for the new playlist: ")

# while playlist_name in existing_playlists:
#     print(f"Playlist '{playlist_name}' already exists. Please choose a different name.")
#     playlist_name = input("Please enter the name for the new playlist: ")

playlist_name = "ChatGPT Playlist 1"

Did you ever figure it out?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.