Skip to content

Commit

Permalink
Error checking for config and new mini 4k banner
Browse files Browse the repository at this point in the history
  • Loading branch information
jkirkcaldy committed Jul 29, 2021
1 parent 26462ed commit 1f3e60d
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 32 deletions.
39 changes: 28 additions & 11 deletions 4k_hdr_poster.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/local/bin/python3
from pathlib import Path
from PIL import Image, ImageChops
from plexapi.server import PlexServer
Expand All @@ -18,23 +19,27 @@
# Do not edit these, use the config file to make any changes

config_object = ConfigParser()
config_object.read("/config/config.ini")
config_object.read(".config.ini")
server = config_object["PLEXSERVER"]
baseurl = (server["PLEX_URL"])
token = (server["TOKEN"])
plexlibrary = (server["FILMSLIBRARY"])
ppath = (server["PLEXPATH"])
mpath = (server["MOUNTEDPATH"])
pbak = (server["POSTER_BU"])
HDR_BANNER = (server["HDR_BANNER"])
pbak = str.lower((server["POSTER_BU"]))
HDR_BANNER = str.lower((server["HDR_BANNER"]))
plex = PlexServer(baseurl, token)
films = plex.library.section(plexlibrary)
mini_4k = str.lower((server["mini_4k"]))
banner_4k = Image.open("img/4K-Template.png")
mini_4k_banner = Image.open("img/4K-mini-Template.png")
banner_hdr = Image.open("img/hdr-poster.png")
chk_banner = Image.open("img/chk-4k.png")
chk_mini_banner = Image.open("img/chk-mini-4k.png")
chk_hdr = Image.open("img/chk_hdr.png")
size = (911,1367)
box= (0,0,911,100)
mini_box = (0,0,301,268)
hdr_box = (0,611,215,720)

now = datetime.now()
Expand All @@ -55,6 +60,19 @@ def add_banner():
background.save('poster.png')
i.uploadPoster(filepath="poster.png")

def add_mini_banner():
background = Image.open('poster.png')
background = background.resize(size,Image.ANTIALIAS)
backgroundchk = background.crop(mini_box)
hash0 = imagehash.average_hash(backgroundchk)
hash1 = imagehash.average_hash(chk_mini_banner)
cutoff= 15
if hash0 - hash1 < cutoff:
print('4K banner exists, moving on...')
else:
background.paste(mini_4k_banner, (0, 0), mini_4k_banner)
background.save('poster.png')
i.uploadPoster(filepath="poster.png")

def add_hdr():
background = Image.open('poster.png')
Expand All @@ -81,7 +99,7 @@ def get_poster():
img.raw.decode_content = True
with open(filename, 'wb') as f:
shutil.copyfileobj(img.raw, f)
if pbak == 'True':
if pbak == 'true':
if backup == True:
print('Backup File Exists, Skipping...')
else:
Expand All @@ -91,30 +109,29 @@ def get_poster():
print(Fore.RED+films.title+"cannot find the poster for this film")
print(Fore.RESET)


def poster_4k_hdr():
print(i.title + ' 4k HDR')
get_poster()
add_banner()
add_hdr()
os.remove('poster.png')


def poster_4k():
print(i.title + " 4K Poster")
get_poster()
add_banner()
if mini_4k == 'true':
add_mini_banner()
else:
add_banner()
os.remove('poster.png')




def poster_hdr():
print(i.title + " HDR Poster")
get_poster()
add_hdr()
os.remove('poster.png')

if HDR_BANNER == 'True':
if HDR_BANNER == 'true':
for i in films.search(resolution="4k", hdr=False):
try:
poster_4k()
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Change this in the config.ini file and add the location of the local directory w

If your paths are the same or you are running the script on the same machine as your plex server make sure that both entries in the config file match otherwise you will get an error.

#### Optional
Now there is the option to use a minified 4k logo if you don't want to have the full width banner on your posters.

from IPython.display import HTML, display
display(HTML("<table><tr><td><img src='https://github.com/jkirkcaldy/plex-utills/blob/master/img/4k-example.png?raw=true'></td><td><img src='https://github.com/jkirkcaldy/plex-utills/blob/master/img/mini-4k-example.png?raw=true'></td></tr></table>"))


### Hide-4k Files
The plex streaming brain has come on a long way and I believe it's no longer necessary to separate your 4k files into a separate library.
Expand All @@ -30,7 +36,7 @@ Run this script on a regular basis to keep on top of your library.
Set transcode to True in the config file. This will send your 4k only files to be optimised through plex. The setting for this is 1080p 10mbps. This is not reccomended on low powered hardware.


#### Disney/Pixar collection
### Disney/Pixar collection
This is a script to find all films in your library with a studio having Disney in the studio's title, e.g. Walt Disney Pictures or Disney animation. It then adds all of these films into a collection named Disney.

It then does the same for Pixar
Expand Down
16 changes: 9 additions & 7 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ FILMSLIBRARY = Films
PLEXPATH = /media
# no trailing slash.
MOUNTEDPATH = /films
POSTER_BU = True


# Set to true to add a HDR banner, setting to False will only add the 4k Banner to your media.
HDR_BANNER = True
4k_hdr_posters = true
mini_4k = true
HDR_BANNER = TRUE
POSTER_BU = true

4k_hdr_posters = True
Disney = True
Pixar = True
hide_4k = True
Pixar = true
hide_4k = False

#Set this to true, if you would like your server to create an optimised HD version of your 4K films. This is not reccomended on low powered hardware
transcode = True
transcode = false

[SCHEDULES]
#enter the time of day you want the script to run
poster_schedule = 23:00
disney_schedule = 22:00
pixar_schedule = 22:15
pixar_schedule = 22:10
hide_poster_schedule = 01:00
117 changes: 117 additions & 0 deletions config_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/local/bin/python
import os
import subprocess
from configparser import ConfigParser
import subprocess
import schedule
import time
from datetime import datetime
import re
from colorama import Fore, Back, Style


config_object = ConfigParser()
config_object.read("/config/config.ini")
server = config_object["PLEXSERVER"]
schedules = config_object["SCHEDULES"]
hdr_4k_posters = str.lower((server["4k_hdr_posters"]))
Disney = str.lower((server["Disney"]))
Pixar = (str.lower(server["Pixar"]))
hide_4k = str.lower((server["hide_4k"]))
pbak = str.lower((server["POSTER_BU"]))
HDR_BANNER = str.lower((server["HDR_BANNER"]))
optimise = str.lower((server["transcode"]))
mini_4k = str.lower((server["mini_4k"]))
t1 = (schedules["poster_schedule"])
t2 = (schedules["disney_schedule"])
t3 = (schedules["pixar_schedule"])
t4 = (schedules["hide_poster_schedule"])



if pbak == 'true':
pass
elif pbak == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if HDR_BANNER == 'true':
pass
elif HDR_BANNER == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if mini_4k == 'true':
pass
elif mini_4k == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if hdr_4k_posters == 'true':
pass
elif hdr_4k_posters == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if Disney == 'true':
pass
elif Disney == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if Pixar == 'true':
pass
elif Pixar == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if hide_4k == 'true':
pass
elif hide_4k == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')

if optimise == 'true':
pass
elif optimise == 'false':
pass
else:
raise ValueError('SYNTAX ERROR: Please enter either "true" or "false" to set the script behaviour.')



