ycdl/frontends/ycdl_flask/ycdl_flask_dev.py

126 lines
3.6 KiB
Python
Raw Normal View History

'''
2021-09-09 02:32:21 +00:00
ycdl_flask_dev
==============
This file is the gevent launcher for local / development use.
2021-09-09 02:32:21 +00:00
> ycdl_flask_dev port <flags>
port:
Port number on which to run the server. Default 5000.
--dont-create:
If this flag is passed, YCDL will raise an error if there is not an
existing ycdl.db in the current directory.
--https:
If this flag is not passed, HTTPS will automatically be enabled if the port
is 443. You can pass this flag to enable HTTPS on other ports.
We expect to find ycdl.key and ycdl.crt in frontends/ycdl_flask/https.
--localhost-only:
If this flag is passed, only localhost will be able to access the server.
Other users on the LAN will be blocked.
--refresh-rate X:
Starts a background thread that refreshes all channels once every X seconds.
'''
2020-09-22 09:50:24 +00:00
import gevent.monkey; gevent.monkey.patch_all()
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
2021-09-09 02:32:21 +00:00
from voussoirkit import betterhelp
2021-09-05 08:27:02 +00:00
from voussoirkit import operatornotify
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
2021-09-09 02:31:56 +00:00
log = vlogging.getLogger(__name__, 'ycdl_flask_dev')
2020-09-22 09:50:24 +00:00
import ycdl
import youtube_credentials
import backend
site = backend.site
site.debug = True
####################################################################################################
site = backend.site
2020-09-22 09:50:24 +00:00
HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https')
2016-11-29 04:16:16 +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:
http = gevent.pywsgi.WSGIServer(
2020-09-22 09:50:24 +00:00
listener=('0.0.0.0', port),
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,
)
else:
http = gevent.pywsgi.WSGIServer(
listener=('0.0.0.0', port),
application=site,
)
2020-09-30 22:15:30 +00:00
if localhost_only:
site.localhost_only = True
youtube_core = ycdl.ytapi.Youtube(youtube_credentials.get_youtube_key())
2021-09-09 02:31:56 +00:00
backend.common.init_ycdldb(youtube_core, create=create)
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)'
2021-09-09 02:31:56 +00:00
log.info(message)
if refresh_rate is None:
log.info('No background refresher thread because --refresh-rate was not passed.')
else:
backend.common.start_refresher_thread(refresh_rate)
2020-09-22 09:50:24 +00:00
try:
http.serve_forever()
except KeyboardInterrupt:
2021-09-09 02:31:56 +00:00
log.info('Goodbye')
return 0
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,
port=args.port,
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
)
2021-09-09 02:31:56 +00:00
@vlogging.main_decorator
2021-09-05 08:27:02 +00:00
@operatornotify.main_decorator(subject='YCDL', notify_every_line=True)
def main(argv):
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)
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')
parser.add_argument('--refresh_rate', '--refresh-rate', dest='refresh_rate', type=int, default=None)
parser.set_defaults(func=ycdl_flask_launch_argparse)
2021-09-09 02:32:21 +00:00
return betterhelp.single_main(argv, parser, __doc__)
2016-11-29 04:16:16 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))