2019-01-26 21:12:19 +00:00
|
|
|
# coding=utf-8
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2019-10-28 20:16:33 +00:00
|
|
|
import os
|
2020-10-18 15:25:14 +00:00
|
|
|
|
2021-04-05 14:09:55 +00:00
|
|
|
bazarr_version = 'unknown'
|
2021-02-26 15:57:47 +00:00
|
|
|
|
|
|
|
version_file = os.path.join(os.path.dirname(__file__), '..', 'VERSION')
|
|
|
|
if os.path.isfile(version_file):
|
|
|
|
with open(version_file, 'r') as f:
|
2021-04-05 14:09:55 +00:00
|
|
|
bazarr_version = f.readline()
|
|
|
|
bazarr_version = bazarr_version.rstrip('\n')
|
2021-02-26 15:57:47 +00:00
|
|
|
|
2021-05-11 03:00:47 +00:00
|
|
|
os.environ["BAZARR_VERSION"] = bazarr_version.lstrip('v')
|
2019-09-18 15:30:46 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
import app.libs # noqa W0611
|
|
|
|
import requests # noqa E402
|
2019-02-24 03:21:40 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from threading import Thread # noqa E402
|
|
|
|
from functools import wraps # noqa E402
|
2022-01-03 03:59:30 +00:00
|
|
|
from urllib.parse import unquote # noqa E402
|
2022-05-03 03:25:04 +00:00
|
|
|
|
|
|
|
import mimetypes # noqa E402
|
|
|
|
|
2022-03-16 06:26:15 +00:00
|
|
|
from flask import request, redirect, abort, render_template, Response, session, send_file, stream_with_context, \
|
2022-05-01 12:00:20 +00:00
|
|
|
send_from_directory # noqa E402
|
2018-11-28 11:45:39 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from app.get_args import args # noqa E402
|
|
|
|
from app.config import settings, configure_proxy_func, base_url # noqa E402
|
|
|
|
from init import * # noqa E402
|
|
|
|
from app.database import System # noqa E402
|
|
|
|
from app.notifier import update_notifier # noqa E402
|
|
|
|
from languages.get_languages import load_language_in_db # noqa E402
|
|
|
|
from app.signalr_client import sonarr_signalr_client, radarr_signalr_client # noqa E402
|
|
|
|
from app.check_update import apply_update, check_releases # noqa E402
|
|
|
|
from app.server import app, webserver # noqa E402
|
|
|
|
from utilities.helper import check_credentials # noqa E402
|
|
|
|
from constants import headers # noqa E402
|
|
|
|
from sonarr.info import get_sonarr_info, url_sonarr # noqa E402
|
|
|
|
from radarr.info import get_radarr_info, url_radarr # noqa E402
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2021-02-26 15:57:47 +00:00
|
|
|
# Install downloaded update
|
|
|
|
if bazarr_version != '':
|
|
|
|
apply_update()
|
|
|
|
check_releases()
|
2019-06-02 23:19:51 +00:00
|
|
|
|
2020-05-08 04:22:14 +00:00
|
|
|
configure_proxy_func()
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2021-02-26 15:57:47 +00:00
|
|
|
# Reset the updated once Bazarr have been restarted after an update
|
2021-05-26 20:47:14 +00:00
|
|
|
System.update({System.updated: '0'}).execute()
|
2018-10-03 10:53:22 +00:00
|
|
|
|
|
|
|
# Load languages in database
|
|
|
|
load_language_in_db()
|
|
|
|
|
2018-12-15 00:36:28 +00:00
|
|
|
login_auth = settings.auth.type
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2019-08-09 00:39:30 +00:00
|
|
|
update_notifier()
|
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
|
|
|
|
def check_login(actual_method):
|
|
|
|
@wraps(actual_method)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if settings.auth.type == 'basic':
|
|
|
|
auth = request.authorization
|
|
|
|
if not (auth and check_credentials(request.authorization.username, request.authorization.password)):
|
|
|
|
return ('Unauthorized', 401, {
|
|
|
|
'WWW-Authenticate': 'Basic realm="Login Required"'
|
|
|
|
})
|
|
|
|
elif settings.auth.type == 'form':
|
|
|
|
if 'logged_in' not in session:
|
|
|
|
return abort(401, message="Unauthorized")
|
|
|
|
actual_method(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2021-04-15 17:19:23 +00:00
|
|
|
@app.errorhandler(404)
|
2022-05-01 12:00:20 +00:00
|
|
|
def page_not_found():
|
2021-04-15 17:19:23 +00:00
|
|
|
return redirect(base_url, code=302)
|
2019-08-09 00:39:30 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
@app.route('/', defaults={'path': ''})
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def catch_all(path):
|
2021-04-21 14:29:25 +00:00
|
|
|
auth = True
|
|
|
|
if settings.auth.type == 'basic':
|
|
|
|
auth = request.authorization
|
|
|
|
if not (auth and check_credentials(request.authorization.username, request.authorization.password)):
|
|
|
|
return ('Unauthorized', 401, {
|
|
|
|
'WWW-Authenticate': 'Basic realm="Login Required"'
|
|
|
|
})
|
|
|
|
elif settings.auth.type == 'form':
|
|
|
|
if 'logged_in' not in session:
|
|
|
|
auth = False
|
2019-12-29 21:17:39 +00:00
|
|
|
|
2021-02-26 15:57:47 +00:00
|
|
|
try:
|
2021-05-27 12:07:16 +00:00
|
|
|
updated = System.get().updated
|
2022-01-03 03:59:30 +00:00
|
|
|
except Exception:
|
2021-05-27 12:07:16 +00:00
|
|
|
updated = '0'
|
2019-12-13 14:46:24 +00:00
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
inject = dict()
|
|
|
|
inject["baseUrl"] = base_url
|
|
|
|
inject["canUpdate"] = not args.no_update
|
2021-05-27 12:07:16 +00:00
|
|
|
inject["hasUpdate"] = updated != '0'
|
2019-12-13 14:46:24 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
if auth:
|
|
|
|
inject["apiKey"] = settings.auth.apikey
|
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
template_url = base_url
|
|
|
|
if not template_url.endswith("/"):
|
|
|
|
template_url += "/"
|
2019-11-27 03:12:16 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
return render_template("index.html", BAZARR_SERVER_INJECT=inject, baseUrl=template_url)
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2022-03-16 06:26:15 +00:00
|
|
|
@app.route('/assets/<path:filename>')
|
|
|
|
def web_assets(filename):
|
2022-05-03 03:25:04 +00:00
|
|
|
# forcing mimetypes to prevent bad configuration in Windows registry to prevent Bazarr UI from showing
|
|
|
|
mimetypes.add_type('application/javascript', '.js')
|
|
|
|
mimetypes.add_type('text/css', '.css')
|
|
|
|
mimetypes.add_type('font/woff2', '.woff2')
|
|
|
|
mimetypes.add_type('image/svg+xml', '.svg')
|
|
|
|
mimetypes.add_type('image/png', '.png')
|
|
|
|
mimetypes.add_type('image/x-icon', '.ico')
|
|
|
|
|
2022-05-04 17:23:24 +00:00
|
|
|
# send_from_directory needs an absolute path then we'll use realpath() here
|
|
|
|
path = os.path.realpath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'frontend', 'build', 'assets'))
|
2022-03-16 06:26:15 +00:00
|
|
|
return send_from_directory(path, filename)
|
|
|
|
|
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
@check_login
|
2019-12-16 13:58:10 +00:00
|
|
|
@app.route('/bazarr.log')
|
2018-10-03 10:53:22 +00:00
|
|
|
def download_log():
|
2021-03-25 14:22:43 +00:00
|
|
|
return send_file(os.path.join(args.config_dir, 'log', 'bazarr.log'), cache_timeout=0, as_attachment=True)
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
@check_login
|
2021-03-25 14:22:43 +00:00
|
|
|
@app.route('/images/series/<path:url>', methods=['GET'])
|
|
|
|
def series_images(url):
|
|
|
|
url = url.strip("/")
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey = settings.sonarr.apikey
|
2021-03-25 18:48:30 +00:00
|
|
|
baseUrl = settings.sonarr.base_url
|
2021-08-04 19:29:37 +00:00
|
|
|
if get_sonarr_info.is_legacy():
|
2021-06-16 17:45:54 +00:00
|
|
|
url_image = (url_sonarr() + '/api/' + url.lstrip(baseUrl) + '?apikey=' +
|
|
|
|
apikey).replace('poster-250', 'poster-500')
|
|
|
|
else:
|
|
|
|
url_image = (url_sonarr() + '/api/v3/' + url.lstrip(baseUrl) + '?apikey=' +
|
|
|
|
apikey).replace('poster-250', 'poster-500')
|
2018-10-03 10:53:22 +00:00
|
|
|
try:
|
2021-05-14 18:42:45 +00:00
|
|
|
req = requests.get(url_image, stream=True, timeout=15, verify=False, headers=headers)
|
2022-01-03 03:59:30 +00:00
|
|
|
except Exception:
|
2021-03-25 14:22:43 +00:00
|
|
|
return '', 404
|
2018-10-03 10:53:22 +00:00
|
|
|
else:
|
2019-12-31 22:09:04 +00:00
|
|
|
return Response(stream_with_context(req.iter_content(2048)), content_type=req.headers['content-type'])
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
@check_login
|
2021-03-25 14:22:43 +00:00
|
|
|
@app.route('/images/movies/<path:url>', methods=['GET'])
|
|
|
|
def movies_images(url):
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey = settings.radarr.apikey
|
2021-03-25 14:22:43 +00:00
|
|
|
baseUrl = settings.radarr.base_url
|
2021-08-04 19:29:37 +00:00
|
|
|
if get_radarr_info.is_legacy():
|
2021-05-31 19:12:55 +00:00
|
|
|
url_image = url_radarr() + '/api/' + url.lstrip(baseUrl) + '?apikey=' + apikey
|
|
|
|
else:
|
|
|
|
url_image = url_radarr() + '/api/v3/' + url.lstrip(baseUrl) + '?apikey=' + apikey
|
2018-10-03 10:53:22 +00:00
|
|
|
try:
|
2021-05-14 18:42:45 +00:00
|
|
|
req = requests.get(url_image, stream=True, timeout=15, verify=False, headers=headers)
|
2022-01-03 03:59:30 +00:00
|
|
|
except Exception:
|
2021-03-25 14:22:43 +00:00
|
|
|
return '', 404
|
2018-10-03 10:53:22 +00:00
|
|
|
else:
|
2019-12-31 22:09:04 +00:00
|
|
|
return Response(stream_with_context(req.iter_content(2048)), content_type=req.headers['content-type'])
|
2018-10-03 10:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def configured():
|
2021-05-26 20:47:14 +00:00
|
|
|
System.update({System.configured: '1'}).execute()
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2021-04-21 14:29:25 +00:00
|
|
|
@check_login
|
2021-03-25 14:22:43 +00:00
|
|
|
@app.route('/test', methods=['GET'])
|
|
|
|
@app.route('/test/<protocol>/<path:url>', methods=['GET'])
|
|
|
|
def proxy(protocol, url):
|
2020-04-23 00:07:21 +00:00
|
|
|
url = protocol + '://' + unquote(url)
|
2021-03-25 14:22:43 +00:00
|
|
|
params = request.args
|
2018-10-03 10:53:22 +00:00
|
|
|
try:
|
2021-05-14 18:42:45 +00:00
|
|
|
result = requests.get(url, params, allow_redirects=False, verify=False, timeout=5, headers=headers)
|
2020-04-13 12:20:14 +00:00
|
|
|
except Exception as e:
|
2020-05-03 02:41:03 +00:00
|
|
|
return dict(status=False, error=repr(e))
|
2018-10-03 10:53:22 +00:00
|
|
|
else:
|
2020-05-03 02:41:03 +00:00
|
|
|
if result.status_code == 200:
|
2021-03-25 14:22:43 +00:00
|
|
|
try:
|
|
|
|
version = result.json()['version']
|
|
|
|
return dict(status=True, version=version)
|
|
|
|
except Exception:
|
2021-06-02 02:13:25 +00:00
|
|
|
return dict(status=False, error='Error Occurred. Check your settings.')
|
2020-05-03 02:41:03 +00:00
|
|
|
elif result.status_code == 401:
|
|
|
|
return dict(status=False, error='Access Denied. Check API key.')
|
2021-06-02 02:13:25 +00:00
|
|
|
elif result.status_code == 404:
|
|
|
|
return dict(status=False, error='Cannot get version. Maybe unsupported legacy API call?')
|
2020-05-03 02:41:03 +00:00
|
|
|
elif 300 <= result.status_code <= 399:
|
|
|
|
return dict(status=False, error='Wrong URL Base.')
|
|
|
|
else:
|
|
|
|
return dict(status=False, error=result.raise_for_status())
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
if settings.general.getboolean('use_sonarr'):
|
2021-10-21 00:46:22 +00:00
|
|
|
Thread(target=sonarr_signalr_client.start).start()
|
2021-05-08 14:39:00 +00:00
|
|
|
if settings.general.getboolean('use_radarr'):
|
2021-10-21 00:46:22 +00:00
|
|
|
Thread(target=radarr_signalr_client.start).start()
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
|
2020-04-16 11:52:35 +00:00
|
|
|
if __name__ == "__main__":
|
2020-05-18 01:51:16 +00:00
|
|
|
webserver.start()
|