a = re.compile("^[0-9]{2}:[0-9]{2}$")
if a.match(t1) and hdr_4k_posters == 'true':
pass
elif hdr_4k_posters != 'true':
pass
else:
raise ValueError('Please make sure that your scheduled times are written in the format HH:MM')
if a.match(t2) and Disney == 'true':
pass
elif Disney != 'true':
pass
else:
raise ValueError('Please make sure that your scheduled times are written in the format HH:MM')
if a.match(t3) and Pixar == 'true':
pass
elif Pixar != 'true':
pass
else:
raise ValueError('Please make sure that your scheduled times are written in the format HH:MM')
if a.match(t4) and hide_4k == 'true':
pass
elif hide_4k != 'true':
pass
else:
raise ValueError('Please make sure that your scheduled times are written in the format HH:MM')

print('Config check passed')

4 changes: 3 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ services:
restart: unless-stopped
volumes:
- <your Plex media directory>:/films
- <your config location>:/config
- <your config location>:/config
environment:
- TZ=Europe/London
4 changes: 2 additions & 2 deletions hide-4k.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
config_object = ConfigParser()
config_object.read("/config/config.ini")
server = config_object["PLEXSERVER"]
optimise = (server["transcode"])
optimise = str.lower((server["transcode"]))
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(current_time, ": Hide 4k films script starting now")
Expand All @@ -28,7 +28,7 @@
if optimise == 'False':
movie.addLabel('Untranscodable')
print(movie.title+' has only 4k avaialble, setting untranscodable' )
elif optimise == 'True':
elif optimise == 'true':
print('Sending', movie.title, 'to be transcoded')
movie.optimize(deviceProfile="Android", videoQuality=10)
for movie in b:
Expand Down
Binary file added img/4K-mini-Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/4k-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/chk-mini-4k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mini-4k-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 17 additions & 9 deletions run_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/bin/python
#!/usr/local/bin/python3
import os
import subprocess
from configparser import ConfigParser
Expand All @@ -12,10 +12,10 @@
config_object.read("/config/config.ini")
server = config_object["PLEXSERVER"]
schedules = config_object["SCHEDULES"]
hdr_4k_posters = (server["4k_hdr_posters"])
Disney = (server["Disney"])
Pixar = (server["Pixar"])
hide_4k = (server["hide_4k"])
hdr_4k_posters = str.lower((server["4k_hdr_posters"]))
Disney = str.lower((server["Disney"]))
Pixar = (str.lower(server["Pixar"]))
hide_4k = str.lower((server["hide_4k"]))

t1 = (schedules["poster_schedule"])
t2 = (schedules["disney_schedule"])
Expand All @@ -25,25 +25,30 @@
now = datetime.now()
current_time = now.strftime("%H:%M:%S")

def check_config():
p = subprocess.Popen('python -u ./config_check.py', shell=True, stderr=subprocess.STDOUT)
output = p.communicate()
print(output[0])

def posters():
if hdr_4k_posters == "True":
if hdr_4k_posters == "true":
p = subprocess.Popen('python -u ./4k_hdr_poster.py', shell=True)
output = p.communicate()
print(output[0])

def disney():
if Disney == "True":
if Disney == "true":
p = subprocess.Popen('python -u ./disney_collection.py', shell=True)
output = p.communicate()
print(output[0])

def pixar():
if Pixar == "True":
if Pixar == "true":
p = subprocess.Popen('python -u ./Pixar_collection.py', shell=True)
output = p.communicate()
print(output[0])
def hide_4k_films():
if hide_4k == "True":
if hide_4k == "true":
p = subprocess.Popen('python -u ./hide-4k.py', shell=True)
output = p.communicate()
print(output[0])
Expand All @@ -52,6 +57,9 @@ def working():
print(current_time, ": This script is still working, check back later for more info")

print(current_time, ": This script is now running, check back later for more info")
check_config()



schedule.every(60).minutes.do(working)
schedule.every().day.at(t1).do(posters)
Expand Down
2 changes: 1 addition & 1 deletion setup_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from colorama import Fore, Back, Style

config_object = ConfigParser()
config_object.read("config.ini")
config_object.read(".config.ini")

server = config_object["PLEXSERVER"]
baseurl = (server["PLEX_URL"])
Expand Down

0 comments on commit 1f3e60d

Please sign in to comment.