Skip to content

Commit

Permalink
Feature/scheduling (#4)
Browse files Browse the repository at this point in the history
* wrap main functionality in a function so it can be called on a timer later

* add scheduling with crontab

* add envs to tune scheduling behavior

* update main execution behavior with new options

* update ignores

* fix some indentation issues
  • Loading branch information
Mike Zrimsek authored Feb 27, 2024
1 parent 10f15e3 commit 06f83dd
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
config.yaml
config.yaml
__pycache__
2 changes: 1 addition & 1 deletion add_kermode_intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from utils import load_app_config, find_movie

def update_mark_kermode_reviews_intros(app_config: dict):
'''Downloads BFI 'Mark Kermode Reviews' video to related films'''
'''Downloads BFI 'Mark Kermode Reviews' video to related films'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
2 changes: 1 addition & 1 deletion add_tcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from utils import load_app_config, find_movie

def update_turner_classic_movies_extras(app_config: dict):
'''Downloads Turner classic movies intros and outros'''
'''Downloads Turner classic movies intros and outros'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ services:

- DISABLE_TV_YEAR_CHECK=${DISABLE_TV_YEAR_CHECK:-false}

- SCHEDULING_ENABLED=${SCHEDULING_ENABLED:-true}
- SCHEDULING_CRONTAB=${SCHEDULING_CRONTAB:-0 6 * * *}
- SCHEDULING_TIMEZONE=${SCHEDULING_TIMEZONE:-America/New_York}
- RUN_SCHEDULED_TASK_IMMEDIATELY=${RUN_SCHEDULED_TASK_IMMEDIATELY:-true}

- DO_KERMODE_INTROS=${DO_KERMODE_INTROS:-false}
- DO_KERMODE_LISTS=${DO_KERMODE_LISTS:-false}
- DO_TURNER_CLASSIC_MOVIE_EXTRAS=${DO_TURNER_CLASSIC_MOVIE_EXTRAS:-false}
Expand Down
10 changes: 10 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ JELLYFIN_MOVIES_DIR=/movies
# Flag which disables the checking of years for tv shows - can help if some get missed
DISABLE_TV_YEAR_CHECK=false

# scheduling config settings
# Toggles Scheduling
SCHEDULING_ENABLED=true
# Scheduling configuration
SCHEDULING_CRONTAB=0 6 * * *
# Scheduling timezone
SCHEDULING_TIMEZONE=Etc/UTC
# Enable to fire off the scripts immediately on start in addition to on the scheduled task - has no effect if scheduling disabled
RUN_SCHEDULED_TASK_IMMEDIATELY=true

# flags for different tasks to fire off
# whether to run add_kermode_intro.py
DO_KERMODE_INTROS=false
Expand Down
2 changes: 1 addition & 1 deletion imdb_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from utils import load_app_config, request_repeat_get, request_repeat_post, find_collection_with_name_or_create, get_all_collections

def update_imdb_chart_collections(app_config: dict):
'''Make collections from IMDB charts'''
'''Make collections from IMDB charts'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
2 changes: 1 addition & 1 deletion imdb_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utils import load_app_config, request_repeat_get, request_repeat_post, find_collection_with_name_or_create, get_all_collections

def update_imdb_list_collections(app_config: dict):
'''Make collections from IMDB lists'''
'''Make collections from IMDB lists'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
2 changes: 1 addition & 1 deletion kermode_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from utils import load_app_config, find_collection_with_name_or_create, get_all_collections

def update_mark_kermode_reviews_collection(app_config: dict):
'''Adds videos with have a BFI 'Mark Kermode Reviews' video to a collection'''
'''Adds videos with have a BFI 'Mark Kermode Reviews' video to a collection'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
2 changes: 1 addition & 1 deletion letterboxd_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utils import load_app_config, request_repeat_get, request_repeat_post, find_collection_with_name_or_create, get_all_collections

def update_letterboxd_list_collections(app_config: dict):
'''Make collections from Letterboxd lists'''
'''Make collections from Letterboxd lists'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
74 changes: 48 additions & 26 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from apscheduler.schedulers.blocking import BlockingScheduler

from utils import load_app_config

from add_kermode_intro import update_mark_kermode_reviews_intros
from add_tcm import update_turner_classic_movies_extras
from imdb_chart import update_imdb_chart_collections
Expand All @@ -7,37 +10,56 @@
from letterboxd_list import update_letterboxd_list_collections
from tspdt_list import update_top_1000_movies_collection

app_config = load_app_config()
do_kermode_intros = app_config["do_kermode_intros"]
do_kermode_lists = app_config["do_kermode_lists"]
do_turner_classic_movie_extras = app_config["do_turner_classic_movie_extras"]
do_top_1000_movies_list = app_config["do_top_1000_movies_list"]
do_imdb_charts = app_config["do_imdb_charts"]
do_imdb_lists = app_config["do_imdb_lists"]
do_letterboxd_lists = app_config["do_letterboxd_lists"]
def execute_collection_scripts(app_config: dict = None):
do_kermode_intros = app_config["do_kermode_intros"]
do_kermode_lists = app_config["do_kermode_lists"]
do_turner_classic_movie_extras = app_config["do_turner_classic_movie_extras"]
do_top_1000_movies_list = app_config["do_top_1000_movies_list"]
do_imdb_charts = app_config["do_imdb_charts"]
do_imdb_lists = app_config["do_imdb_lists"]
do_letterboxd_lists = app_config["do_letterboxd_lists"]

all_disabled = not (do_kermode_intros or do_kermode_lists or do_turner_classic_movie_extras or do_top_1000_movies_list or do_imdb_charts or do_imdb_lists or do_letterboxd_lists)
if all_disabled:
print("All tasks are disabled. Enable at least one with environment settings. Please ensure config.yaml is populated as needed.")
exit()
all_disabled = not (do_kermode_intros or do_kermode_lists or do_turner_classic_movie_extras or do_top_1000_movies_list or do_imdb_charts or do_imdb_lists or do_letterboxd_lists)
if all_disabled:
print("All tasks are disabled. Enable at least one with environment settings. Please ensure config.yaml is populated as needed.")
print()
exit()

if do_kermode_intros:
update_mark_kermode_reviews_intros(app_config)
if do_kermode_intros:
update_mark_kermode_reviews_intros(app_config)

if do_kermode_lists:
update_mark_kermode_reviews_collection(app_config)
if do_kermode_lists:
update_mark_kermode_reviews_collection(app_config)

if do_turner_classic_movie_extras:
update_turner_classic_movies_extras(app_config)
if do_turner_classic_movie_extras:
update_turner_classic_movies_extras(app_config)

if do_top_1000_movies_list:
update_top_1000_movies_collection(app_config)
if do_top_1000_movies_list:
update_top_1000_movies_collection(app_config)

if do_imdb_charts:
update_imdb_chart_collections(app_config)
if do_imdb_charts:
update_imdb_chart_collections(app_config)

if do_imdb_lists:
update_imdb_list_collections(app_config)
if do_imdb_lists:
update_imdb_list_collections(app_config)

if do_letterboxd_lists:
update_letterboxd_list_collections(app_config)
if do_letterboxd_lists:
update_letterboxd_list_collections(app_config)

if __name__ == "__main__":
app_config = load_app_config()
scheduling_enabled = app_config["scheduling_enabled"]
scheduling_crontab = app_config["scheduling_crontab"]
scheduling_timezone = app_config["scheduling_timezone"]
run_scheduled_task_immediately = app_config["run_scheduled_task_immediately"]

if scheduling_enabled:
if run_scheduled_task_immediately:
execute_collection_scripts(app_config)

scheduler = BlockingScheduler()

scheduler.add_job(func=execute_collection_scripts, trigger=CronTrigger.from_crontab(scheduling_crontab), timezone=scheduling_timezone, args=[app_config])
else:
execute_collection_scripts(app_config)
exit()
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
requests
lxml
simplejson
python-dotenv
pyyaml
pyyaml
apscheduler
2 changes: 1 addition & 1 deletion tspdt_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from utils import load_app_config, find_collection_with_name_or_create, get_all_collections

def update_top_1000_movies_collection(app_config: dict):
'''Gets top 1000 movies from the TSPDT masterlist'''
'''Gets top 1000 movies from the TSPDT masterlist'''
server_url = app_config["server_url"]
api_key= app_config["api_key"]
user_id = app_config["user_id"]
Expand Down
6 changes: 6 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def load_env_config():

config_dict["disable_tv_year_filter"] = get_env_variable("DISABLE_TV_YEAR_CHECK", default_value=False)

is_scheduling_enabled = get_env_variable("SCHEDULING_ENABLED", default_value=False)
config_dict["scheduling_enabled"] = is_scheduling_enabled
config_dict["scheduling_crontab"] = get_env_variable("SCHEDULING_CRONTAB") if is_scheduling_enabled else ""
config_dict["scheduling_timezone"] = get_env_variable("SCHEDULING_TIMEZONE") if is_scheduling_enabled else ""
config_dict["run_scheduled_task_immediately"] = get_env_variable("RUN_SCHEDULED_TASK_IMMEDIATELY", default_value=False)

config_dict["do_kermode_intros"] = get_env_variable("DO_KERMODE_INTROS", default_value=False)
config_dict["do_kermode_lists"] = get_env_variable("DO_KERMODE_LIST", default_value=False)
config_dict["do_turner_classic_movie_extras"] = get_env_variable("DO_TURNER_CLASSIC_MOVIE_EXTRAS", default_value=False)
Expand Down

0 comments on commit 06f83dd

Please sign in to comment.