Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2020 00:21
What would you like to do?
Import playlists into Plex using plexapi
#!/usr/bin/env python3
Import playlists into a local Plex server. I used this to import my Google Play
Music playlists when it was being sunset. This consumes the JSON output by my at
The script will die if it can't find a track, only creates a playlist if it can
find all the tracks, and will skip playlists that already exist. This allows the
script to be used in an iterative process by tweaking the track title contents
in the JSON file as necessary and rerunning it. If you set the "Removed" field
to 1 in the JSON, this will skip the track and continue.
This is a pure hack job but it only had to work once and it saved me hours
of time recreating dozens of ordered playlists.
I'm posting it as a gist primarily as an example for how to search for tracks
(and artists) in using plexapi and create playlists.
You will need to populate the global variables to values for your Plex server
and library.
import json
import sys
import plexapi
from plexapi.server import PlexServer
# URL and token to your Plex server. See also:
baseurl = "http://localhost:32400"
token = "TOKEN"
# Your Plex library name
library_name = "My Music"
# JSON playlist file
playlist_json_file = "playlists.json"
def main():
# connect to plex
plex = PlexServer(baseurl, token)
# load all playlist names from Plex
plex_playlists = []
for playlist in plex.playlists():
plex_library = plex.library.section(library_name)
# load playlists from JSON
with open(playlist_json_file) as inputfile:
playlists = json.load(inputfile)
for playlist in playlists:
if playlist["Title"] in plex_playlists:
print(f"Skipping {playlist['Title']}")
print(f"Importing {playlist['Title']}")
plex_items = []
for track in playlist["Tracks"]:
if track["Removed"]:
print(f"Skipping {track['Title']}")
plex_track = None
print(f"Searching for {track['Title']}")
# Find the plex items matching on title and artist
results =["Title"], libtype="track")
if len(results) == 1:
plex_track = results[0]
for result in results:
if result.artist().title == track["Artist"]:
plex_track = result
if not plex_track:
# ok try to find it by artist then title and normalize the
# track title a bit to improve matching
print("Falling back to artist search")
normalized_track_names = [
track["Title"].lower().replace("’", "'"),
track["Title"].lower().replace("'", "’"),
track["Title"].lower().replace("“", '"'),
track["Title"].lower().replace('"', "“"),
track["Title"].lower().replace("&", "and"),
track["Title"].lower().replace("and", "&"),
results =["Artist"], libtype="artist")
for result in results:
for search_track in result.tracks():
if search_track.title.lower() in normalized_track_names:
plex_track = search_track
if plex_track:
if plex_track:
f"Unable to find track for {track['Title']} by {track['Artist']}"
plexapi.playlist.Playlist.create(plex, playlist["Title"], plex_items)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment