new module "ythdd_db.py" handles database connections now. minor improvements to ythdd.py
This commit is contained in:
69
ythdd.py
69
ythdd.py
@@ -3,82 +3,41 @@ from flask import Flask, render_template
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from markupsafe import escape
|
||||
import requests, json, toml, time
|
||||
import views, downloader, ythdd_v, ythdd_api, ythdd_globals
|
||||
import views, downloader, ythdd_api, ythdd_globals, ythdd_db
|
||||
#from ythdd_db import db
|
||||
|
||||
config = toml.load("config.toml")
|
||||
global app
|
||||
#config = toml.load("config.toml")
|
||||
#global app
|
||||
ythdd_globals.starttime = int(time.time())
|
||||
ythdd_globals.apiRequests = 0
|
||||
ythdd_globals.apiFailedRequests = 0
|
||||
ythdd_globals.isProxied = config['general']['is_proxied']
|
||||
ythdd_globals.isProxied = ythdd_globals.config['general']['is_proxied']
|
||||
ythdd_globals.outsideApiHits = 0
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{config["general"]["db_file_path"]}"
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{ythdd_globals.config["general"]["db_file_path"]}"
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.add_url_rule('/', view_func=views.index)
|
||||
app.add_url_rule('/index.html', view_func=views.index)
|
||||
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)
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
class LocalUsers(db.Model):
|
||||
uid = db.Column(db.Integer, primary_key=True)
|
||||
nick = db.Column(db.String)
|
||||
subscriptions = db.Column(db.JSON) # for RSS feed ???
|
||||
playlists = db.Column(db.JSON)
|
||||
watch_history = db.Column(db.JSON)
|
||||
queue_history = db.Column(db.JSON)
|
||||
|
||||
class Videos(db.Model):
|
||||
ytid = db.Column(db.String(11), primary_key=True)
|
||||
archive_version = db.Column(db.Integer)
|
||||
archive_type = db.Column(db.Integer) # 0 - V+A, 1 - V, 2 - A, 3 - other
|
||||
archive_format = db.Column(db.Integer) # 0, 1, 2, 3... from config.toml[postprocessing][presets]
|
||||
title = db.Column(db.String)
|
||||
description = db.Column(db.String)
|
||||
file_path = db.Column(db.String) # with extension
|
||||
viewcount = db.Column(db.Integer)
|
||||
duration = db.Column(db.Integer)
|
||||
like_count = db.Column(db.Integer)
|
||||
upload_date = db.Column(db.Integer)
|
||||
archive_date = db.Column(db.Integer)
|
||||
channel_id = db.Column(db.String(24)) # author, RemoteUser
|
||||
requested_by = db.Column(db.Integer) # LocalUser
|
||||
comments = db.Column(db.JSON) # TODO: watch frequent commenters
|
||||
|
||||
class Playlists(db.Model):
|
||||
# For archiving videos' list from playlist.
|
||||
ytid = db.Column(db.String(11), primary_key=True)
|
||||
name = db.Column(db.String)
|
||||
videos = db.Column(db.String)
|
||||
modify_date = db.Column(db.Integer)
|
||||
archive_date = db.Column(db.Integer)
|
||||
channel_id = db.Column(db.String(24)) # author, RemoteUser
|
||||
requested_by = db.Column(db.Integer) # LocalUser
|
||||
|
||||
class VideoQueue(db.Model):
|
||||
uid = db.Column(db.Integer, primary_key=True)
|
||||
ytid = db.Column(db.String(11))
|
||||
requested_by = db.Column(db.String)
|
||||
|
||||
class RemoteUsers(db.Model):
|
||||
channel_id = db.Column(db.String(24), primary_key=True) # possibly can change?
|
||||
name = db.Column(db.String)
|
||||
subscribers = db.Column(db.Integer)
|
||||
avatar_path = db.Column(db.String)
|
||||
badge = db.Column(db.Integer) # 0 - no badge, 1 - verified, 2 - music
|
||||
db = ythdd_db.initDB(app)
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
@app.route('/<string:val>', methods=['GET'])
|
||||
def blank(val):
|
||||
return f"{val}: not implemented in ythdd {ythdd_v.version}"
|
||||
return f"{val}: not implemented in ythdd {ythdd_globals.version}"
|
||||
#users = db.session.query(LocalUsers).all()
|
||||
#return users
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("welcome to ythdd")
|
||||
print("to run locally, use ip 127.0.0.1. to run on all interfaces, use 0.0.0.0.")
|
||||
print("enter hostname:port to run flask app on:")
|
||||
host_port = input('> ').split(':')
|
||||
#app.run(host="127.0.0.1", port=5000)
|
||||
app.run(host="0.0.0.0", port=5000)
|
||||
#app.run(host="0.0.0.0", port=5000)
|
||||
app.run(host=host_port[0], port=int(host_port[1]))
|
||||
@@ -2,10 +2,10 @@
|
||||
from flask import Response, request
|
||||
from markupsafe import escape
|
||||
import requests, time, json
|
||||
import ythdd_v, ythdd_api_v1, ythdd_globals
|
||||
import ythdd_api_v1, ythdd_globals
|
||||
|
||||
def api_greeting():
|
||||
string = {'status': 200, 'msg': f"ok (ythdd {ythdd_v.version})", 'latest_api': f"v{ythdd_v.api_version}"}
|
||||
string = {'status': 200, 'msg': f"ok (ythdd {ythdd_globals.version})", 'latest_api': f"v{ythdd_globals.apiVersion}"}
|
||||
string = json.dumps(string)
|
||||
return Response(string, mimetype='application/json')
|
||||
|
||||
@@ -29,7 +29,7 @@ def api_global_catchall(received_request):
|
||||
status, received, data = 500, f"internal server error: call ended in failure: {e}", []
|
||||
else:
|
||||
ythdd_globals.apiFailedRequests += 1
|
||||
status, received, data = 405, f'error: unsupported api version: "{request_list[0]}". try: "v{ythdd_v.api_version}".', []
|
||||
status, received, data = 405, f'error: unsupported api version: "{request_list[0]}". try: "v{ythdd_globals.apiVersion}".', []
|
||||
|
||||
response = {'status': status, 'msg': received, 'data': data}
|
||||
return Response(json.dumps(response), mimetype='application/json', status=status)
|
||||
61
ythdd_db.py
Normal file
61
ythdd_db.py
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python3
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
import toml
|
||||
import ythdd_globals
|
||||
|
||||
database_file = ythdd_globals.config["general"]["db_file_path"]
|
||||
global db
|
||||
|
||||
#db = SQLAlchemy()
|
||||
|
||||
def initDB(app):
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
class LocalUsers(db.Model):
|
||||
uid = db.Column(db.Integer, primary_key=True)
|
||||
nick = db.Column(db.String)
|
||||
subscriptions = db.Column(db.JSON) # for RSS feed ???
|
||||
playlists = db.Column(db.JSON)
|
||||
watch_history = db.Column(db.JSON)
|
||||
queue_history = db.Column(db.JSON)
|
||||
|
||||
class Videos(db.Model):
|
||||
ytid = db.Column(db.String(11), primary_key=True)
|
||||
archive_version = db.Column(db.Integer)
|
||||
archive_type = db.Column(db.Integer) # 0 - V+A, 1 - V, 2 - A, 3 - other
|
||||
archive_format = db.Column(db.Integer) # 0, 1, 2, 3... from config.toml[postprocessing][presets]
|
||||
title = db.Column(db.String)
|
||||
description = db.Column(db.String)
|
||||
file_path = db.Column(db.String) # with extension
|
||||
viewcount = db.Column(db.Integer)
|
||||
duration = db.Column(db.Integer)
|
||||
like_count = db.Column(db.Integer)
|
||||
upload_date = db.Column(db.Integer)
|
||||
archive_date = db.Column(db.Integer)
|
||||
channel_id = db.Column(db.String(24)) # author, RemoteUser
|
||||
requested_by = db.Column(db.Integer) # LocalUser
|
||||
comments = db.Column(db.JSON) # TODO: watch frequent commenters
|
||||
|
||||
class Playlists(db.Model):
|
||||
# For archiving videos' list from playlist.
|
||||
ytid = db.Column(db.String(11), primary_key=True)
|
||||
name = db.Column(db.String)
|
||||
videos = db.Column(db.String)
|
||||
modify_date = db.Column(db.Integer)
|
||||
archive_date = db.Column(db.Integer)
|
||||
channel_id = db.Column(db.String(24)) # author, RemoteUser
|
||||
requested_by = db.Column(db.Integer) # LocalUser
|
||||
|
||||
class VideoQueue(db.Model):
|
||||
uid = db.Column(db.Integer, primary_key=True)
|
||||
ytid = db.Column(db.String(11))
|
||||
requested_by = db.Column(db.String)
|
||||
|
||||
class RemoteUsers(db.Model):
|
||||
channel_id = db.Column(db.String(24), primary_key=True) # possibly can change?
|
||||
name = db.Column(db.String)
|
||||
subscribers = db.Column(db.Integer)
|
||||
avatar_path = db.Column(db.String)
|
||||
badge = db.Column(db.Integer) # 0 - no badge, 1 - verified, 2 - music
|
||||
|
||||
return db
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/python3
|
||||
import time
|
||||
import time, toml
|
||||
|
||||
global starttime, apiRequests, apiFailedRequests, outsideApiHits
|
||||
global starttime, apiRequests, apiFailedRequests, outsideApiHits, config, version, apiVersion
|
||||
|
||||
#def init():
|
||||
# starttime = int(time.time())
|
||||
@@ -9,5 +9,9 @@ global starttime, apiRequests, apiFailedRequests, outsideApiHits
|
||||
#def notImplemented(data):
|
||||
# return 501, f"not recognised/implemented: {data[0]}", []
|
||||
|
||||
config = toml.load("config.toml")
|
||||
version = "0.0.1"
|
||||
apiVersion = "1"
|
||||
|
||||
def getUptime():
|
||||
return int(time.time()) - starttime
|
||||
Reference in New Issue
Block a user