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-02-26 15:57:47 +00:00
|
|
|
bazarr_version = ''
|
|
|
|
|
|
|
|
version_file = os.path.join(os.path.dirname(__file__), '..', 'VERSION')
|
|
|
|
if os.path.isfile(version_file):
|
|
|
|
with open(version_file, 'r') as f:
|
|
|
|
bazarr_version = f.read()
|
|
|
|
|
2019-10-28 20:16:33 +00:00
|
|
|
os.environ["BAZARR_VERSION"] = bazarr_version
|
2019-09-18 15:30:46 +00:00
|
|
|
|
2019-09-28 16:38:26 +00:00
|
|
|
import gc
|
|
|
|
import libs
|
|
|
|
|
2018-11-28 11:45:39 +00:00
|
|
|
import hashlib
|
2020-05-07 11:39:59 +00:00
|
|
|
import calendar
|
2018-10-31 19:34:40 +00:00
|
|
|
|
|
|
|
from get_args import args
|
2019-11-06 05:08:17 +00:00
|
|
|
from logger import empty_log
|
2021-03-25 14:22:43 +00:00
|
|
|
from config import settings, url_sonarr, url_radarr, configure_proxy_func, base_url
|
2019-10-28 14:38:57 +00:00
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
from init import *
|
2021-01-19 04:49:51 +00:00
|
|
|
from database import database
|
2019-02-24 03:21:40 +00:00
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
from notifier import update_notifier
|
2019-02-24 03:21:40 +00:00
|
|
|
|
2020-02-13 04:16:22 +00:00
|
|
|
from urllib.parse import unquote
|
2021-01-19 04:49:51 +00:00
|
|
|
from get_languages import load_language_in_db, language_from_alpha2, alpha3_from_alpha2
|
2020-01-21 00:35:55 +00:00
|
|
|
from flask import make_response, request, redirect, abort, render_template, Response, session, flash, url_for, \
|
2019-12-31 22:09:04 +00:00
|
|
|
send_file, stream_with_context
|
2019-11-20 03:02:59 +00:00
|
|
|
|
2018-11-28 11:45:39 +00:00
|
|
|
from get_series import *
|
|
|
|
from get_episodes import *
|
2019-09-03 03:22:38 +00:00
|
|
|
from get_movies import *
|
2018-11-28 11:45:39 +00:00
|
|
|
|
2021-02-26 15:57:47 +00:00
|
|
|
from check_update import apply_update, check_if_new_update, check_releases
|
2020-05-18 02:22:36 +00:00
|
|
|
from server import app, webserver
|
2019-12-29 21:17:39 +00:00
|
|
|
from functools import wraps
|
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
|
|
|
|
database.execute("UPDATE system SET updated='0'")
|
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-03-25 14:22:43 +00:00
|
|
|
@app.route('/', defaults={'path': ''})
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def catch_all(path):
|
|
|
|
return render_template("index.html")
|
2019-12-29 21:17:39 +00:00
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2019-12-13 14:46:24 +00:00
|
|
|
@app.context_processor
|
2020-05-15 15:43:55 +00:00
|
|
|
def template_variable_processor():
|
2021-03-25 14:22:43 +00:00
|
|
|
updated = False
|
2021-02-26 15:57:47 +00:00
|
|
|
try:
|
|
|
|
updated = database.execute("SELECT updated FROM system", only_one=True)['updated']
|
|
|
|
except:
|
|
|
|
pass
|
2019-12-13 14:46:24 +00:00
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
inject = dict()
|
|
|
|
inject["apiKey"] = settings.auth.apikey
|
|
|
|
inject["baseUrl"] = base_url
|
|
|
|
inject["canUpdate"] = not args.no_update
|
|
|
|
inject["hasUpdate"] = updated != '0'
|
2019-12-13 14:46:24 +00:00
|
|
|
|
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-03-25 14:22:43 +00:00
|
|
|
return dict(BAZARR_SERVER_INJECT=inject, baseUrl=template_url)
|
2019-11-27 03:12:16 +00:00
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
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-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 14:22:43 +00:00
|
|
|
baseUrl = settings.sonarr.base_url.strip("/")
|
|
|
|
url_image = (url_sonarr() + '/api' + url.lstrip(baseUrl) + '?apikey=' + apikey).replace('poster-250', 'poster-500')
|
2018-10-03 10:53:22 +00:00
|
|
|
try:
|
2020-01-02 06:16:00 +00:00
|
|
|
req = requests.get(url_image, stream=True, timeout=15, verify=False)
|
2018-10-03 10:53:22 +00:00
|
|
|
except:
|
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-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
|
|
|
|
url_image = url_radarr() + '/api/' + url.lstrip(baseUrl) + '?apikey=' + apikey
|
2018-10-03 10:53:22 +00:00
|
|
|
try:
|
2020-02-06 11:40:39 +00:00
|
|
|
req = requests.get(url_image, stream=True, timeout=15, verify=False)
|
2018-10-03 10:53:22 +00:00
|
|
|
except:
|
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
|
|
|
|
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
# @app.route('/check_update')
|
|
|
|
# @authenticate
|
|
|
|
# def check_update():
|
|
|
|
# if not args.no_update:
|
|
|
|
# check_and_apply_update()
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2021-03-25 14:22:43 +00:00
|
|
|
# return '', 200
|
2018-10-03 10:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def configured():
|
2019-11-06 03:35:27 +00:00
|
|
|
database.execute("UPDATE system SET configured = 1")
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
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-03-25 14:22:43 +00:00
|
|
|
result = requests.get(url, params, allow_redirects=False, verify=False, timeout=5)
|
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:
|
|
|
|
return dict(status=False, error='Error Occured. 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.')
|
|
|
|
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
|
|
|
|
2020-04-16 11:52:35 +00:00
|
|
|
if __name__ == "__main__":
|
2020-05-18 01:51:16 +00:00
|
|
|
webserver.start()
|