new iOS/web extractors, image proxying done by views.py

- ythdd_globals.py - added helper function to get user-configured header
- ythdd.py - now checks for config.toml in work directory
- requirements.txt - add brotli, so that requests can decompress
innertube request
This commit is contained in:
2024-12-26 20:15:45 +01:00
parent 1e4b05c33b
commit 0099736a74
7 changed files with 251 additions and 45 deletions

View File

@@ -6,15 +6,18 @@ from argparse import ArgumentParser
from ythdd_globals import colors
import requests, json, toml, time
import views, downloader, ythdd_api, ythdd_globals, ythdd_db
import os
from flask_apscheduler import APScheduler
app = Flask(__name__)
app = Flask(__name__)
app_host = "None"
app_port = "None"
def setup():
# sanity check: make sure config is set
# required to make `flask --app ythdd run --debug` work
global config
global config, app_host, app_port
try:
if not config['general']:
ythdd_globals.setConfig(ythdd_globals.configfile)
@@ -31,6 +34,25 @@ def setup():
ythdd_globals.isProxied = config['general']['is_proxied']
ythdd_globals.outsideApiHits = 0
are_we_sure_of_host_and_port = True
if app_host == "None":
app_host = "127.0.0.1"
are_we_sure_of_host_and_port = False
if app_port == "None":
app_port = "5000"
are_we_sure_of_host_and_port = False
public_facing_url = config['general']['public_facing_url']
rewrite_sanity_check = public_facing_url.replace(f"{app_host}:{app_port}", "")
if not config['general']['is_proxied'] and public_facing_url == rewrite_sanity_check:
sanity_string = f"{colors.WARNING}Heads up!{colors.ENDC} Public facing URL does not match the IP and port the server is running on.\n"
sanity_string += f" Expected: {colors.OKCYAN}{config['general']['public_facing_url']}{colors.ENDC}, but"
if not are_we_sure_of_host_and_port: sanity_string += " (assuming it's)"
sanity_string += f" running on: {colors.OKCYAN}{app_host}:{app_port}{colors.ENDC}.\n"
sanity_string += f" This is just a sanity check and may not neccessarily mean bad configuration.\n"
sanity_string += f" If you're running a reverse proxy, set {colors.OKCYAN}is_proxied{colors.ENDC} to true to silence this message.\n"
print(sanity_string)
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{config['general']['db_file_path']}"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.add_url_rule('/', view_func=views.index)
@@ -38,6 +60,8 @@ def setup():
app.add_url_rule('/home', view_func=views.home)
app.add_url_rule('/api/', view_func=ythdd_api.api_greeting)
app.add_url_rule('/api/<path:received_request>', view_func=ythdd_api.api_global_catchall)
app.add_url_rule('/vi/<path:received_request>', view_func=views.thumbnailProxy)
app.add_url_rule('/ggpht/<path:received_request>', view_func=views.ggphtProxy)
db = ythdd_db.initDB(app, config)
with app.app_context():
@@ -81,15 +105,19 @@ def main(args):
host = host_port[0]
port = host_port[1]
global config
global config, app_host, app_port
try:
# if specified, use custom config file
ythdd_globals.configfile = args.config
ythdd_globals.setConfig(ythdd_globals.configfile)
except:
# if not, use dummy file
ythdd_globals.configfile = ""
# if not, try using the default "config.toml"
if os.path.exists("config.toml"):
ythdd_globals.configfile = "config.toml"
else:
# unless it's not there, if that's the case then use the dummy file
ythdd_globals.configfile = ""
# but try to set the API secret if provided by the user
if args.secret:
ythdd_globals.randomly_generated_passcode = args.secret
@@ -97,6 +125,9 @@ def main(args):
config = ythdd_globals.config
app_host = host
app_port = port
setup()
app.run(host=host, port=int(port))
@@ -115,4 +146,6 @@ if __name__ == "__main__":
main(args)
else:
app_host = os.getenv("FLASK_RUN_HOST", "None")
app_port = os.getenv("FLASK_RUN_PORT", "None")
setup()