Skip to content

Commit 1616d91

Browse files
committed
- Rewrite nearly the whole thing for better parsing, support, readability, etc. (OOP forever!)
- Consolidate Plex API config with schedules, now in config.yaml - Changes to schema for schedules - No more priority of, e.g. date_range over monthly - No more default - `misc` reworked to `always` - Update Docker files - Update README - Delete old files, linting stuff (screw mypy)
1 parent 7a8f97b commit 1616d91

25 files changed

+1232
-1500
lines changed

.pylintrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

Dockerfile

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,8 @@ COPY requirements.txt requirements.txt
1212
# Install Python requirements
1313
RUN pip3 install --no-cache-dir -r requirements.txt
1414

15-
# Make Docker /config volume for optional config file
16-
VOLUME /config
17-
18-
# Copy logging.conf file from build machine to Docker /config folder
19-
COPY logging.conf /config/
20-
21-
# Copy example config file from build machine to Docker /config folder
22-
# Also copies any existing config.ini file from build machine to Docker /config folder, (will cause the bot to use the existing config file if it exists)
23-
COPY config.ini* /config/
24-
25-
# Copy example schedule file from build machine to Docker /config folder
26-
# Also copies any existing schedules.yaml file from build machine to Docker /config folder, (will cause the bot to use the existing schedule file if it exists)
27-
COPY schedules.yaml* /config/
15+
# Copy config file from build machine to Docker /config folder
16+
COPY config.yaml /
2817

2918
# Make Docker /logs volume for log file
3019
VOLUME /logs

README.md

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,35 @@ Install Python requirements:
3232
pip install -r requirements.txt
3333
```
3434

35-
Copy `config.ini.sample` to `config.ini` and complete the `[auth]` section with your Plex server information.
36-
37-
Copy `schedules.yaml.sample` to `schedules.yaml` and [edit your schedule](#schedule-rules).
35+
Copy `config.yaml.example` to `config.yaml`, provide your `plex` details and [edit your schedule](#schedule-rules).
3836

3937
Run the script:
4038

4139
```sh
42-
python schedule_preroll.py
40+
python run.py
4341
```
4442

4543
#### Advanced Usage
4644

4745
```sh
48-
$ python schedule_preroll.py -h
46+
$ python run.py -h
4947

50-
usage: schedule_preroll.py [-h] [-v] [-l LOG_CONFIG_FILE] [-c CONFIG_FILE] [-s SCHEDULE_FILE]
48+
usage: run.py [-h] [-c CONFIG] [-l LOG] [-d]
5149

52-
Automate scheduling of pre-roll intros for Plex
50+
Plex Prerolls - A tool to manage prerolls for Plex
5351

54-
optional arguments:
52+
options:
5553
-h, --help show this help message and exit
56-
-v, --version show the version number and exit
57-
-lc LOG_CONFIG_FILE, --logconfig-path LOG_CONFIG_FILE
58-
Path to logging config file. [Default: ./logging.conf]
59-
-c CONFIG_FILE, --config-path CONFIG_FILE
60-
Path to Config.ini to use for Plex Server info. [Default: ./config.ini]
61-
-s SCHEDULE_FILE, --schedule-path SCHEDULE_FILE
62-
Path to pre-roll schedule file (YAML) to be use. [Default: ./schedules.yaml]
54+
-c CONFIG, --config CONFIG
55+
Path to config file. Defaults to 'config.yaml'
56+
-l LOG, --log LOG Log file directory. Defaults to 'logs/'
57+
-d, --dry-run Dry run, no real changes made
6358
```
6459

6560
##### Example
6661

6762
```sh
68-
python schedule_preroll.py \
69-
-c path/to/custom/config.ini \
70-
-s path/to/custom/schedules.yaml \
71-
-lc path/to/custom/logger.conf
63+
python run.py -c path/to/custom/config.yaml -l path/to/custom/log/directory/ # Trailing slash required
7264
```
7365

7466
### Run as Docker Container
@@ -94,18 +86,18 @@ docker run -d \
9486
-e PGID=1000 \
9587
-e TZ=Etc/UTC \
9688
-e CRON_SCHEDULE="0 0 * * *" \
97-
-v /path/to/config:/config \
89+
-v /path/to/config:/ \
9890
-v /path/to/logs:/logs \
9991
--restart unless-stopped \
10092
nwithan8/plex_prerolls:latest
10193
```
10294

