ycdl/frontends/ycdl_flask/backend/common.py

105 lines
3.3 KiB
Python
Raw Normal View History

2016-11-29 04:16:16 +00:00
'''
Do not execute this file directly.
2020-09-22 09:50:24 +00:00
Use ycdl_flask_launch.py to start the server with gevent.
2016-11-29 04:16:16 +00:00
'''
2020-03-28 23:49:33 +00:00
import flask; from flask import request
2020-08-28 23:43:02 +00:00
import gzip
import io
2016-11-29 04:16:16 +00:00
import mimetypes
import os
import threading
2020-03-28 23:49:33 +00:00
import time
2020-03-29 00:05:43 +00:00
from voussoirkit import pathclass
2016-11-29 04:16:16 +00:00
import ycdl
from . import jinja_filters
2020-09-22 09:50:24 +00:00
# Flask init #######################################################################################
2017-10-09 04:39:07 +00:00
root_dir = pathclass.Path(__file__).parent.parent
TEMPLATE_DIR = root_dir.with_child('templates')
STATIC_DIR = root_dir.with_child('static')
FAVICON_PATH = STATIC_DIR.with_child('favicon.png')
site = flask.Flask(
__name__,
template_folder=TEMPLATE_DIR.absolute_path,
static_folder=STATIC_DIR.absolute_path,
)
2016-11-29 04:16:16 +00:00
site.config.update(
SEND_FILE_MAX_AGE_DEFAULT=180,
TEMPLATES_AUTO_RELOAD=True,
)
site.jinja_env.add_extension('jinja2.ext.do')
2020-09-22 09:50:24 +00:00
site.jinja_env.trim_blocks = True
site.jinja_env.lstrip_blocks = True
jinja_filters.register_all(site)
2016-11-29 04:16:16 +00:00
site.debug = True
2020-09-30 22:15:30 +00:00
site.localhost_only = False
2016-11-29 04:16:16 +00:00
2020-09-22 09:50:24 +00:00
####################################################################################################
2020-09-30 22:15:30 +00:00
@site.before_request
def before_request():
ip = request.remote_addr
if site.localhost_only and ip != '127.0.0.1':
flask.abort(403)
2020-08-28 23:43:02 +00:00
gzip_minimum_size = 500
2020-09-22 09:50:24 +00:00
gzip_maximum_size = 5 * 2**20
2020-08-28 23:43:02 +00:00
gzip_level = 3
@site.after_request
def after_request(response):
'''
Thank you close.io.
https://github.com/closeio/Flask-gzip
'''
accept_encoding = request.headers.get('Accept-Encoding', '')
bail = False
bail = bail or response.status_code < 200
bail = bail or response.status_code >= 300
bail = bail or response.direct_passthrough
2020-09-22 09:50:24 +00:00
bail = bail or int(response.headers.get('Content-Length', 0)) > gzip_maximum_size
2020-08-28 23:43:02 +00:00
bail = bail or len(response.get_data()) < gzip_minimum_size
bail = bail or 'gzip' not in accept_encoding.lower()
bail = bail or 'Content-Encoding' in response.headers
if bail:
return response
gzip_buffer = io.BytesIO()
gzip_file = gzip.GzipFile(mode='wb', compresslevel=gzip_level, fileobj=gzip_buffer)
gzip_file.write(response.get_data())
gzip_file.close()
response.set_data(gzip_buffer.getvalue())
response.headers['Content-Encoding'] = 'gzip'
response.headers['Content-Length'] = len(response.get_data())
return response
2016-11-29 04:16:16 +00:00
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
2020-09-22 09:50:24 +00:00
def init_ycdldb(*args, **kwargs):
global ycdldb
ycdldb = ycdl.ycdldb.YCDLDB(*args, **kwargs)
2016-11-29 04:16:16 +00:00
def refresher_thread(rate):
while True:
time.sleep(rate)
print('Starting refresh job.')
thread_kwargs = {'force': False, 'skip_failures': True}
refresh_job = threading.Thread(target=ycdldb.refresh_all_channels, kwargs=thread_kwargs, daemon=True)
refresh_job.start()
def start_refresher_thread(rate):
print(f'Starting refresher thread, once per {rate} seconds.')
refresher = threading.Thread(target=refresher_thread, args=[rate], daemon=True)
refresher.start()