Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Created July 16, 2017 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/8ede4aabce105217a70cc2386ce673f7 to your computer and use it in GitHub Desktop.
Save JonnyWong16/8ede4aabce105217a70cc2386ce673f7 to your computer and use it in GitHub Desktop.
Saves artist.jpg to the Artist folder.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Saves artist.jpg to the Artist folder.
# Author: /u/SwiftPanda16
# Requires: plexapi, requests
from plexapi.server import PlexServer
import os
import requests
### EDIT SETTINGS ###
PLEX_URL = "http://localhost:32400"
PLEX_TOKEN = "xxxxxxxxxx"
MUSIC_LIBRARY_NAME = "Music"
DISC_FOLDERS = ("Disc 1", "Disc 2", "Disc 01", "Disc 02")
## CODE BELOW ##
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
headers = {"X-Plex-Token": PLEX_TOKEN}
for artist in plex.library.section(MUSIC_LIBRARY_NAME).all():
album = artist.albums()[0]
track = album.tracks()[0]
media = track.media[0]
part = media.parts[0]
file = part.file
path = os.path.dirname(file) # Folder containing the track
if path.endswith(DISC_FOLDERS): # If it is a disc folder
path = os.path.dirname(path) # Go up one folder
path = os.path.dirname(path) # Go up one folder
r = requests.get(PLEX_URL + track.grandparentThumb, headers=headers, stream=True)
with open(os.path.join(path, "artist.jpg"), "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment