import os
from ahserver.webapp import webapp
from ahserver.filestorage import FileStorage
from ahserver.serverenv import ServerEnv
from appPublic.registerfunction import RegisterFunction
from appPublic.log import debug
from appPublic.worker import awaitify

from appbase.init import load_appbase
from rbac.init import load_rbac
from findperson.init import load_findperson
from ext import init_ui_ext
from loadmedia import save_media, load_media
from imageface import create_knownface

def runytdlp(cookie_webpath, url):
	# we have a dl in ~/bin, and ~bin in PATH
	fs = FileStorage()
	cookiefile = fs.realPath(cookie_webpath)
	cmd = f"""cd /d/ymq/media/dl; yt-dlp --cookies {cookiefile} --proxy socks5://127.0.0.1:1086 -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a] /best[ext=mp4]/best' "{url}" """
	debug(f'execute:{cmd}')
	os.system(cmd)

def get_module_dbname(mn):
	return 'mediadb'

def init():
	g = ServerEnv()
	g.get_module_dbname = get_module_dbname
	g.runytdlp = awaitify(runytdlp)
	g.create_knownface = create_knownface
	g.load_media = load_media
	g.save_media = save_media
	rf = RegisterFunction()
	rf.register('get_module_dbname', get_module_dbname)
	init_ui_ext()
	load_appbase()
	load_rbac()
	load_findperson()

if __name__ == '__main__':
	webapp(init)

