136 lines
4.8 KiB
Python
Executable File
136 lines
4.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import Union
|
|
import yaml
|
|
from appdirs import user_data_dir
|
|
|
|
import server.background
|
|
import server.helpers.regex
|
|
from server import opts
|
|
from server.api import shared
|
|
from server.api.job_tracker import JobTracker
|
|
from server.mysql import DatabaseConnection, check_if_database_exists, db_logger, get_console_logger, init_db, test_mysql_connection
|
|
from ydl.files import create_directories, resolve_path
|
|
|
|
|
|
def load_config(path: Union[str, Path]):
|
|
with open(path, 'r') as file:
|
|
return yaml.safe_load(file)
|
|
|
|
|
|
cwd = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--config', default=Path(cwd, 'config.yml'), help='Path to the main config file. Default: ./config.yml')
|
|
parser.add_argument('--erase-db', action='store_true', help='Erase and reset the database')
|
|
parser.add_argument('--force-db-setup', action='store_true', help='Execute the setup sql regardless')
|
|
args = parser.parse_args()
|
|
|
|
temp_logger = get_console_logger(debug=opts.verbose)
|
|
|
|
config_path = resolve_path(Path(args.config))
|
|
if not config_path.is_file():
|
|
print('Config file does not exist:', config_path)
|
|
quit(1)
|
|
config = load_config(config_path)
|
|
|
|
opts.base_output = resolve_path(Path(config['base_output']))
|
|
if not opts.base_output.is_dir():
|
|
print('Base output directory does not exist:', opts.base_output)
|
|
quit(1)
|
|
|
|
if 'download_cache_directory' in config.keys():
|
|
download_cache_directory = resolve_path(Path(config['download_cache_directory']))
|
|
if not download_cache_directory.is_dir():
|
|
print('Download cache directory does not exist:', download_cache_directory)
|
|
quit(1)
|
|
else:
|
|
download_cache_directory = user_data_dir('automated-youtube-dl', 'cyberes')
|
|
|
|
if 'threads' in config.keys():
|
|
opts.threads = config['threads']
|
|
if opts.threads <= 0:
|
|
print("Can't have <= 0 threads!")
|
|
quit(1)
|
|
|
|
if 'jobs_cleanup_time' in config.keys():
|
|
opts.jobs_cleanup_time = config['jobs_cleanup_time']
|
|
if opts.jobs_cleanup_time <= 0:
|
|
print("jobs_cleanup_time must be greater than 0!")
|
|
quit(1)
|
|
|
|
if 'logs_directory' in config.keys():
|
|
logs_directory = resolve_path(Path(config['logs_directory']))
|
|
if not logs_directory.is_dir():
|
|
print('Logs directory does not exist:', logs_directory)
|
|
quit(1)
|
|
else:
|
|
logs_directory = opts.base_output / 'logs'
|
|
create_directories(logs_directory) # create the default path in base_output which should exist.
|
|
|
|
if config['verbose']:
|
|
opts.verbose = True
|
|
|
|
opts.mysql = {
|
|
'host': config['mysql']['host'],
|
|
'user': config['mysql']['user'],
|
|
'password': config['mysql']['password'],
|
|
'database': config['mysql']['database']
|
|
}
|
|
|
|
temp_logger.info('Connecting to database...')
|
|
mysql_success, mysql_error = test_mysql_connection()
|
|
if not mysql_success:
|
|
print('Failed to connect to MySQL database:', mysql_error)
|
|
quit(1)
|
|
temp_logger.info('Database connected!')
|
|
|
|
if args.erase_db:
|
|
prompt = input('Really erase? y/n > ')
|
|
if prompt.lower() == 'y':
|
|
with DatabaseConnection() as conn:
|
|
cursor = conn.cursor()
|
|
cursor.execute("show tables;")
|
|
result = cursor.fetchall()
|
|
for table in result:
|
|
t = table[0]
|
|
cursor.execute(f'TRUNCATE TABLE {t}')
|
|
print(t)
|
|
quit()
|
|
|
|
db_created = check_if_database_exists()
|
|
if not db_created:
|
|
temp_logger.info('Setting up database...')
|
|
init_db()
|
|
temp_logger.info('Database created!')
|
|
db_correct, missing_tables = check_if_database_exists(partial=True)
|
|
if not db_correct:
|
|
if not args.force_db_setup:
|
|
temp_logger.fatal(f'Your database is missing tables: {", ".join(missing_tables)}. Please compare your DB to the setup SQL script. Or, try the --force-db-setup arg (not recommended).')
|
|
quit(1)
|
|
else:
|
|
temp_logger.info('--force-db-setup forced us to set up the database...')
|
|
init_db()
|
|
|
|
# The DB is all set up so now we can log to it
|
|
temp_logger.handlers.clear()
|
|
del temp_logger
|
|
logger = db_logger('MAIN', 'logs', console=True)
|
|
logger.setLevel(logging.INFO)
|
|
|
|
if config['rm_cache_startup']:
|
|
subprocess.run('yt-dlp --rm-cache-dir', shell=True)
|
|
logger.info('Cleared yt-dlp cache')
|
|
|
|
# TODO: print startup variables including the ones above
|
|
|
|
shared.global_job_tracker = JobTracker()
|
|
server.background.start()
|
|
server.api.start()
|
|
logger.info('Startup completed')
|