bazarr/bazarr/utils.py

101 lines
3.5 KiB
Python
Raw Normal View History

# coding=utf-8
import os
import sqlite3
import time
import platform
2019-07-11 12:31:52 +00:00
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
2019-06-11 18:45:48 +00:00
def history_log(action, sonarrSeriesId, sonarrEpisodeId, description, video_path=None, language=None, provider=None,
score=None, forced=False):
# Open database connection
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c = db.cursor()
2019-06-16 19:24:47 +00:00
history = c.execute(
'''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(action, sonarrSeriesId, sonarrEpisodeId, time.time(), description, video_path, language, provider, score))
2019-06-16 19:24:47 +00:00
# Commit changes to DB
db.commit()
2019-06-16 19:24:47 +00:00
# Close database connection
db.close()
2019-06-11 18:45:48 +00:00
def history_log_movie(action, radarrId, description, video_path=None, language=None, provider=None, score=None,
forced=False):
# Open database connection
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c = db.cursor()
2019-06-16 19:24:47 +00:00
history = c.execute(
'''INSERT INTO table_history_movie(action, radarrId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
(action, radarrId, time.time(), description, video_path, language, provider, score))
2019-06-16 19:24:47 +00:00
# Commit changes to DB
db.commit()
2019-06-16 19:24:47 +00:00
# Close database connection
db.close()
2019-06-11 18:45:48 +00:00
def get_binary(name):
binaries_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'bin'))
2019-06-16 19:24:47 +00:00
exe = None
2019-08-13 17:46:29 +00:00
if name != 'mediainfo':
installed_exe = which(name)
2019-06-16 19:24:47 +00:00
2019-08-13 17:46:29 +00:00
if name != 'mediainfo' and installed_exe and os.path.isfile(installed_exe):
return installed_exe
else:
2019-06-11 18:45:48 +00:00
if platform.system() == "Windows": # Windows
exe = os.path.abspath(os.path.join(binaries_dir, "Windows", "i386", name, "%s.exe" % name))
if exe and not os.path.isfile(exe):
2019-07-11 12:31:52 +00:00
if sys.maxsize > 2**32: # is 64bits Python
exe = os.path.abspath(os.path.join(binaries_dir, "Windows", "x86_64", name, "%s.dll" % name))
else: # is 32bits Python
exe = os.path.abspath(os.path.join(binaries_dir, "Windows", "i386", name, "%s.dll" % name))
2019-06-16 19:24:47 +00:00
2019-06-11 18:45:48 +00:00
elif platform.system() == "Darwin": # MacOSX
exe = os.path.abspath(os.path.join(binaries_dir, "MacOSX", "i386", name, name))
2019-06-16 19:24:47 +00:00
2019-06-11 18:45:48 +00:00
elif platform.system() == "Linux": # Linux
exe = os.path.abspath(os.path.join(binaries_dir, "Linux", platform.machine(), name, name))
2019-06-16 19:24:47 +00:00
if exe and os.path.isfile(exe):
2019-06-11 18:45:48 +00:00
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)