10395
#### Paths and Environment Variables
10496

105-
| Path | Description |
106-
|-----------|--------------------------------------------------------------------------------------|
107-
| `/config` | Path to config files (`config.ini` and `schedules.yaml` should be in this directory) |
108-
| `/logs` | Path to log files (`schedule_preroll.log` will be in this directory) |
97+
| Path | Description |
98+
|---------|-------------------------------------------------------------------|
99+
| `/` | Path to config files (`config.yaml` should be in this directory) |
100+
| `/logs` | Path to log files (`Plex Prerolls.log` will be in this directory) |
109101

110102
| Environment Variable | Description |
111103
|----------------------|-------------------------------------------------------------------|
@@ -118,27 +110,28 @@ docker run -d \
118110

119111
## Schedule Rules
120112

121-
Schedules follow the following priority:
122-
1. **misc**: Items listed in `always_use` will always be included (appended) to the preroll list
123-
- If you have a large set of prerolls, you can provide all paths and use `random_count` to randomly select a smaller subset of the list to use on each run.
113+
Any entry whose schedule falls within the current date/time at the time of execution will be added to the preroll.
124114

125-
2. **date_range**: Schedule based on a specific date/time range
115+
You can define as many schedules as you want, in the following categories (order does not matter):
126116

127-
3. **weekly**: Schedule based on a specific week of the year
117+
1. **always**: Items listed here will always be included (appended) to the preroll list
118+
- If you have a large set of prerolls, you can provide all paths and use `random_count` to randomly select a smaller
119+
subset of the list to use on each run.
128120

