bazarr/bazarr/utils.py

130 lines
4.8 KiB
Python
Raw Normal View History

# coding=utf-8
import os
import time
import platform
import logging
import requests
from whichcraft import which
from get_args import args
from config import settings, url_sonarr, url_radarr
2020-02-12 17:41:40 +00:00
from database import database
from websocket_handler import event_stream
from subliminal import region as subliminal_cache_region
import datetime
import glob
2020-01-30 23:03:35 +00:00
def history_log(action, sonarr_series_id, sonarr_episode_id, description, video_path=None, language=None, provider=None,
score=None):
2019-10-22 02:13:37 +00:00
database.execute("INSERT INTO table_history (action, sonarrSeriesId, sonarrEpisodeId, timestamp, description,"
2020-01-30 23:03:35 +00:00
"video_path, language, provider, score) VALUES (?,?,?,?,?,?,?,?,?)",
(action, sonarr_series_id, sonarr_episode_id, time.time(), description, video_path, language,
provider, score))
2020-02-12 17:41:40 +00:00
event_stream.write(type='episodeHistory')
2020-01-30 23:03:35 +00:00
def history_log_movie(action, radarr_id, description, video_path=None, language=None, provider=None, score=None):
2019-10-22 02:13:37 +00:00
database.execute("INSERT INTO table_history_movie (action, radarrId, timestamp, description, video_path, language, "
2020-01-30 23:03:35 +00:00
"provider, score) VALUES (?,?,?,?,?,?,?,?)",
(action, radarr_id, time.time(), description, video_path, language, provider, score))
2020-02-12 17:41:40 +00:00
event_stream.write(type='movieHistory')
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-26 02:10:31 +00:00
installed_exe = which(name)
2019-06-16 19:24:47 +00:00
2019-08-26 02:10:31 +00:00
if 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))
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-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)
def get_sonarr_version():
sonarr_version = ''
2020-01-30 23:03:35 +00:00
if settings.general.getboolean('use_sonarr'):
try:
2020-01-30 23:03:35 +00:00
sv = url_sonarr() + "/api/system/status?apikey=" + settings.sonarr.apikey
sonarr_version = requests.get(sv, timeout=60, verify=False).json()['version']
2020-01-30 23:03:35 +00:00
except Exception:
2019-10-25 13:48:22 +00:00
logging.debug('BAZARR cannot get Sonarr version')
return sonarr_version
2019-10-09 04:08:39 +00:00
def get_sonarr_platform():
sonarr_platform = ''
2020-01-30 23:03:35 +00:00
if settings.general.getboolean('use_sonarr'):
2019-10-09 04:08:39 +00:00
try:
2020-01-30 23:03:35 +00:00
sv = url_sonarr() + "/api/system/status?apikey=" + settings.sonarr.apikey
response = requests.get(sv, timeout=60, verify=False).json()
if response['isLinux'] or response['isOsx']:
2019-10-09 04:08:39 +00:00
sonarr_platform = 'posix'
2020-01-30 23:03:35 +00:00
elif response['isWindows']:
2019-10-09 04:08:39 +00:00
sonarr_platform = 'nt'
2020-01-30 23:03:35 +00:00
except Exception:
logging.debug('BAZARR cannot get Sonarr platform')
2019-10-09 04:08:39 +00:00
return sonarr_platform
def get_radarr_version():
radarr_version = ''
2020-01-30 23:03:35 +00:00
if settings.general.getboolean('use_radarr'):
try:
2020-01-30 23:03:35 +00:00
rv = url_radarr() + "/api/system/status?apikey=" + settings.radarr.apikey
radarr_version = requests.get(rv, timeout=60, verify=False).json()['version']
2020-01-30 23:03:35 +00:00
except Exception:
2019-10-25 13:48:22 +00:00
logging.debug('BAZARR cannot get Radarr version')
return radarr_version
2019-10-09 04:08:39 +00:00
def get_radarr_platform():
radarr_platform = ''
2020-01-30 23:03:35 +00:00
if settings.general.getboolean('use_radarr'):
2019-10-09 04:08:39 +00:00
try:
2020-01-30 23:03:35 +00:00
rv = url_radarr() + "/api/system/status?apikey=" + settings.radarr.apikey
response = requests.get(rv, timeout=60, verify=False).json()
if response['isLinux'] or response['isOsx']:
2019-10-09 04:08:39 +00:00
radarr_platform = 'posix'
2020-01-30 23:03:35 +00:00
elif response['isWindows']:
2019-10-09 04:08:39 +00:00
radarr_platform = 'nt'
2020-01-30 23:03:35 +00:00
except Exception:
logging.debug('BAZARR cannot get Radarr platform')
2019-10-09 04:08:39 +00:00
return radarr_platform