diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index 9922a48fb..564672db5 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -35,11 +35,6 @@ from get_args import args from queueconfig import notifications from pymediainfo import MediaInfo -# configure the cache - -# fixme: do this inside a setup routine -region.configure('dogpile.cache.memory') - def get_video(path, title, sceneName, use_scenename, use_mediainfo, providers=None, media_type="movie"): """ @@ -278,6 +273,9 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce if not saved_any: logging.debug('BAZARR No subtitles were found for this file: ' + path) return None + + subliminal.region.backend.sync() + logging.debug('BAZARR Ended searching subtitles for file: ' + path) @@ -370,6 +368,9 @@ def manual_search(path, language, hi, forced, providers, providers_auth, sceneNa final_subtitles = sorted(subtitles_list, key=lambda x: x['score'], reverse=True) logging.debug('BAZARR ' + str(len(final_subtitles)) + " subtitles have been found for this file: " + path) logging.debug('BAZARR Ended searching subtitles for this file: ' + path) + + subliminal.region.backend.sync() + return final_subtitles @@ -482,6 +483,9 @@ def manual_download_subtitle(path, language, hi, forced, subtitle, provider, pro "BAZARR Tried to manually download a subtitles for file: " + path + " but we weren't able to do (probably throttled by " + str( subtitle.provider_name) + ". Please retry later or select a subtitles from another provider.") return None + + subliminal.region.backend.sync() + logging.debug('BAZARR Ended manually downloading subtitles for file: ' + path) @@ -845,7 +849,7 @@ def refine_from_mediainfo(path, video): else: logging.debug('BAZARR MediaInfo library used is %s', exe) - media_info = MediaInfo.parse(path, library_file=exe); + media_info = MediaInfo.parse(path, library_file=exe) video_track = next((t for t in media_info.tracks if t.track_type == 'Video'), None) if not video_track: diff --git a/bazarr/init.py b/bazarr/init.py index b020e48e0..b57907036 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -13,6 +13,10 @@ from check_update import check_releases from get_args import args from utils import get_binary +from dogpile.cache.region import register_backend as register_cache_backend +import subliminal +import datetime + # set subliminal_patch user agent os.environ["SZ_USER_AGENT"] = "Bazarr/1" @@ -47,6 +51,15 @@ if not os.path.exists(os.path.join(args.config_dir, 'db')): if not os.path.exists(os.path.join(args.config_dir, 'log')): os.mkdir(os.path.join(args.config_dir, 'log')) logging.debug("BAZARR Created log folder") +if not os.path.exists(os.path.join(args.config_dir, 'cache')): + os.mkdir(os.path.join(args.config_dir, 'cache')) + logging.debug("BAZARR Created cache folder") + +# Configure dogpile file caching for Subliminal request +register_cache_backend("subzero.cache.file", "subzero.cache_backends.file", "SZFileBackend") +subliminal.region.configure('subzero.cache.file', expiration_time=datetime.timedelta(days=30), + arguments={'appname': "sz_cache", 'app_cache_dir': args.config_dir}) +subliminal.region.backend.sync() if not os.path.exists(os.path.join(args.config_dir, 'config', 'releases.txt')): check_releases() diff --git a/bazarr/scheduler.py b/bazarr/scheduler.py index b51aa2ce3..bb2bff681 100644 --- a/bazarr/scheduler.py +++ b/bazarr/scheduler.py @@ -5,6 +5,7 @@ from get_movies import update_movies from get_series import update_series from config import settings from get_subtitle import wanted_search_missing_subtitles, upgrade_subtitles +from utils import cache_maintenance from get_args import args if not args.no_update: from check_update import check_and_apply_update, check_releases @@ -121,6 +122,9 @@ if settings.general.getboolean('upgrade_subs') and (settings.general.getboolean( scheduler.add_job(upgrade_subtitles, IntervalTrigger(hours=12), max_instances=1, coalesce=True, misfire_grace_time=15, id='upgrade_subtitles', name='Upgrade previously downloaded subtitles') +scheduler.add_job(cache_maintenance, IntervalTrigger(hours=24), max_instances=1, coalesce=True, + misfire_grace_time=15, id='cache_cleanup', name='Cache maintenance') + schedule_update_job() sonarr_full_update() radarr_full_update() diff --git a/bazarr/utils.py b/bazarr/utils.py index b9440fffe..b149524c3 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -5,10 +5,15 @@ import sqlite3 import time import platform import sys +import logging from whichcraft import which from get_args import args +from subliminal import region as subliminal_cache_region +import datetime +import glob + def history_log(action, sonarrSeriesId, sonarrEpisodeId, description, video_path=None, language=None, provider=None, score=None, forced=False): @@ -69,3 +74,27 @@ def get_binary(name): if exe and os.path.isfile(exe): return exe + + +def cache_maintenance(): + main_cache_validity = 14 # days + pack_cache_validity = 4 # days + + logging.info("BAZARR Running cache maintenance") + now = datetime.datetime.now() + + def remove_expired(path, expiry): + mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path)) + if mtime + datetime.timedelta(days=expiry) < now: + try: + os.remove(path) + except (IOError, OSError): + logging.debug("Couldn't remove cache file: %s", os.path.basename(path)) + + # main cache + for fn in subliminal_cache_region.backend.all_filenames: + remove_expired(fn, main_cache_validity) + + # archive cache + for fn in glob.iglob(os.path.join(args.config_dir, "*.archive")): + remove_expired(fn, pack_cache_validity) \ No newline at end of file