Created
March 8, 2017 09:20
-
-
Save matbor/ae525ff4516d6dd523e06ff10b0f8846 to your computer and use it in GitHub Desktop.
This script will create a collection of your movies based on the movie rating, ie. If you want all G rated movies in a collection on it's own.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Description: Create a Plex collection from based on content rating. | |
# Original Author: /u/SwiftPanda16 | |
# Modified by: @bordignon on Twitter. | |
# Requires: plexapi, requests | |
# | |
import requests | |
from plexapi.server import PlexServer | |
### EDIT SETTINGS ### | |
PLEX_URL = 'http://localhost:32400' | |
PLEX_TOKEN = 'xxxxxxxxxxxx' | |
PLEX_LIBRARY_NAME = 'Movies' # where the movie's are located. | |
COLLECTION_NAME = 'Kids' # New collection name, ie. KIDS movies. | |
CONTENT_RATING_SEARCH = ['G','PG'] # The Movie's Content Rating, ie. G or PG for kids movies. | |
### CODE BELOW ### | |
def add_collection(media_item): | |
library_key = media_item.librarySectionID | |
rating_key = media_item.ratingKey | |
collection_key = "collection[{key}].tag.tag".format(key=len(media_item.collections)) | |
headers = {"X-Plex-Token": PLEX_TOKEN} | |
params = {"type": 1, | |
"id": rating_key, | |
"collection.locked": 1, | |
collection_key: COLLECTION_NAME | |
} | |
url = "{base_url}/library/sections/{library}/all".format(base_url=PLEX_URL, library=library_key) | |
r = requests.put(url, headers=headers, params=params) | |
def main(): | |
try: | |
plex = PlexServer(PLEX_URL, PLEX_TOKEN) | |
except: | |
print("No Plex server found at '{base_url}', or invalid token.".format(base_url=PLEX_URL)) | |
print("Exiting script.") | |
return | |
movies = plex.library.section(PLEX_LIBRARY_NAME) | |
x = 0 | |
y = 0 | |
#for video in movies.search(unwatched=True): | |
for key in CONTENT_RATING_SEARCH: | |
for video in movies.search(contentRating=key): | |
print("%s (ID:%s) Rating: %s") % (video.title, video.ratingKey, video.contentRating) | |
x = x + 1 | |
try: | |
media_item = plex.library.getByKey(video.ratingKey) | |
if COLLECTION_NAME.lower() not in [c.tag.lower() for c in media_item.collections]: | |
add_collection(media_item) | |
y = y + 1 | |
else: | |
print(" This movie already exists in this Collection. Skipping.") | |
continue | |
except: | |
print("Error adding") | |
pass | |
print("Movie's found with matching rating: %s" % (x)) | |
print("Movie's added to collection: %s" % (y)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment