Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active May 3, 2024 07:09
Show Gist options
  • Save JonnyWong16/4cfcf8ea50dab1b720b4d30e9a01835c to your computer and use it in GitHub Desktop.
Save JonnyWong16/4cfcf8ea50dab1b720b4d30e9a01835c to your computer and use it in GitHub Desktop.
Automatically add a label to recently added items in your Plex library
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Automatically add a label to recently added items in your Plex library
# Author: /u/SwiftPanda16
# Requires: requests
# Tautulli script trigger:
# * Notify on recently added
# Tautulli script conditions:
# * Filter which media to add labels to using conditions. Examples:
# [ Media Type | is | movie ]
# [ Show Name | is | Game of Thrones ]
# [ Album Name | is | Reputation ]
# [ Video Resolution | is | 4k ]
# [ Genre | contains | horror ]
# Tautulli script arguments:
# * Recently Added:
# --title {title} --section_id {section_id} --media_type {media_type} --rating_key {rating_key} --parent_rating_key {parent_rating_key} --grandparent_rating_key {grandparent_rating_key} --label "Label"
import argparse
import os
import requests
### OVERRIDES - ONLY EDIT IF RUNNING SCRIPT WITHOUT TAUTULLI ###
PLEX_URL = ''
PLEX_TOKEN = ''
### CODE BELOW ###
PLEX_URL = PLEX_URL or os.getenv('PLEX_URL', PLEX_URL)
PLEX_TOKEN = PLEX_TOKEN or os.getenv('PLEX_TOKEN', PLEX_TOKEN)
MEDIA_TYPES_PARENT_VALUES = {'movie': 1, 'show': 2, 'season': 2, 'episode': 2, 'album': 9, 'track': 9}
def add_label(media_type_value, rating_key, section_id, label):
headers = {'X-Plex-Token': PLEX_TOKEN}
params = {'type': media_type_value,
'id': rating_key,
'label.locked': 1,
'label[0].tag.tag': label
}
url = '{base_url}/library/sections/{section_id}/all'.format(base_url=PLEX_URL, section_id=section_id)
r = requests.put(url, headers=headers, params=params)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--title', required=True)
parser.add_argument('--section_id', required=True)
parser.add_argument('--media_type', required=True)
parser.add_argument('--rating_key', required=True)
parser.add_argument('--parent_rating_key', required=True)
parser.add_argument('--grandparent_rating_key', required=True)
parser.add_argument('--label', required=True)
opts = parser.parse_args()
if opts.media_type not in MEDIA_TYPES_PARENT_VALUES:
print("Cannot add label to '{opts.title}': Invalid media type '{opts.media_type}'".format(opts=opts))
else:
media_type_value = MEDIA_TYPES_PARENT_VALUES[opts.media_type]
rating_key = ''
if opts.media_type in ('movie', 'show', 'album'):
rating_key = opts.rating_key
elif opts.media_type in ('season', 'track'):
rating_key = opts.parent_rating_key
elif opts.media_type in ('episode'):
rating_key = opts.grandparent_rating_key
if rating_key and rating_key.isdigit():
add_label(media_type_value, int(rating_key), opts.section_id, opts.label)
print("The label '{opts.label}' was added to '{opts.title}' ({rating_key}).".format(opts=opts, rating_key=rating_key))
else:
print("Cannot add label to '{opts.title}': Invalid rating key '{rating_key}'".format(opts=opts, rating_key=rating_key))
@JPH71
Copy link

JPH71 commented Mar 8, 2021

How would one do this via an agent?
when running update script on a movie that has being searched for
I would like to add the website where a review was found in the sharing label
i tried metadata.labels.add('my string')
but that did not work

@abyssalsmoke
Copy link

How might I tweak this to add all my newly added movies into a collection called hide. Im using the new smart collections but they dont hide shit in my library so just want to make sure all new stuff gets hidden automatically.

@JohnnyGrey86
Copy link

Just wanted to say this Tautully script worked beautifully! Now If I can only find a way to have Plex not automatically merge library items.

@jakej1020
Copy link

Do you think this would be a good starting point to modify to change audio streams/ subtitles on library add? Specifically I have automation setup for two libraries of anime, one that grabs new episodes (Japanese) and the other will grab multi-audio episodes as they release a little later. I have a custom script setup to delete the old episode with single audio, but then I currently get notified and use PASTA to manually set the tracks. I'd like to automate this too if possible, so let me know if you have any ideas or feedback. Thanks!

@markyskus
Copy link

markyskus commented Apr 30, 2024

could you help with this script a little bit? I have got 3 movie and 3 tv show folder on my nas, I added the folders to my plex library all 3 movie folder to 1 library, I want to automatically adding labels for example Kids for new movies from folder Kids Movies (not by rating)
which section of the scripts must be changing?

I know, I could do it manually, but I want it automatically :D

or somebody :)

@JPH71
Copy link

JPH71 commented Apr 30, 2024 via email

@markyskus
Copy link

thanks, but I don't want to make separate libraries, sharing label works fine, just would be awesome if it works automatically, for example download finished in the middle of the night and on saturday morning my kids could watch it before I'm adding the label (2 or 3 libraries is 2 or 3 times manage process) Emby could handle it by default, but I prefer Plex because the picture quality is better (and some feature is look better in emby but worst to handle it)

@JPH71
Copy link

JPH71 commented Apr 30, 2024 via email

@markyskus
Copy link

thanks, I will looking after it, but labeling is really good 1-1 library to manage with 3 type of users, simple to use if user is young or old

with fast search about automatic collection, I think it can't handle my preferences
User1 (me) see every 3 folder
user2 (kids) only see folder 2 and folder 3
User3 (granparents) only see folder 1 and folder 2

but I don't see options to make collections about the folder where it is

@markyskus
Copy link

and rating is not option too, for example the last harry potter is pg-13 and loaded weapon 1 is pg-13 too, but my kids could watch hp (with me or my wife) but not loaded weapon (and not mention about movies without us-rating, too many variable, bigger task to makethe rules for ratings then manually adding every single movie or series a label :D )

@JPH71
Copy link

JPH71 commented Apr 30, 2024 via email

@markyskus
Copy link

finally, I think it's work how I want (so-so)
I'm playing with the conditions, and the result was File, contains, (folder name)
trying it with 3 movie, 2 in the specific folder and 1 in other. the 1 in other folder hasnt got the tag, it ok :D but only 1 got the label from the 2 :\

@markyskus
Copy link

I found out why, for some reason the script only runs on the penultimate one, not on the last one added. so the kids tag only appears if a new movie has been added afterwards

@markyskus
Copy link

and some time later it added the latest to the latest movie, so its working like a charm with movies
it would be great if there were some solution for series and collections as well

@JPH71
Copy link

JPH71 commented May 2, 2024 via email

@markyskus
Copy link

naah, I recently added manually

@JPH71
Copy link

JPH71 commented May 2, 2024 via email

@markyskus
Copy link

ah, I understand what you mean, before the script I added manually the label, after the script is added automatically in 5 minutes :) that's good for me :)

@markyskus
Copy link

can't figured out, why it's not working with series
accidentally I make a notification without condition (file+contains with blank) and the script runs well and write the tag to the series, then I modified the notify like the working with movies (file+contains+animation) then nothing happens, log says there is no animation in the file (and there is, and in xml wiew about an episode i can see there is)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment