2016-11-29 04:16:16 +00:00
|
|
|
'''
|
|
|
|
Do not execute this file directly.
|
2021-04-03 01:00:01 +00:00
|
|
|
Use ycdl_flask_dev.py or ycdl_flask_prod.py.
|
2016-11-29 04:16:16 +00:00
|
|
|
'''
|
2020-03-28 23:49:33 +00:00
|
|
|
import flask; from flask import request
|
2023-06-24 19:01:11 +00:00
|
|
|
import functools
|
2020-01-07 06:07:43 +00:00
|
|
|
import threading
|
2020-03-28 23:49:33 +00:00
|
|
|
import time
|
2023-08-10 06:24:47 +00:00
|
|
|
import traceback
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2021-04-03 01:25:55 +00:00
|
|
|
from voussoirkit import flasktools
|
2020-03-29 00:05:43 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-09-09 02:31:56 +00:00
|
|
|
from voussoirkit import vlogging
|
|
|
|
|
|
|
|
log = vlogging.getLogger(__name__)
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
import ycdl
|
|
|
|
|
2018-12-18 03:17:53 +00:00
|
|
|
from . import jinja_filters
|
|
|
|
|
2020-09-22 09:50:24 +00:00
|
|
|
# Flask init #######################################################################################
|
|
|
|
|
2021-11-01 23:04:25 +00:00
|
|
|
# __file__ = .../ycdl_flask/backend/common.py
|
|
|
|
# root_dir = .../ycdl_flask
|
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')
|
2021-11-01 23:04:25 +00:00
|
|
|
BROWSER_CACHE_DURATION = 180
|
2017-10-09 04:39:07 +00:00
|
|
|
|
|
|
|
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(
|
2021-11-01 23:04:25 +00:00
|
|
|
SEND_FILE_MAX_AGE_DEFAULT=BROWSER_CACHE_DURATION,
|
2016-11-29 04:16:16 +00:00
|
|
|
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
|
|
|
|
2021-11-11 07:40:11 +00:00
|
|
|
# This timestamp indicates the last time that all channels got a refresh.
|
|
|
|
# If the user clicks the "refresh all channels" button, we can update this
|
|
|
|
# timestamp so that the background refresher thread knows that it can wait
|
|
|
|
# a little longer.
|
|
|
|
# I chose the initial value as time.time() instead of 0 because when I'm
|
|
|
|
# testing the server and restarting it often, I don't want it making a bunch of
|
|
|
|
# network requests and/or burning API calls every time.
|
|
|
|
last_refresh = time.time()
|
|
|
|
|
2021-04-03 01:25:55 +00:00
|
|
|
# Request decorators ###############################################################################
|
2020-09-22 09:50:24 +00:00
|
|
|
|
2020-09-30 22:15:30 +00:00
|
|
|
@site.before_request
|
|
|
|
def before_request():
|
2021-06-05 04:34:05 +00:00
|
|
|
request.is_localhost = (request.remote_addr == '127.0.0.1')
|
2021-02-26 03:45:11 +00:00
|
|
|
if site.localhost_only and not request.is_localhost:
|
2020-09-30 22:15:30 +00:00
|
|
|
flask.abort(403)
|
|
|
|
|
2020-08-28 23:43:02 +00:00
|
|
|
@site.after_request
|
|
|
|
def after_request(response):
|
2021-04-03 01:25:55 +00:00
|
|
|
response = flasktools.gzip_response(request, response)
|
2020-08-28 23:43:02 +00:00
|
|
|
return response
|
|
|
|
|
2023-06-24 19:01:11 +00:00
|
|
|
site.route = flasktools.decorate_and_route(
|
|
|
|
flask_app=site,
|
|
|
|
decorators=[
|
|
|
|
flasktools.ensure_response_type,
|
|
|
|
functools.partial(
|
|
|
|
flasktools.give_theme_cookie,
|
|
|
|
cookie_name='ycdl_theme',
|
|
|
|
default_theme='slate',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def render_template(request, template_name, **kwargs):
|
|
|
|
theme = request.cookies.get('ycdl_theme', None)
|
|
|
|
|
|
|
|
response = flask.render_template(
|
|
|
|
template_name,
|
|
|
|
request=request,
|
|
|
|
theme=theme,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2016-11-29 04:16:16 +00:00
|
|
|
####################################################################################################
|
|
|
|
|
2021-04-04 18:16:44 +00:00
|
|
|
# These functions will be called by the launcher, flask_dev, flask_prod.
|
|
|
|
|
2020-09-22 09:50:24 +00:00
|
|
|
def init_ycdldb(*args, **kwargs):
|
|
|
|
global ycdldb
|
2021-10-16 04:00:04 +00:00
|
|
|
ycdldb = ycdl.ycdldb.YCDLDB.closest_ycdldb(*args, **kwargs)
|
2016-11-29 04:16:16 +00:00
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
def refresh_all_channels():
|
|
|
|
with ycdldb.transaction:
|
|
|
|
ycdldb.refresh_all_channels(force=False, skip_failures=True)
|
|
|
|
|
2020-03-28 23:42:54 +00:00
|
|
|
def refresher_thread(rate):
|
2021-11-11 07:40:11 +00:00
|
|
|
global last_refresh
|
2020-01-07 06:07:43 +00:00
|
|
|
while True:
|
2022-01-10 05:59:18 +00:00
|
|
|
# If the user pressed the refresh button, the thread will wake from
|
|
|
|
# sleep and find that it should go back to sleep for a little longer.
|
|
|
|
while True:
|
|
|
|
next_refresh = last_refresh + rate
|
|
|
|
wait = next_refresh - time.time()
|
|
|
|
if wait <= 0:
|
|
|
|
break
|
2021-11-11 07:40:11 +00:00
|
|
|
time.sleep(wait)
|
2022-01-10 05:59:18 +00:00
|
|
|
|
2021-09-09 02:31:56 +00:00
|
|
|
log.info('Starting refresh job.')
|
2020-11-10 04:01:47 +00:00
|
|
|
refresh_job = threading.Thread(
|
2022-07-16 05:30:06 +00:00
|
|
|
target=refresh_all_channels,
|
2020-11-10 04:01:47 +00:00
|
|
|
daemon=True,
|
|
|
|
)
|
2020-01-07 06:07:43 +00:00
|
|
|
refresh_job.start()
|
2021-11-11 07:40:11 +00:00
|
|
|
last_refresh = time.time()
|
2020-01-07 06:07:43 +00:00
|
|
|
|
2023-08-10 06:24:47 +00:00
|
|
|
def ignore_shorts_thread(rate):
|
|
|
|
last_commit_id = None
|
|
|
|
while True:
|
|
|
|
if ycdldb.last_commit_id == last_commit_id:
|
|
|
|
# log.debug('Sleeping again due to no new commits.')
|
|
|
|
time.sleep(5 * rate)
|
|
|
|
continue
|
|
|
|
|
|
|
|
last_commit_id = ycdldb.last_commit_id
|
|
|
|
|
|
|
|
log.info('Starting shorts job.')
|
|
|
|
videos = ycdldb.get_videos_by_sql('''
|
|
|
|
SELECT * FROM videos
|
|
|
|
LEFT JOIN channels ON channels.id = videos.author_id
|
|
|
|
WHERE is_shorts IS NULL AND duration < 62 AND state = "pending" AND channels.ignore_shorts = 1
|
|
|
|
ORDER BY published DESC
|
|
|
|
LIMIT 10
|
|
|
|
''')
|
|
|
|
videos = list(videos)
|
|
|
|
if len(videos) == 0:
|
|
|
|
time.sleep(rate)
|
|
|
|
continue
|
|
|
|
|
|
|
|
with ycdldb.transaction:
|
|
|
|
for video in videos:
|
|
|
|
try:
|
|
|
|
is_shorts = ycdl.ytapi.video_is_shorts(video.id)
|
|
|
|
except Exception as exc:
|
|
|
|
log.warning(traceback.format_exc())
|
2023-08-20 23:08:38 +00:00
|
|
|
video.mark_state('ignored')
|
2023-08-10 06:24:47 +00:00
|
|
|
time.sleep(rate)
|
|
|
|
|
2020-03-28 23:42:54 +00:00
|
|
|
def start_refresher_thread(rate):
|
2021-09-09 02:31:56 +00:00
|
|
|
log.info('Starting refresher thread, once per %d seconds.', rate)
|
2020-03-28 23:42:54 +00:00
|
|
|
refresher = threading.Thread(target=refresher_thread, args=[rate], daemon=True)
|
|
|
|
refresher.start()
|
2023-08-10 06:24:47 +00:00
|
|
|
|
|
|
|
shorts_killer = threading.Thread(target=ignore_shorts_thread, args=[60], daemon=True)
|
|
|
|
shorts_killer.start()
|