2021-01-01 16:12:22 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""Schedule Plex server related Pre-roll Intro videos
|
|
|
|
A helper script to automate management of Plex pre-rolls.
|
|
|
|
Define when you want different pre-rolls to play throughout the year.
|
|
|
|
|
|
|
|
Set it and forget it!
|
|
|
|
|
|
|
|
Optional Arguments:
|
2021-01-01 18:51:22 +00:00
|
|
|
-h, --help show this help message and exit
|
|
|
|
-v, --version show the version number and exit
|
2021-01-07 03:33:14 +00:00
|
|
|
-lc LOG_CONFIG_FILE, --logconfig-path LOG_CONFIG_FILE
|
2021-01-21 02:54:47 +00:00
|
|
|
Path to logging config file.
|
|
|
|
[Default: ./logging.conf]
|
2021-01-01 18:51:22 +00:00
|
|
|
-c CONFIG_FILE, --config-path CONFIG_FILE
|
2021-01-21 02:54:47 +00:00
|
|
|
Path to Config.ini to use for Plex Server info.
|
|
|
|
[Default: ./config.ini]
|
2021-01-01 18:51:22 +00:00
|
|
|
-s SCHEDULE_FILE, --schedule-path SCHEDULE_FILE
|
2021-01-21 02:54:47 +00:00
|
|
|
Path to pre-roll schedule file (YAML) to be use.
|
|
|
|
[Default: ./preroll_schedules.yaml]
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
Requirements:
|
|
|
|
- See Requirements.txt for Python modules
|
|
|
|
|
|
|
|
Scheduling:
|
|
|
|
Add to system scheduler such as:
|
|
|
|
> crontab -e
|
|
|
|
> 0 0 * * * python path/to/schedule_preroll.py >/dev/null 2>&1
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
FileNotFoundError: [description]
|
|
|
|
KeyError: [description]
|
|
|
|
ConfigError: [description]
|
|
|
|
FileNotFoundError: [description]
|
|
|
|
"""
|
2021-01-03 07:34:20 +00:00
|
|
|
|
2021-01-01 16:12:22 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import requests
|
2021-01-07 03:33:14 +00:00
|
|
|
from datetime import datetime, date, timedelta
|
2021-01-01 16:12:22 +00:00
|
|
|
import yaml
|
2021-01-07 03:33:14 +00:00
|
|
|
from typing import NamedTuple, Union, Optional, Tuple, List, Dict
|
|
|
|
from argparse import Namespace, ArgumentParser
|
2021-01-01 16:12:22 +00:00
|
|
|
from configparser import ConfigParser
|
|
|
|
from configparser import Error as ConfigError
|
|
|
|
from plexapi.server import PlexServer, CONFIG
|
|
|
|
|
2021-01-03 07:34:20 +00:00
|
|
|
# import local util modules
|
2021-01-01 16:12:22 +00:00
|
|
|
import plexutil
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
filename = os.path.basename(sys.argv[0])
|
|
|
|
SCRIPT_NAME = os.path.splitext(filename)[0]
|
|
|
|
|
2021-01-07 03:33:14 +00:00
|
|
|
class ScheduleEntry(NamedTuple):
|
|
|
|
type: str
|
2021-01-21 02:54:47 +00:00
|
|
|
startdate: datetime
|
|
|
|
enddate: datetime
|
2021-01-07 03:33:14 +00:00
|
|
|
force: bool
|
|
|
|
path: str
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
ScheduleType = Dict[str, List[ScheduleEntry]]
|
2021-01-07 03:33:14 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def arguments() -> Namespace:
|
|
|
|
"""Setup and Return command line arguments
|
2021-01-01 16:12:22 +00:00
|
|
|
See https://docs.python.org/3/howto/argparse.html
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
argparse.Namespace: Namespace object
|
|
|
|
"""
|
|
|
|
description = 'Automate scheduling of pre-roll intros for Plex'
|
2021-01-21 02:54:47 +00:00
|
|
|
version = '0.10.1'
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
config_default = './config.ini'
|
2021-01-01 18:31:15 +00:00
|
|
|
log_config_default = './logging.conf'
|
2021-01-01 16:12:22 +00:00
|
|
|
schedule_default = './preroll_schedules.yaml'
|
|
|
|
parser = ArgumentParser(description='{}'.format(description))
|
2021-01-03 07:34:20 +00:00
|
|
|
parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(version),
|
2021-01-21 02:54:47 +00:00
|
|
|
help='show the version number and exit'
|
|
|
|
)
|
2021-01-07 03:57:06 +00:00
|
|
|
parser.add_argument('-lc', '--logconfig-file',
|
2021-01-03 07:34:20 +00:00
|
|
|
dest='log_config_file', action='store',
|
|
|
|
default=log_config_default,
|
2021-01-21 02:54:47 +00:00
|
|
|
help='Path to logging config file. [Default: {}]' \
|
|
|
|
.format(log_config_default)
|
|
|
|
)
|
2021-01-07 03:33:14 +00:00
|
|
|
parser.add_argument('-t', '--test-run',
|
|
|
|
dest='do_test_run', action='store_true',
|
|
|
|
default=False,
|
2021-01-21 02:54:47 +00:00
|
|
|
help='Perform a test run, display output but dont save'
|
|
|
|
)
|
2021-01-07 03:57:06 +00:00
|
|
|
parser.add_argument('-c', '--config-file',
|
2021-01-03 07:34:20 +00:00
|
|
|
dest='config_file', action='store',
|
2021-01-21 02:54:47 +00:00
|
|
|
help='Path to Config.ini to use for Plex Server info. [Default: {}]' \
|
|
|
|
.format(config_default)
|
|
|
|
)
|
2021-01-07 03:57:06 +00:00
|
|
|
parser.add_argument('-s', '--schedule-file',
|
2021-01-03 07:34:20 +00:00
|
|
|
dest='schedule_file', action='store',
|
2021-01-21 02:54:47 +00:00
|
|
|
help='Path to pre-roll schedule file (YAML) to be use. [Default: {}]' \
|
|
|
|
.format(schedule_default)
|
|
|
|
)
|
2021-01-01 16:12:22 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def schedule_types() -> ScheduleType:
|
|
|
|
"""Return the main types of schedules to be used for storage processing
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-01-21 02:54:47 +00:00
|
|
|
ScheduleType: Dict of main schema items
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
2021-01-21 02:54:47 +00:00
|
|
|
schema : ScheduleType = {
|
|
|
|
'default': [],
|
|
|
|
'monthly': [],
|
|
|
|
'weekly': [],
|
|
|
|
'date_range': [],
|
|
|
|
'misc': []
|
|
|
|
}
|
2021-01-01 16:12:22 +00:00
|
|
|
return schema
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def week_range(year:int, weeknum:int) -> Tuple[datetime, datetime]:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Return the starting/ending date range of a given year/week
|
|
|
|
|
|
|
|
Args:
|
2021-01-01 18:31:15 +00:00
|
|
|
year (int): Year to calc range for
|
|
|
|
weeknum (int): Month of the year (1-12)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-01-21 02:54:47 +00:00
|
|
|
DateTime: Start date of the Year/Month
|
|
|
|
DateTime: End date of the Year/Month
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
2021-01-03 07:34:20 +00:00
|
|
|
start = datetime.strptime('{}-W{}-0'.format(year, int(weeknum)-1),
|
|
|
|
"%Y-W%W-%w").date()
|
|
|
|
end = start + timedelta(days=6)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
start = datetime.combine(start, datetime.min.time())
|
|
|
|
end = datetime.combine(end, datetime.max.time())
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
return (start, end)
|
|
|
|
|
|
|
|
def month_range(year:int, monthnum:int) -> Tuple[datetime, datetime]:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Return the starting/ending date range of a given year/month
|
|
|
|
|
|
|
|
Args:
|
2021-01-01 18:31:15 +00:00
|
|
|
year (int): Year to calc range for
|
2021-01-01 16:12:22 +00:00
|
|
|
monthnum (int): Month of the year (1-12)
|
|
|
|
|
|
|
|
Returns:
|
2021-01-21 02:54:47 +00:00
|
|
|
DateTime: Start date of the Year/Month
|
|
|
|
DateTime: End date of the Year/Month
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
2021-01-03 07:34:20 +00:00
|
|
|
start = date(year, monthnum, 1)
|
|
|
|
next_month = start.replace(day=28) + timedelta(days=4)
|
|
|
|
end = next_month - timedelta(days=next_month.day)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
start = datetime.combine(start, datetime.min.time())
|
|
|
|
end = datetime.combine(end, datetime.max.time())
|
|
|
|
|
|
|
|
return (start, end)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-07 03:33:14 +00:00
|
|
|
def duration_seconds(start:Union[date,datetime], end:Union[date,datetime]) -> float:
|
|
|
|
"""Return length of time between two date/datetime in seconds
|
|
|
|
|
|
|
|
Args:
|
|
|
|
start (date/datetime): [description]
|
|
|
|
end (date/datetime): [description]
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
float: Length in time seconds
|
|
|
|
"""
|
|
|
|
if not isinstance(start, datetime):
|
|
|
|
start = datetime.combine(start, datetime.min.time())
|
|
|
|
if not isinstance(end, datetime):
|
|
|
|
end = datetime.combine(end, datetime.max.time())
|
|
|
|
|
|
|
|
delta = end - start
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
logger.debug('duration_second[] Start: {} End: {} Duration: {}'.format(start,
|
|
|
|
end,
|
|
|
|
delta.total_seconds()
|
|
|
|
))
|
2021-01-07 03:33:14 +00:00
|
|
|
return delta.total_seconds()
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def make_datetime(value: Union[str, date, datetime], lowtime: bool=True) -> datetime:
|
|
|
|
"""Returns a DateTime object with a calculated Time component if none provided
|
|
|
|
converts:
|
|
|
|
* Date to DateTime, with a Time of Midnight 00:00 or 11:59 pm
|
|
|
|
* String to DateTime, with a Time as defined in the string
|
|
|
|
|
|
|
|
Args:
|
|
|
|
value (Union[str, date, datetime]): Input value to convert to a DateTime object
|
|
|
|
lowtime (bool, optional): Calculate time to be midnight (True) or 11:59 PM (False).
|
|
|
|
Defaults to True.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
TypeError: Unknown type to calculate
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
datetime: DateTime object with time component set if none provided
|
|
|
|
"""
|
|
|
|
today = date.today()
|
|
|
|
now = datetime.now()
|
|
|
|
dt_val = datetime(today.year, today.month, today.day, 0,0,0)
|
|
|
|
|
|
|
|
# append the low or high time of the day
|
|
|
|
if lowtime:
|
|
|
|
time = datetime.min.time()
|
|
|
|
else:
|
|
|
|
time = datetime.max.time()
|
|
|
|
|
|
|
|
# determine how to translate the input value
|
|
|
|
if isinstance(value, datetime):
|
|
|
|
dt_val = value
|
|
|
|
elif isinstance(value, date):
|
|
|
|
dt_val = datetime.combine(value, time)
|
|
|
|
elif isinstance(value, str):
|
|
|
|
try:
|
|
|
|
# Expect format of DateType string to be (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS)
|
|
|
|
# allow 'xx' to denote 'every' similar to Cron "*"
|
|
|
|
msg = 'Translating string value="{}" to datetime (LowTime={})'.format(value,
|
|
|
|
lowtime)
|
|
|
|
logger.debug(msg)
|
|
|
|
|
|
|
|
# default to today and the time period (low/high)
|
|
|
|
year, month, day = today.year, today.month, today.day
|
|
|
|
hour, minute, second = time.hour, time.minute, time.second
|
|
|
|
|
|
|
|
# start parsing the Time out, for later additional processing
|
|
|
|
dateparts = value.lower().split('-')
|
|
|
|
|
|
|
|
year = today.year if dateparts[0] == 'xxxx' else int(dateparts[0])
|
|
|
|
month = today.month if dateparts[1] == 'xx' else int(dateparts[1])
|
|
|
|
|
|
|
|
dateparts_day = dateparts[2].split(' ')
|
|
|
|
|
|
|
|
day = today.day if dateparts_day[0] == 'xx' else int(dateparts_day[0])
|
|
|
|
|
|
|
|
# attempt to parse out Time components
|
|
|
|
if len(dateparts_day) > 1:
|
|
|
|
timeparts = dateparts_day[1].split(':')
|
|
|
|
if len(timeparts) > 1:
|
|
|
|
hour = now.hour if timeparts[0] == 'xx' else int(timeparts[0])
|
|
|
|
minute = now.minute if timeparts[1] == 'xx' else int(timeparts[1])
|
|
|
|
second = now.second + 1 if timeparts[2] == 'xx' else int(timeparts[2])
|
|
|
|
|
|
|
|
dt_val = datetime(year, month, day, hour, minute, second)
|
|
|
|
logger.debug('Datetime-> "{}"'.format(dt_val))
|
|
|
|
except Exception as e:
|
|
|
|
msg = 'Unable to parse date string "{}"'.format(value)
|
|
|
|
logger.error(msg, exc_info=e)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
msg = 'UnknownType: Unable to parse date string "{}" for type "{}"'.format(value,
|
|
|
|
type(value)
|
|
|
|
)
|
|
|
|
logger.error(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
|
|
|
|
return dt_val
|
|
|
|
|
|
|
|
def preroll_schedule(schedule_file: Optional[str]=None) -> List[ScheduleEntry]:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Return a listing of defined preroll schedules for searching/use
|
|
|
|
|
2021-01-01 18:31:15 +00:00
|
|
|
Args:
|
2021-01-07 03:33:14 +00:00
|
|
|
schedule_file (str): path/to/schedule_preroll.yaml style config file (YAML Format)
|
2021-01-01 18:31:15 +00:00
|
|
|
|
2021-01-01 16:12:22 +00:00
|
|
|
Raises:
|
|
|
|
FileNotFoundError: If no schedule config file exists
|
|
|
|
|
|
|
|
Returns:
|
2021-01-21 02:54:47 +00:00
|
|
|
list: list of ScheduleEntries
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
|
|
|
default_files = ['preroll_schedules.yaml', 'preroll_schedules.yml']
|
|
|
|
|
2021-01-01 18:31:15 +00:00
|
|
|
filename = None
|
2021-01-07 03:33:14 +00:00
|
|
|
if schedule_file != '' and schedule_file != None:
|
|
|
|
if os.path.exists(str(schedule_file)):
|
2021-01-01 18:31:15 +00:00
|
|
|
filename = schedule_file
|
2021-01-01 16:12:22 +00:00
|
|
|
else:
|
2021-01-03 07:34:20 +00:00
|
|
|
msg = 'Pre-roll Schedule file "{}" not found'.format(schedule_file)
|
|
|
|
raise FileNotFoundError(msg)
|
2021-01-01 16:12:22 +00:00
|
|
|
else:
|
|
|
|
for f in default_files:
|
|
|
|
if os.path.exists(f):
|
2021-01-01 18:31:15 +00:00
|
|
|
filename = f
|
2021-01-01 16:12:22 +00:00
|
|
|
break
|
|
|
|
|
2021-01-03 07:34:20 +00:00
|
|
|
# if we still cant find a schedule file, we abort
|
2021-01-01 18:31:15 +00:00
|
|
|
if not filename:
|
2021-01-03 07:34:20 +00:00
|
|
|
msg = 'Missing schedule file: "{}"'.format('" / "'.join(default_files))
|
2021-01-01 16:12:22 +00:00
|
|
|
logger.critical(msg)
|
|
|
|
raise FileNotFoundError(msg)
|
|
|
|
|
2021-01-01 18:31:15 +00:00
|
|
|
with open(filename, 'r') as file:
|
2021-01-07 03:33:14 +00:00
|
|
|
contents = yaml.load(file, Loader=yaml.SafeLoader)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-03 07:34:20 +00:00
|
|
|
today = date.today()
|
2021-01-21 02:54:47 +00:00
|
|
|
schedule : List[ScheduleEntry] = []
|
|
|
|
for schedule_section in schedule_types():
|
2021-01-07 03:33:14 +00:00
|
|
|
if schedule_section == 'weekly':
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
use = contents[schedule_section]['enabled']
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if use:
|
|
|
|
for i in range(1,53):
|
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
path = contents[schedule_section][i]
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if path:
|
2021-01-21 02:54:47 +00:00
|
|
|
start, end = week_range(today.year, i)
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
entry = ScheduleEntry(type=schedule_section,
|
|
|
|
force=False,
|
|
|
|
startdate=start,
|
|
|
|
enddate=end,
|
|
|
|
path=path)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
schedule.append(entry)
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
2021-01-01 16:12:22 +00:00
|
|
|
# skip KeyError for missing Weeks
|
2021-01-21 02:54:47 +00:00
|
|
|
msg = 'Key Value not found: "{}"->"{}", skipping week'.format(schedule_section,
|
|
|
|
i)
|
2021-01-03 07:34:20 +00:00
|
|
|
logger.debug(msg)
|
2021-01-07 03:33:14 +00:00
|
|
|
pass
|
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found in "{}" section'.format(schedule_section)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
elif schedule_section == 'monthly':
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
use = contents[schedule_section]['enabled']
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if use:
|
|
|
|
for i in range(1,13):
|
2021-01-03 07:34:20 +00:00
|
|
|
month_abrev = date(today.year, i, 1).strftime('%b').lower()
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
path = contents[schedule_section][month_abrev]
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if path:
|
2021-01-21 02:54:47 +00:00
|
|
|
start, end = month_range(today.year, i)
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
entry = ScheduleEntry(type=schedule_section,
|
|
|
|
force=False,
|
|
|
|
startdate=start,
|
|
|
|
enddate=end,
|
|
|
|
path=path)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
schedule.append(entry)
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
2021-01-01 16:12:22 +00:00
|
|
|
# skip KeyError for missing Months
|
2021-01-21 02:54:47 +00:00
|
|
|
msg = 'Key Value not found: "{}"->"{}", skipping month'.format(schedule_section,
|
|
|
|
month_abrev)
|
2021-01-03 07:34:20 +00:00
|
|
|
logger.warning(msg)
|
2021-01-07 03:33:14 +00:00
|
|
|
pass
|
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found in "{}" section'.format(schedule_section)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
elif schedule_section == 'date_range':
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
use = contents[schedule_section]['enabled']
|
2021-01-01 16:12:22 +00:00
|
|
|
if use:
|
2021-01-07 03:33:14 +00:00
|
|
|
for r in contents[schedule_section]['ranges']:
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
|
|
|
path = r['path']
|
|
|
|
|
|
|
|
if path:
|
2021-01-07 03:33:14 +00:00
|
|
|
try:
|
|
|
|
force = r['force']
|
|
|
|
except KeyError as ke:
|
|
|
|
# special case Optional, ignore
|
|
|
|
force = False
|
|
|
|
pass
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
start = make_datetime(r['start_date'], lowtime=True)
|
|
|
|
end = make_datetime(r['end_date'], lowtime=False)
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
entry = ScheduleEntry(type=schedule_section,
|
|
|
|
force=force,
|
|
|
|
startdate=start,
|
|
|
|
enddate=end,
|
|
|
|
path=path)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
schedule.append(entry)
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found for entry: "{}"'.format(entry)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found in "{}" section'.format(schedule_section)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
elif schedule_section == 'misc':
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
use = contents[schedule_section]['enabled']
|
2021-01-01 16:12:22 +00:00
|
|
|
if use:
|
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
path = contents[schedule_section]['always_use']
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if path:
|
2021-01-07 03:33:14 +00:00
|
|
|
entry = ScheduleEntry(type=schedule_section,
|
|
|
|
force=False,
|
2021-01-21 02:54:47 +00:00
|
|
|
startdate=datetime(today.year, today.month, today.day,
|
|
|
|
0, 0, 0),
|
|
|
|
enddate=datetime(today.year, today.month, today.day,
|
|
|
|
23,59,59),
|
2021-01-07 03:33:14 +00:00
|
|
|
path=path)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
schedule.append(entry)
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found for entry: "{}"'.format(entry)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found in "{}" section'.format(schedule_section)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
elif schedule_section == 'default':
|
2021-01-01 16:12:22 +00:00
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
use = contents[schedule_section]['enabled']
|
2021-01-01 16:12:22 +00:00
|
|
|
if use:
|
|
|
|
try:
|
2021-01-07 03:33:14 +00:00
|
|
|
path = contents[schedule_section]['path']
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
if path:
|
2021-01-07 03:33:14 +00:00
|
|
|
entry = ScheduleEntry(type=schedule_section,
|
|
|
|
force=False,
|
2021-01-21 02:54:47 +00:00
|
|
|
startdate=datetime(today.year, today.month, today.day,
|
|
|
|
0, 0, 0),
|
|
|
|
enddate=datetime(today.year, today.month, today.day,
|
|
|
|
23,59,59),
|
2021-01-07 03:33:14 +00:00
|
|
|
path=path)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
schedule.append(entry)
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found for entry: "{}"'.format(entry)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
|
|
|
except KeyError as ke:
|
|
|
|
msg = 'Key Value not found in "{}" section'.format(schedule_section)
|
|
|
|
logger.error(msg, exc_info=ke)
|
|
|
|
raise
|
2021-01-01 16:12:22 +00:00
|
|
|
else:
|
2021-01-07 03:33:14 +00:00
|
|
|
msg = 'Unknown schedule_section "{}" detected'.format(schedule_section)
|
|
|
|
logger.error(msg)
|
|
|
|
raise ValueError(msg)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
# Sort list so most recent Ranges appear first
|
2021-01-07 03:33:14 +00:00
|
|
|
schedule.sort(reverse=True, key=lambda x:x.startdate)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
return schedule
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def build_listing_string(items: List[str], play_all: bool=False) -> str:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Build the Plex formatted string of preroll paths
|
|
|
|
|
|
|
|
Args:
|
2021-01-01 18:31:15 +00:00
|
|
|
items (list): List of preroll video paths to place into a string listing
|
|
|
|
play_all (bool, optional): Play all videos. [Default: False (Random choice)]
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: CSV Listing (, or ;) based on play_all param of preroll video paths
|
|
|
|
"""
|
|
|
|
if play_all:
|
|
|
|
# use , to play all entries
|
|
|
|
listing = ','.join(items)
|
|
|
|
else:
|
|
|
|
#use ; to play random selection
|
|
|
|
listing = ';'.join(items)
|
|
|
|
|
|
|
|
return listing
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def preroll_listing(schedule: List[ScheduleEntry], for_datetime: Optional[datetime]=None) -> str:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Return listing of preroll videos to be used by Plex
|
|
|
|
|
2021-01-01 18:31:15 +00:00
|
|
|
Args:
|
2021-01-07 03:33:14 +00:00
|
|
|
schedule (List[ScheduleEntry]): List of schedule entries (See: getPrerollSchedule)
|
2021-01-03 07:34:20 +00:00
|
|
|
for_datetime (datetime, optional): Date to process pre-roll string for [Default: Today]
|
2021-01-21 02:54:47 +00:00
|
|
|
Useful for simulating what different dates produce
|
2021-01-01 18:31:15 +00:00
|
|
|
|
2021-01-01 16:12:22 +00:00
|
|
|
Returns:
|
2021-01-01 18:31:15 +00:00
|
|
|
string: listing of preroll video paths to be used for Extras. CSV style: (;|,)
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
|
|
|
listing = ''
|
2021-01-21 02:54:47 +00:00
|
|
|
entries = schedule_types()
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-01 18:31:15 +00:00
|
|
|
# determine which date to build the listing for
|
2021-01-03 07:34:20 +00:00
|
|
|
if for_datetime:
|
|
|
|
if isinstance(for_datetime, datetime):
|
|
|
|
check_datetime = for_datetime
|
|
|
|
else:
|
|
|
|
check_datetime = datetime.combine(for_datetime, datetime.now().time())
|
2021-01-01 18:31:15 +00:00
|
|
|
else:
|
2021-01-03 07:34:20 +00:00
|
|
|
check_datetime = datetime.now()
|
2021-01-01 18:31:15 +00:00
|
|
|
|
|
|
|
# process the schedule for the given date
|
2021-01-01 16:12:22 +00:00
|
|
|
for entry in schedule:
|
2021-01-01 18:31:15 +00:00
|
|
|
try:
|
2021-01-21 02:54:47 +00:00
|
|
|
entry_start = entry.startdate
|
|
|
|
entry_end = entry.enddate
|
2021-01-03 07:34:20 +00:00
|
|
|
if not isinstance(entry_start, datetime):
|
|
|
|
entry_start = datetime.combine(entry_start, datetime.min.time())
|
|
|
|
if not isinstance(entry_end, datetime):
|
|
|
|
entry_end = datetime.combine(entry_end, datetime.max.time())
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
msg = 'checking "{}" against: "{}" - "{}"'.format(check_datetime,
|
|
|
|
entry_start,
|
|
|
|
entry_end)
|
2021-01-03 07:34:20 +00:00
|
|
|
logger.debug(msg)
|
|
|
|
|
|
|
|
if entry_start <= check_datetime <= entry_end:
|
2021-01-21 02:54:47 +00:00
|
|
|
entry_type = entry.type
|
|
|
|
entry_path = entry.path
|
2021-01-07 03:33:14 +00:00
|
|
|
entry_force = False
|
|
|
|
try:
|
2021-01-21 02:54:47 +00:00
|
|
|
entry_force = entry.force
|
2021-01-07 03:33:14 +00:00
|
|
|
except KeyError as ke:
|
|
|
|
# special case Optional, ignore
|
|
|
|
pass
|
|
|
|
|
|
|
|
msg = 'Check PASS: Using "{}" - "{}"'.format(entry_start, entry_end)
|
2021-01-03 07:34:20 +00:00
|
|
|
logger.debug(msg)
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
if entry_path:
|
2021-01-07 03:33:14 +00:00
|
|
|
found = False
|
|
|
|
# check new schedule item against exist list
|
|
|
|
for e in entries[entry_type]:
|
|
|
|
duration_new = duration_seconds(entry_start, entry_end)
|
2021-01-21 02:54:47 +00:00
|
|
|
duration_curr = duration_seconds(e.startdate, e.enddate)
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
# only the narrowest timeframe should stay
|
|
|
|
# disregard if a force entry is there
|
2021-01-21 02:54:47 +00:00
|
|
|
if duration_new < duration_curr and e.force != True:
|
2021-01-07 03:33:14 +00:00
|
|
|
entries[entry_type].remove(e)
|
2021-01-21 02:54:47 +00:00
|
|
|
|
|
|
|
found = True
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
# prep for use if New, or is a force Usage
|
|
|
|
if not found or entry_force == True:
|
|
|
|
entries[entry_type].append(entry)
|
2021-01-01 18:31:15 +00:00
|
|
|
except KeyError as ke:
|
2021-01-03 07:34:20 +00:00
|
|
|
msg = 'KeyError with entry "{}"'.format(entry)
|
2021-01-07 04:05:23 +00:00
|
|
|
logger.error(msg, exc_info=ke)
|
2021-01-07 03:33:14 +00:00
|
|
|
raise
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
# Build the merged output based or order of Priority
|
|
|
|
merged_list = []
|
|
|
|
if entries['misc']:
|
2021-01-07 03:33:14 +00:00
|
|
|
merged_list.extend([p.path for p in entries['misc']])
|
2021-01-01 16:12:22 +00:00
|
|
|
if entries['date_range']:
|
2021-01-07 03:33:14 +00:00
|
|
|
merged_list.extend([p.path for p in entries['date_range']])
|
2021-01-01 16:12:22 +00:00
|
|
|
if entries['weekly'] and not entries['date_range']:
|
2021-01-07 03:33:14 +00:00
|
|
|
merged_list.extend([p.path for p in entries['weekly']])
|
2021-01-01 16:12:22 +00:00
|
|
|
if entries['monthly'] \
|
|
|
|
and not entries['weekly'] and not entries['date_range']:
|
2021-01-07 03:33:14 +00:00
|
|
|
merged_list.extend([p.path for p in entries['monthly']])
|
2021-01-01 16:12:22 +00:00
|
|
|
if entries['default'] \
|
|
|
|
and not entries['monthly'] and not entries['weekly'] and not entries['date_range']:
|
2021-01-07 03:33:14 +00:00
|
|
|
merged_list.extend([p.path for p in entries['default']])
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
listing = build_listing_string(merged_list)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
return listing
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
def save_preroll_listing(plex: PlexServer, preroll_listing: Union[str, List[str]]) -> None:
|
2021-01-01 16:12:22 +00:00
|
|
|
"""Save Plex Preroll info to PlexServer settings
|
|
|
|
|
|
|
|
Args:
|
|
|
|
plex (PlexServer): Plex server to update
|
2021-01-07 03:33:14 +00:00
|
|
|
preroll_listing (str, list[str]): csv listing or List of preroll paths to save
|
2021-01-01 16:12:22 +00:00
|
|
|
"""
|
|
|
|
# if happend to send in an Iterable List, merge to a string
|
|
|
|
if type(preroll_listing) is list:
|
2021-01-21 02:54:47 +00:00
|
|
|
preroll_listing = build_listing_string(list(preroll_listing))
|
2021-01-07 03:33:14 +00:00
|
|
|
|
|
|
|
msg = 'Attempting save of pre-rolls: "{}"'.format(preroll_listing)
|
|
|
|
logger.debug(msg)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
plex.settings.get('cinemaTrailersPrerollID').set(preroll_listing)
|
|
|
|
plex.settings.save()
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
msg = 'Saved Pre-Rolls: Server: "{}" Pre-Rolls: "{}"'.format(plex.friendlyName,
|
|
|
|
preroll_listing)
|
2021-01-07 03:33:14 +00:00
|
|
|
logger.info(msg)
|
|
|
|
|
2021-01-01 16:12:22 +00:00
|
|
|
if __name__ == '__main__':
|
2021-01-21 02:54:47 +00:00
|
|
|
args = arguments()
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
plexutil.init_logger(args.log_config_file)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
cfg = plexutil.plex_config(args.config_file)
|
2021-01-01 16:12:22 +00:00
|
|
|
|
|
|
|
# Initialize Session information
|
|
|
|
sess = requests.Session()
|
|
|
|
# Ignore verifying the SSL certificate
|
|
|
|
sess.verify = False # '/path/to/certfile'
|
|
|
|
# If verify is set to a path of a directory (not a cert file),
|
|
|
|
# the directory needs to be processed with the c_rehash utility
|
|
|
|
# from OpenSSL.
|
|
|
|
if sess.verify is False:
|
|
|
|
# Disable the warning that the request is insecure, we know that...
|
|
|
|
import urllib3
|
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
|
|
|
|
try:
|
|
|
|
plex = PlexServer(cfg['PLEX_URL'], cfg['PLEX_TOKEN'], session=sess)
|
|
|
|
except Exception as e:
|
2021-01-03 07:34:20 +00:00
|
|
|
msg = 'Error connecting to Plex'
|
|
|
|
logger.error(msg, exc_info=e)
|
2021-01-01 16:12:22 +00:00
|
|
|
raise e
|
|
|
|
|
2021-01-21 02:54:47 +00:00
|
|
|
schedule = preroll_schedule(args.schedule_file)
|
|
|
|
prerolls = preroll_listing(schedule)
|
2021-01-01 18:31:15 +00:00
|
|
|
|
2021-01-07 03:33:14 +00:00
|
|
|
if args.do_test_run:
|
|
|
|
msg = 'Test Run of Plex Pre-Rolls: **Nothing being saved**\n{}\n'.format(prerolls)
|
|
|
|
logger.debug(msg)
|
|
|
|
print(msg)
|
|
|
|
else:
|
2021-01-21 02:54:47 +00:00
|
|
|
save_preroll_listing(plex, prerolls)
|