This commit is contained in:
Blacktwin 2018-04-03 11:57:51 -04:00
parent 96594c93d6
commit 7ffaa7543f

View File

@ -1,19 +1,22 @@
''' """
Pull Movie and TV Show poster images from Plex. Description: Pull Movie and TV Show poster images from Plex. Save to Movie and TV Show directories in scripts working directory.
Save to Movie and TV Show directories in scripts working directory. Author: Blacktwin
Requires: plexapi
''' Example:
python plex_api_poster_pull.py
"""
from plexapi.server import PlexServer from plexapi.server import PlexServer
# pip install plexapi
import re import re
import os import os
import urllib import urllib
PLEX_URL = 'http://localhost:32400' PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'xxxxxxx' PLEX_TOKEN = ''
plex = PlexServer(PLEX_URL, PLEX_TOKEN) plex = PlexServer(PLEX_URL, PLEX_TOKEN)
library_name = ['Movies','TV Shows'] # You library names library_name = ['Movies','TV Shows'] # You library names
@ -27,30 +30,24 @@ show_path = '{}/TV Shows'.format(os.path.dirname(__file__))
if not os.path.isdir(show_path): if not os.path.isdir(show_path):
os.mkdir(show_path) os.mkdir(show_path)
child_lst = []
# Get all movies or shows from LIBRARY_NAME # Get all movies or shows from LIBRARY_NAME
for library in library_name: for library in library_name:
for child in plex.library.section(library).all(): for child in plex.library.section(library).all():
child_lst.append(child) # Clean names of special characters
name = re.sub('\W+',' ', child.title)
# Add (year) to name
for video in child_lst: name = '{} ({})'.format(name, child.year)
# Clean names of special characters # Pull URL for poster
name = re.sub('\W+',' ', video.title) thumb_url = '{}{}?X-Plex-Token={}'.format(PLEX_URL, child.thumb, PLEX_TOKEN)
# Pull URL for poster # Select path based on media_type
thumb_url = '{}{}?X-Plex-Token={}'.format(PLEX_URL, video.thumb, PLEX_TOKEN) if child.type == 'movie':
if video.type == 'movie': image_path = u'{}/{}.jpg'.format(movie_path, name)
image_path = u'{}/{}.jpg'.format(movie_path, name) elif child.type == 'show':
image_path = u'{}/{}.jpg'.format(show_path, name)
# Check if file already exists
if os.path.isfile(image_path): if os.path.isfile(image_path):
print("ERROR, %s already exist" % image_path) print("ERROR, %s already exist" % image_path)
else: else:
# Save to Movie directory # Save to directory
urllib.urlretrieve(thumb_url, image_path)
elif video.type == 'show':
image_path = u'{}/{}.jpg'.format(show_path, name)
if os.path.isfile(image_path):
print("ERROR, %s already exist" % image_path)
else:
# Save to TV Show directory
urllib.urlretrieve(thumb_url, image_path) urllib.urlretrieve(thumb_url, image_path)