2020-09-30 21:47:05 +00:00
|
|
|
'''
|
|
|
|
This file is the gevent launcher for local / development use.
|
|
|
|
|
|
|
|
Simply run it on the command line:
|
2020-09-30 22:03:29 +00:00
|
|
|
python ycdl_flask_dev.py [port]
|
2020-09-30 21:47:05 +00:00
|
|
|
'''
|
2020-09-22 09:50:24 +00:00
|
|
|
import gevent.monkey; gevent.monkey.patch_all()
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
import logging
|
2020-09-30 21:47:05 +00:00
|
|
|
handler = logging.StreamHandler()
|
|
|
|
log_format = '{levelname}:ycdl.{module}.{funcName}: {message}'
|
|
|
|
handler.setFormatter(logging.Formatter(log_format, style='{'))
|
|
|
|
logging.getLogger().addHandler(handler)
|
2017-10-09 04:39:07 +00:00
|
|
|
|
2020-03-28 23:44:47 +00:00
|
|
|
import argparse
|
2016-11-29 04:16:16 +00:00
|
|
|
import gevent.pywsgi
|
2021-01-29 00:47:52 +00:00
|
|
|
import os
|
2016-11-29 04:16:16 +00:00
|
|
|
import sys
|
2016-12-07 06:11:09 +00:00
|
|
|
|
2020-09-22 09:50:24 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-11-21 00:23:36 +00:00
|
|
|
from voussoirkit import vlogging
|
2020-09-22 09:50:24 +00:00
|
|
|
|
|
|
|
import ycdl
|
2021-04-26 01:02:26 +00:00
|
|
|
import youtube_credentials
|
2020-09-30 22:03:29 +00:00
|
|
|
import backend
|
|
|
|
|
2021-01-29 00:46:54 +00:00
|
|
|
site = backend.site
|
|
|
|
site.debug = True
|
|
|
|
|
2020-09-30 22:03:29 +00:00
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
site = backend.site
|
2020-09-22 09:50:24 +00:00
|
|
|
|
|
|
|
HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https')
|
2020-11-21 00:23:36 +00:00
|
|
|
LOG_LEVEL = vlogging.NOTSET
|
2016-11-29 04:16:16 +00:00
|
|
|
|
2020-09-30 22:17:27 +00:00
|
|
|
def ycdl_flask_launch(
|
|
|
|
*,
|
|
|
|
create,
|
|
|
|
localhost_only,
|
|
|
|
port,
|
|
|
|
refresh_rate,
|
|
|
|
use_https,
|
|
|
|
):
|
2020-09-22 09:50:24 +00:00
|
|
|
if use_https is None:
|
|
|
|
use_https = port == 443
|
|
|
|
|
|
|
|
if use_https:
|
2020-03-28 23:44:47 +00:00
|
|
|
http = gevent.pywsgi.WSGIServer(
|
2020-09-22 09:50:24 +00:00
|
|
|
listener=('0.0.0.0', port),
|
2020-09-30 22:03:29 +00:00
|
|
|
application=site,
|
2020-09-22 09:50:24 +00:00
|
|
|
keyfile=HTTPS_DIR.with_child('ycdl.key').absolute_path,
|
|
|
|
certfile=HTTPS_DIR.with_child('ycdl.crt').absolute_path,
|
2020-03-28 23:44:47 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
http = gevent.pywsgi.WSGIServer(
|
|
|
|
listener=('0.0.0.0', port),
|
2020-09-30 22:03:29 +00:00
|
|
|
application=site,
|
2020-03-28 23:44:47 +00:00
|
|
|
)
|
|
|
|
|
2020-09-30 22:15:30 +00:00
|
|
|
if localhost_only:
|
|
|
|
site.localhost_only = True
|
|
|
|
|
2021-04-26 01:02:26 +00:00
|
|
|
youtube_core = ycdl.ytapi.Youtube(youtube_credentials.get_youtube_key())
|
2020-11-21 00:23:36 +00:00
|
|
|
backend.common.init_ycdldb(youtube_core, create=create, log_level=LOG_LEVEL)
|
|
|
|
ycdl.ytrss.log.setLevel(LOG_LEVEL)
|
2020-09-22 09:50:24 +00:00
|
|
|
|
2020-03-28 23:44:47 +00:00
|
|
|
if refresh_rate is not None:
|
2020-09-30 22:03:29 +00:00
|
|
|
backend.common.start_refresher_thread(refresh_rate)
|
2020-09-22 09:50:24 +00:00
|
|
|
|
2021-01-30 12:34:46 +00:00
|
|
|
message = f'Starting server on port {port}, pid={os.getpid()}.'
|
2020-09-22 09:50:24 +00:00
|
|
|
if use_https:
|
|
|
|
message += ' (https)'
|
|
|
|
print(message)
|
2020-03-28 23:44:47 +00:00
|
|
|
|
2020-09-22 09:50:24 +00:00
|
|
|
try:
|
|
|
|
http.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2020-03-28 23:44:47 +00:00
|
|
|
|
|
|
|
def ycdl_flask_launch_argparse(args):
|
|
|
|
return ycdl_flask_launch(
|
2020-09-22 09:50:24 +00:00
|
|
|
create=args.create,
|
2020-09-30 22:15:30 +00:00
|
|
|
localhost_only=args.localhost_only,
|
2020-03-28 23:44:47 +00:00
|
|
|
port=args.port,
|
2020-11-03 08:04:32 +00:00
|
|
|
refresh_rate=args.refresh_rate,
|
2020-09-22 09:50:24 +00:00
|
|
|
use_https=args.use_https,
|
2016-11-29 04:16:16 +00:00
|
|
|
)
|
|
|
|
|
2020-03-28 23:44:47 +00:00
|
|
|
def main(argv):
|
2020-11-21 00:23:36 +00:00
|
|
|
global LOG_LEVEL
|
|
|
|
(LOG_LEVEL, argv) = vlogging.get_level_by_argv(argv)
|
|
|
|
|
2020-03-28 23:44:47 +00:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('port', nargs='?', type=int, default=5000)
|
2020-09-22 09:50:24 +00:00
|
|
|
parser.add_argument('--dont_create', '--dont-create', '--no-create', dest='create', action='store_false', default=True)
|
2020-09-30 22:17:27 +00:00
|
|
|
parser.add_argument('--https', dest='use_https', action='store_true', default=None)
|
2020-09-30 22:15:30 +00:00
|
|
|
parser.add_argument('--localhost_only', '--localhost-only', dest='localhost_only', action='store_true')
|
2020-11-03 08:04:32 +00:00
|
|
|
parser.add_argument('--refresh_rate', '--refresh-rate', dest='refresh_rate', type=int, default=None)
|
2020-03-28 23:44:47 +00:00
|
|
|
parser.set_defaults(func=ycdl_flask_launch_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
2016-11-29 04:16:16 +00:00
|
|
|
|
2020-03-28 23:44:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|