diff --git a/bazarr.py b/bazarr.py index 30b147f4d..07be970cd 100644 --- a/bazarr.py +++ b/bazarr.py @@ -11,6 +11,8 @@ import itertools import operator import requests import pycountry +import pretty +import datetime from PIL import Image from io import BytesIO from fdsend import send_file @@ -27,6 +29,7 @@ from get_sonarr_settings import * from list_subtitles import * from get_subtitle import * from utils import * +from scheduler import * import logging from logging.handlers import TimedRotatingFileHandler @@ -223,16 +226,7 @@ def wanted(): def wanted_search_missing_subtitles(): ref = request.environ['HTTP_REFERER'] - db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) - db.create_function("path_substitution", 1, path_replace) - c = db.cursor() - - c.execute("SELECT path_substitution(path) FROM table_episodes WHERE table_episodes.missing_subtitles != '[]'") - data = c.fetchall() - c.close() - - for episode in data: - wanted_download_subtitles(episode[0]) + wanted_search_missing_subtitles() redirect(ref) @@ -304,8 +298,12 @@ def system(): logs = [] for line in reversed(open(os.path.join(os.path.dirname(__file__), 'data/log/bazarr.log')).readlines()): logs.append(line.rstrip()) + + task_list = [] + for job in scheduler.get_jobs(): + task_list.append([job.name, job.trigger.interval.__str__(), pretty.date(job.next_run_time.replace(tzinfo=None))]) - return template('system', tasks=tasks, logs=logs, base_url=base_url) + return template('system', tasks=tasks, logs=logs, base_url=base_url, task_list=task_list) @route(base_url + '/remove_subtitles', method='POST') def remove_subtitles(): diff --git a/get_subtitle.py b/get_subtitle.py index 18e7f8313..18e2c7baf 100644 --- a/get_subtitle.py +++ b/get_subtitle.py @@ -65,3 +65,14 @@ def wanted_download_subtitles(path): list_missing_subtitles(episode[3]) history_log(1, episode[3], episode[2], message) +def wanted_search_missing_subtitles(): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) + db.create_function("path_substitution", 1, path_replace) + c = db.cursor() + + c.execute("SELECT path_substitution(path) FROM table_episodes WHERE table_episodes.missing_subtitles != '[]'") + data = c.fetchall() + c.close() + + for episode in data: + wanted_download_subtitles(episode[0]) diff --git a/requirements.txt b/requirements.txt index 91abafe8f..ac2749d18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +apscheduler babelfish bottle bottle-fdsend diff --git a/scheduler.py b/scheduler.py index 87b16683a..506f13baf 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,12 +1,11 @@ -import datetime, threading, time +from get_series import * +from get_episodes import * +from get_subtitle import * -def foo(): - next_call = time.time() - while True: - print datetime.datetime.now() - next_call = next_call+1; - time.sleep(next_call - time.time()) +from apscheduler.schedulers.background import BackgroundScheduler -timerThread = threading.Thread(target=foo) -timerThread.daemon = True -timerThread.start() +scheduler = BackgroundScheduler() +scheduler.add_job(update_series, 'interval', minutes=1, id='update_series', name='Update series list from Sonarr') +scheduler.add_job(add_new_episodes, 'interval', minutes=1, id='add_new_episodes', name='Add new episodes from Sonarr') +scheduler.add_job(wanted_search_missing_subtitles, 'interval', minutes=15, id='wanted_search_missing_subtitles', name='Search for wanted subtitles') +scheduler.start() diff --git a/views/system.tpl b/views/system.tpl index 31695a950..8765bd7dc 100644 --- a/views/system.tpl +++ b/views/system.tpl @@ -78,7 +78,26 @@ About
- Tasks +
+ + + + + + + + + + %for task in task_list: + + + + + + %end + +
NameIntervalNext Execution
{{task[0]}}{{task[1]}}{{task[2]}}
+