129-
4. **monthly**: Schedule based on a specific month of the year
121+
2. **date_range**: Schedule based on a specific date/time range (including [wildcards](#date-range-section-scheduling))
130122

131-
5. **default**: Default item to use if none of the above apply
123+
3. **weekly**: Schedule based on a specific week of the year
132124

133-
For any conflicting schedules, the script tries to find the closest matching range and highest priority.
125+
4. **monthly**: Schedule based on a specific month of the year
134126

135127
### Advanced Scheduling
136128

137129
#### Date Range Section Scheduling
138130

139131
`date_range` entries can accept both dates (`yyyy-mm-dd`) and datetimes (`yyyy-mm-dd hh:mm:ss`, 24-hour time).
140132

141-
`date_range` entries can also accept wildcards for any of the date/time fields. This can be useful for scheduling recurring events, such as annual events, "first-of-the-month" events, or even hourly events.
133+
`date_range` entries can also accept wildcards for any of the date/time fields. This can be useful for scheduling
134+
recurring events, such as annual events, "first-of-the-month" events, or even hourly events.
142135

143136
```yaml
144137
date_range:
@@ -147,31 +140,46 @@ date_range:
147140
# Each entry requires start_date, end_date, path values
148141
- start_date: 2020-01-01 # Jan 1st, 2020
149142
end_date: 2020-01-02 # Jan 2nd, 2020
150-
path: /path/to/video.mp4
143+
paths:
144+
- /path/to/video.mp4
145+
- /path/to/another/video.mp4
151146
weight: 2 # Add these paths to the list twice (make up greater percentage of prerolls - more likely to be selected)
152147
- start_date: xxxx-07-04 # Every year on July 4th
153148
end_date: xxxx-07-04 # Every year on July 4th
154-
path: /path/to/video.mp4
149+
paths:
150+
- /path/to/video.mp4
151+
- /path/to/another/video.mp4
155152
weight: 1
156-
- start_date: xxxx-xx-02 # Every year on the 2nd of every month
153+
- name: "My Schedule" # Optional name for logging purposes
154+
start_date: xxxx-xx-02 # Every year on the 2nd of every month
157155
end_date: xxxx-xx-03 # Every year on the 3rd of every month
158-
path: /path/to/video.mp4
156+
paths:
157+
- /path/to/video.mp4
158+
- /path/to/another/video.mp4
159159
weight: 1
160160
- start_date: xxxx-xx-xx 08:00:00 # Every day at 8am
161161
end_date: xxxx-xx-xx 09:30:00 # Every day at 9:30am
162-
path: /path/to/holiday_video.mp4
162+
paths:
163+
- /path/to/video.mp4
164+
- /path/to/another/video.mp4
163165
weight: 1
164166
```
165167
166168
You should [adjust your cron schedule](#scheduling-script) to run the script more frequently if you use this feature.
167169
168-
`date_range` entries also accept an optional `weight` value that can be used to adjust the emphasis of this entry over others by adding the listed paths multiple times. Since Plex selects a random preroll from the list of paths, having the same path listed multiple times increases its chances of being selected over paths that only appear once. This allows you to combine, e.g. a `date_range` entry with a `misc` entry, but place more weight/emphasis on the `date_range` entry.
170+
`date_range` entries also accept an optional `weight` value that can be used to adjust the emphasis of this entry over
171+
others by adding the listed paths multiple times. Since Plex selects a random preroll from the list of paths, having the
172+
same path listed multiple times increases its chances of being selected over paths that only appear once. This allows
173+
you to combine, e.g. a `date_range` entry with a `misc` entry, but place more weight/emphasis on the `date_range` entry.
174+
175+
`date_range` entries also accept an optional `name` value that can be used to identify the schedule in the logs.
169176

170177
---
171178

172179
## Scheduling Script
173180

174-
**NOTE:** Scheduling is handled automatically in the Docker version of this script via the `CRON_SCHEDULE` environment variable.
181+
**NOTE:** Scheduling is handled automatically in the Docker version of this script via the `CRON_SCHEDULE` environment
182+
variable.
175183

176184
### Linux
177185

@@ -184,13 +192,14 @@ crontab -e
184192
Place desired schedule (example below for every day at midnight)
185193

186194
```sh
187-
0 0 * * * python /path/to/schedule_preroll.py >/dev/null 2>&1
195+
0 0 * * * python /path/to/run.py >/dev/null 2>&1
188196
```
189197

190-
You can also wrap the execution in a shell script (useful if running other scripts/commands, using venv encapsulation, customizing arguments, etc.)
198+
You can also wrap the execution in a shell script (useful if running other scripts/commands, using venv encapsulation,
199+
customizing arguments, etc.)
191200

192201
```sh
193-
0 0 * * * /path/to/schedule_preroll.sh >/dev/null 2>&1
202+
0 0 * * * /path/to/run_prerolls.sh >/dev/null 2>&1
194203
```
195204

196205
Schedule as frequently as needed for your schedule (ex: hourly, daily, weekly, etc.)

config.ini.sample

Lines changed: 0 additions & 7 deletions
This file was deleted.

config.yaml.example

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# All keys must be in lowercase
2+
# All paths will be case-sensitive based on your environment (Linux, Windows)
3+
4+
plex:
5+
url: http://localhost:32400 # URL to your Plex server
6+
token: thisismyplextoken # Your Plex token
7+
8+
# Always include these pre-rolls
9+
always:
10+
enabled: true
11+
paths:
12+
- "path/to/video1.mp4"
13+
- "path/to/video2.mp4"
14+
- "path/to/video3.mp4"
15+
count: 10 # Optional, randomly select X many videos from the list rather than all of them
16+
weight: 1 # Optional, how much to emphasize these pre-rolls over others (higher = more likely to play)
17+
18+
# Schedule prerolls by date and time frames
19+
date_range:
20+
enabled: true
21+
ranges:
22+
- name: "New Years" # Optional name for logging purposes
23+
start_date: 2020-01-01 # Jan 1st, 2020
24+
end_date: 2020-01-02 # Jan 2nd, 2020
25+
paths:
26+
- "path/to/video1.mp4"
27+
- "path/to/video2.mp4"
28+
- "path/to/video3.mp4"
29+
weight: 2 # Optional, add these paths to the list twice (make up greater percentage of prerolls - more likely to be selected)
30+
- start_date: xxxx-07-04 # Every year on July 4th
31+
end_date: xxxx-07-04 # Every year on July 4th
32+
paths:
33+
- "path/to/video1.mp4"
34+
- "path/to/video2.mp4"
35+
- "path/to/video3.mp4"
36+
- start_date: xxxx-xx-02 # Every year on the 2nd of every month
37+
end_date: xxxx-xx-03 # Every year on the 3rd of every month
38+
paths:
39+
- "path/to/video1.mp4"
40+
- "path/to/video2.mp4"
41+
- "path/to/video3.mp4"
42+
- start_date: xxxx-xx-xx 08:00:00 # Every day at 8am
43+
end_date: xxxx-xx-xx 09:30:00 # Every day at 9:30am
44+
paths:
45+
- "path/to/video1.mp4"
46+
- "path/to/video2.mp4"
47+
- "path/to/video3.mp4"
48+
49+
# Schedule prerolls by week of the year
50+
weekly:
51+
enabled: false
52+
weeks:
53+
- number: 1 # First week of the year
54+
paths:
55+
- "path/to/video1.mp4"
56+
- "path/to/video2.mp4"
57+
- "path/to/video3.mp4"
58+
weight: 1 # Optional, how much to emphasize these pre-rolls over others (higher = more likely to play)
59+
- number: 2 # Second week of the year
60+
paths:
61+
- "path/to/video1.mp4"
62+
- "path/to/video2.mp4"
63+
- "path/to/video3.mp4"
64+
65+
# Schedule prerolls by month of the year
66+
monthly:
67+
enabled: false
68+
months:
69+
- number: 1 # January
70+
paths:
71+
- "path/to/video1.mp4"
72+
- "path/to/video2.mp4"
73+
- "path/to/video3.mp4"
74+
weight: 1 # Optional, how much to emphasize these pre-rolls over others (higher = more likely to play)
75+
- number: 2 # February
76+
paths:
77+
- "path/to/video1.mp4"
78+
- "path/to/video2.mp4"
79+
- "path/to/video3.mp4"

consts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
APP_NAME = "Plex Prerolls"
2+
APP_DESCRIPTION = "A tool to manage prerolls for Plex"
3+
DEFAULT_CONFIG_PATH = "config.yaml"
4+
DEFAULT_LOG_DIR = "logs/"
5+
CONSOLE_LOG_LEVEL = "INFO"
6+
FILE_LOG_LEVEL = "DEBUG"

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ services:
33
plex_prerolls:
44
image: nwithan8/plex_prerolls:latest
55
volumes:
6-
- /path/to/config:/config
6+
- /path/to/config:/
77
- /path/to/logs:/logs
88
environment:
99
CRON_SCHEDULE: "0 0 * * *" # Run every day at midnight, see https://crontab.guru/ for help
10+
DRY_RUN: "false" # Set to true to test without actually downloading
1011
TZ: America/New_York

entrypoint.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ mkdir -p /etc/cron.d
66
# Read cron schedule from environment variable
77
CRON_SCHEDULE=${CRON_SCHEDULE:-"0 0 * * *"} # Default to midnight every day if not supplied
88

9+
# Add "--dry-run" flag if DRY_RUN is set to true
10+
if [ "$DRY_RUN" = "true" ]; then
11+
DRY_RUN_FLAG="--dry-run"
12+
else
13+
DRY_RUN_FLAG=""
14+
fi
15+
16+
# DRY_RUN_FLAG="--dry-run"
17+
918
# Schedule cron job with supplied cron schedule
10-
echo "$CRON_SCHEDULE python3 /schedule_preroll.py -c /config/config.ini -s /config/schedules.yaml -lc /config/logging.conf > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/cron.d/schedule_preroll
19+
echo "$CRON_SCHEDULE python3 /run.py -c /config.yaml -l /logs $DRY_RUN_FLAG > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/cron.d/schedule_preroll
1120

1221
# Give execution rights on the cron job
1322
chmod 0644 /etc/cron.d/schedule_preroll

logging.conf

Lines changed: 0 additions & 36 deletions
This file was deleted.

modules/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)