MAJOR Fix: Resolved API Request Delays;
Feat: Added a dev test account to users for expedited login; Refactor: Improve socket connection handling and add logging for API request duration & update to ListRow key generation for improved uniqueness; Feat: Made it so streams with no set thumbnail use their category's thumbnail; Minor Fix: Corrections to db recommendation methods;
This commit is contained in:
@@ -16,4 +16,4 @@ COPY . .
|
||||
ENV FLASK_APP=blueprints.__init__
|
||||
ENV FLASK_DEBUG=True
|
||||
|
||||
CMD ["gunicorn", "-b", "0.0.0.0:5000", "blueprints.__init__:create_app()"]
|
||||
CMD ["python", "-c", "from blueprints.socket import socketio; from blueprints.__init__ import create_app; app = create_app(); socketio.run(app, host='0.0.0.0', port=5000, debug=True)"]
|
||||
@@ -1,7 +1,7 @@
|
||||
from flask import Flask
|
||||
from flask_session import Session
|
||||
from flask_cors import CORS
|
||||
from blueprints.utils import logged_in_user
|
||||
from blueprints.utils import logged_in_user, record_time
|
||||
from blueprints.errorhandlers import register_error_handlers
|
||||
# from flask_wtf.csrf import CSRFProtect, generate_csrf
|
||||
|
||||
@@ -9,7 +9,8 @@ from blueprints.authentication import auth_bp
|
||||
from blueprints.stripe import stripe_bp
|
||||
from blueprints.user import user_bp
|
||||
from blueprints.streams import stream_bp
|
||||
from blueprints.chat import chat_bp, socketio
|
||||
from blueprints.chat import chat_bp
|
||||
from blueprints.socket import socketio
|
||||
|
||||
from os import getenv
|
||||
|
||||
@@ -29,8 +30,11 @@ def create_app():
|
||||
CORS(app, supports_credentials=True)
|
||||
# csrf.init_app(app)
|
||||
|
||||
socketio.init_app(app)
|
||||
|
||||
Session(app)
|
||||
app.before_request(logged_in_user)
|
||||
app.after_request(record_time)
|
||||
|
||||
# adds in error handlers
|
||||
register_error_handlers(app)
|
||||
@@ -48,7 +52,6 @@ def create_app():
|
||||
app.register_blueprint(stream_bp)
|
||||
app.register_blueprint(chat_bp)
|
||||
|
||||
# Tell sockets where the initialisation app is
|
||||
socketio.init_app(app, cors_allowed_origins="*")
|
||||
socketio.init_app(app)
|
||||
|
||||
return app
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from flask import Blueprint, jsonify, session
|
||||
from database.database import Database
|
||||
from flask_socketio import SocketIO, emit, join_room, leave_room
|
||||
from .socket import socketio
|
||||
from flask_socketio import emit, join_room, leave_room
|
||||
from datetime import datetime
|
||||
from flask_socketio import SocketIO
|
||||
|
||||
chat_bp = Blueprint("chat", __name__)
|
||||
socketio = SocketIO()
|
||||
|
||||
# <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
|
||||
|
||||
|
||||
3
web_server/blueprints/socket.py
Normal file
3
web_server/blueprints/socket.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from flask_socketio import SocketIO
|
||||
|
||||
socketio = SocketIO(cors_allowed_origins="*", async_mode='gevent', logger=True, engineio_logger=True)
|
||||
@@ -120,7 +120,7 @@ def get_following_categories_streams():
|
||||
"""
|
||||
Returns popular streams in categories which the user followed
|
||||
"""
|
||||
streams = followed_categories_recommendations()
|
||||
streams = followed_categories_recommendations(get_user_id(session.get('username')))
|
||||
return jsonify(streams)
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
from flask import redirect, url_for, request, g, session
|
||||
from functools import wraps
|
||||
from re import match
|
||||
from time import time
|
||||
|
||||
def logged_in_user():
|
||||
"""
|
||||
Validator to make sure a user is logged in.
|
||||
"""
|
||||
g.start_time = time()
|
||||
g.user = session.get("username", None)
|
||||
g.admin = session.get("username", None)
|
||||
|
||||
def record_time(response):
|
||||
if hasattr(g, 'start_time'):
|
||||
time_taken = time() - g.start_time
|
||||
print(f"Request to {request.endpoint} took {time_taken:.4f} seconds", flush=True)
|
||||
else:
|
||||
print("No start time found", flush=True)
|
||||
return response
|
||||
|
||||
def login_required(view):
|
||||
"""
|
||||
Add at start of routes where users need to be logged in to access.
|
||||
|
||||
Binary file not shown.
@@ -47,7 +47,8 @@ INSERT INTO subscribes (user_id, subscribed_id, since, expires) VALUES
|
||||
(5, 105, '2024-08-30', '2025-02-28');
|
||||
|
||||
INSERT INTO users (username, password, email, num_followers, stream_key, is_partnered, bio) VALUES
|
||||
('GamerDude2', 'password123', 'gamerdude3@gmail.com', 3200, '7890', 0, 'Streaming my gaming adventures!');
|
||||
('GamerDude2', 'password123', 'gamerdude3@gmail.com', 3200, '7890', 0, 'Streaming my gaming adventures!'),
|
||||
('dev', 'scrypt:32768:8:1$avr94c5cplosNUDc$f2ba0738080facada51a1ed370bf869199e121e547fe64a7094ef0330b5db2ab7fff87700898729977f4cd24f17c17b9e8c0c93e7241dcdf9aa522d5d1732626', 'dev@gmail.com', 1, '8080', 0, 'A test account to save that tedious signup each time!');
|
||||
|
||||
INSERT INTO chat (stream_id, chatter_id, message) VALUES
|
||||
(1, 'Susan', 'Hey Every, loving the stream'),
|
||||
@@ -67,17 +68,4 @@ SELECT * FROM stream_tags;
|
||||
-- To see all tables in the database
|
||||
SELECT name FROM sqlite_master WHERE type='table';
|
||||
|
||||
|
||||
SELECT isLive FROM streams WHERE user_id = '5';
|
||||
|
||||
|
||||
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT chatter_id, message, time_sent
|
||||
FROM chat
|
||||
WHERE stream_id = 1
|
||||
ORDER BY time_sent DESC
|
||||
LIMIT 50
|
||||
)
|
||||
ORDER BY time_sent ASC
|
||||
INSERT INTO users
|
||||
@@ -1,6 +1,3 @@
|
||||
-- View all tables in the database
|
||||
SELECT name FROM sqlite_master WHERE type='table';
|
||||
|
||||
DROP TABLE IF EXISTS users;
|
||||
CREATE TABLE users
|
||||
(
|
||||
|
||||
@@ -21,4 +21,6 @@ typing_extensions==4.12.2
|
||||
urllib3==2.3.0
|
||||
Werkzeug==3.1.3
|
||||
WTForms==3.2.1
|
||||
Gunicorn==20.1.0
|
||||
Gunicorn==20.1.0
|
||||
gevent>=22.10.2
|
||||
gevent-websocket
|
||||
@@ -15,20 +15,22 @@ def user_recommendation_category(user_id: int) -> Optional[int]:
|
||||
""", (user_id,))
|
||||
return data
|
||||
|
||||
def followed_categories_recommendations(user_id: int):
|
||||
#TODO Needs to be reworked to get categories instead of streams of categories (below can be done in another function - get_streams_by_category)
|
||||
def followed_categories_recommendations(user_id : int):
|
||||
"""
|
||||
Returns top 25 streams given a users category following
|
||||
"""
|
||||
with Database() as db:
|
||||
categories = db.fetchall("""
|
||||
SELECT users.user_id, title, username, num_viewers, category_name
|
||||
FROM streams
|
||||
WHERE category_id IN (SELECT category_id FROM categories WHERE user_id = ?)
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25;
|
||||
""", (user_id,))
|
||||
SELECT user_id, title, num_viewers, categories.category_name
|
||||
FROM streams
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
WHERE category_id IN (SELECT category_id FROM categories WHERE user_id = ?)
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25; """, (user_id,))
|
||||
return categories
|
||||
|
||||
#TODO Needs to be reworked to get categories instead of streams of categories
|
||||
def recommendations_based_on_category(category_id: int) -> Optional[List[Tuple[int, str, int]]]:
|
||||
"""
|
||||
Queries stream database to get top 25 most viewed streams based on given category and returns
|
||||
@@ -36,14 +38,14 @@ def recommendations_based_on_category(category_id: int) -> Optional[List[Tuple[i
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchall("""
|
||||
SELECT users.user_id, title, username, num_viewers, category_name
|
||||
FROM streams
|
||||
JOIN users ON users.user_id = streams.user_id
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
WHERE categories.category_id = ?
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25
|
||||
""", (category_id,))
|
||||
SELECT streams.category_id, streams.user_id, streams.title, users.username, streams.num_viewers, categories.category_name
|
||||
FROM streams
|
||||
JOIN users ON users.user_id = streams.user_id
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
WHERE categories.category_id = ?
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25
|
||||
""", (category_id,))
|
||||
return data
|
||||
|
||||
def default_recommendations():
|
||||
@@ -53,13 +55,13 @@ def default_recommendations():
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchall("""
|
||||
SELECT users.user_id, title, username, num_viewers, category_name
|
||||
FROM streams
|
||||
JOIN users ON users.user_id = streams.user_id
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25;
|
||||
""")
|
||||
SELECT stream_id, users.user_id, title, username, num_viewers, category_name
|
||||
FROM streams
|
||||
JOIN users ON users.user_id = streams.user_id
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
ORDER BY num_viewers DESC
|
||||
LIMIT 25;
|
||||
""")
|
||||
return data
|
||||
|
||||
def category_recommendations():
|
||||
|
||||
Reference in New Issue
Block a user