Skip to content

Instantly share code, notes, and snippets.

@jimcampbell100
Created June 11, 2023 18:02
Show Gist options
  • Save jimcampbell100/516b72230810bbded1deda8f3c4d92ed to your computer and use it in GitHub Desktop.
Save jimcampbell100/516b72230810bbded1deda8f3c4d92ed to your computer and use it in GitHub Desktop.
Set Plex Mood to Playlist Name
# import plexapi
import requests
import sys
from plexapi.server import PlexServer
from plexapi.utils import tag_helper
# Next two lines need to be reset to match your server
PLEX_URL = 'http://192.168.0.xxx:32400'
PLEX_TOKEN = 'tokengoeshere'
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
# Add or Remove Tags?
addorremove = input("Do you want to add, remove or refresh tags? [add, remove, refresh] ")
# If Add
if addorremove == "add":
# New or Existing Playlist?
# If this is a new playlist (an m3u file) that is not currently included in Plex,
# it will be uploaded into Plex.
neworexisting = input("Is the playlist new (to be imported) (yes or no)?")
# If New
if neworexisting == "yes":
# Import m3u
music_libraries = {section.key: section for section in plex.library.sections() if
section.type == 'artist'}
if len(music_libraries) == 0:
exit(1)
elif len(music_libraries) == 1:
music_library = list(music_libraries.values())[0]
else:
for key, library in music_libraries.items():
print('\t[{}]: {}'.format(key, library.title))
choice = input('Select the library for the playlist: ')
music_library = music_libraries[choice]
m3ufile = input("Enter the full path to and filename of the playlist: ")
# Create the request string
url = PLEX_URL + "/playlists/upload?sectionID=" + \
music_library + "&path=" + m3ufile + \
"&X-Plex-Token=" + PLEX_TOKEN
# Make the request
try:
response = requests.post(url)
except requests.exceptions.RequestException as err:
sys.exit(err)
if response.status_code == requests.codes.ok:
print("Successfully connected to server " + str(response.status_code))
else:
print("ERROR: HTTP server response code " + str(response.status_code))
sys.exit("Bad HTTP response from Plex server")
# If Either New or Existing
# Get the playlist name
for playlist in plex.playlists():
print(playlist)
playlistName = input("Enter the name of the playlist: ")
playlist = plex.playlist(playlistName)
# Go through the list of items in the playlist and add the playlist name to mood
for track in playlist.items():
existing_moods = [mood.tag for mood in track.moods]
data = tag_helper("mood", existing_moods + [playlistName])
track.edit(**data)
track.reload()
# If Remove
elif addorremove == "remove":
# Get the playlist name
for playlist in plex.playlists():
print(playlist)
playlistName = input("Enter the name of the playlist: ")
playlist = plex.playlist(playlistName)
# Go through the list of items in the playlist and remove the playlist name from mood, if it is there
for track in playlist.items():
existing_moods = [mood.tag for mood in track.moods]
data = tag_helper("mood", [playlistName], True, True)
track.edit(**data)
track.reload()
# If refresh
elif addorremove == "refresh":
# This option exists for a situation where you manage the m3u in a tool outside Plex.
# When the m3u file changes, we remove that tags from all the tracks on the xisting playlist,
# re-import the playlist to get the updates (added or removed tracks), and then set the mood
# on the files associated with teh new playlist.
# We will re-import the m3u, but we first need to remove the tags associated with the current playlist
# Get the playlist name
for playlist in plex.playlists():
print(playlist)
playlistName = input("Enter the name of the playlist: ")
playlist = plex.playlist(playlistName)
# Go through the list of items in the playlist and remove the playlist name from mood, if it is there
for track in playlist.items():
existing_moods = [mood.tag for mood in track.moods]
data = tag_helper("mood", [playlistName], True, True)
track.edit(**data)
track.reload()
# Import m3u
music_libraries = {section.key: section for section in plex.library.sections() if
section.type == 'artist'}
if len(music_libraries) == 0:
exit(1)
elif len(music_libraries) == 1:
music_library = list(music_libraries.values())[0]
else:
for key, library in music_libraries.items():
print('\t[{}]: {}'.format(key, library.title))
music_library = input('Select the library number for the playlist: ')
m3ufile = input("Enter the full path to and filename of the playlist: ")
# Create the request string
url = PLEX_URL + "/playlists/upload?sectionID=" + \
music_library + "&path=" + m3ufile + \
"&X-Plex-Token=" + PLEX_TOKEN
# Make the request
try:
response = requests.post(url)
except requests.exceptions.RequestException as err:
sys.exit(err)
if response.status_code == requests.codes.ok:
print("Successfully connected to server " + str(response.status_code))
else:
print("ERROR: HTTP server response code " + str(response.status_code))
sys.exit("Bad HTTP response from Plex server")
# Go through the list of items in the playlist and add the playlist name to mood
for track in playlist.items():
existing_moods = [mood.tag for mood in track.moods]
data = tag_helper("mood", existing_moods + [playlistName])
track.edit(**data)
track.reload()
else:
sys.exit("Neither add, remove or refresh specified")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment