Merge branch 'main' of https://github.com/john-david3/cs3305-team11
This commit is contained in:
@@ -5,7 +5,7 @@ from utils.user_utils import get_user_id
|
||||
from blueprints.middleware import login_required
|
||||
from database.database import Database
|
||||
from datetime import datetime
|
||||
from celery_tasks import update_thumbnail, combine_ts_stream
|
||||
from celery_tasks.streaming import update_thumbnail, combine_ts_stream
|
||||
from dateutil import parser
|
||||
from utils.path_manager import PathManager
|
||||
import json
|
||||
@@ -60,7 +60,6 @@ def recommended_streams() -> list[dict]:
|
||||
"""
|
||||
|
||||
user_id = session.get("user_id")
|
||||
|
||||
# Get the user's most popular categories
|
||||
category = get_user_preferred_category(user_id)
|
||||
streams = get_streams_based_on_category(category)
|
||||
@@ -112,7 +111,7 @@ def recommended_categories() -> list | list[dict]:
|
||||
|
||||
"""
|
||||
user_id = session.get("user_id")
|
||||
categories = get_user_category_recommendations(user_id)
|
||||
categories = get_user_category_recommendations(1)
|
||||
return jsonify(categories)
|
||||
|
||||
|
||||
@@ -127,6 +126,18 @@ def following_categories_streams():
|
||||
return jsonify(streams)
|
||||
|
||||
|
||||
@login_required
|
||||
@stream_bp.route('/categories/your_categories')
|
||||
def following_your_categories():
|
||||
"""
|
||||
Returns categories which the user followed
|
||||
"""
|
||||
|
||||
streams = get_followed_your_categories(session.get('user_id'))
|
||||
return jsonify(streams)
|
||||
|
||||
|
||||
|
||||
# User Routes
|
||||
@stream_bp.route('/user/<string:username>/status')
|
||||
def user_live_status(username):
|
||||
@@ -162,6 +173,17 @@ def vods(username):
|
||||
vods = get_user_vods(user_id)
|
||||
return jsonify(vods)
|
||||
|
||||
@stream_bp.route('/vods/all')
|
||||
def get_all_vods():
|
||||
"""
|
||||
Returns data of all VODs by all streamers in a JSON-compatible format
|
||||
"""
|
||||
with Database() as db:
|
||||
vods = db.fetchall("SELECT * FROM vods")
|
||||
|
||||
print("Fetched VODs from DB:", vods)
|
||||
|
||||
return jsonify(vods)
|
||||
|
||||
# RTMP Server Routes
|
||||
|
||||
@@ -187,7 +209,7 @@ def init_stream():
|
||||
|
||||
# Create necessary directories
|
||||
username = user_info["username"]
|
||||
create_local_directories(username)
|
||||
create_user_directories(username)
|
||||
|
||||
return redirect(f"/stream/{username}")
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ from utils.auth import *
|
||||
from utils.utils import get_category_id
|
||||
from blueprints.middleware import login_required
|
||||
from utils.email import send_email, forgot_password_body, newsletter_conf
|
||||
from utils.path_manager import PathManager
|
||||
from celery_tasks.streaming import convert_image_to_png
|
||||
import redis
|
||||
|
||||
from io import BytesIO
|
||||
@@ -14,6 +16,8 @@ r = redis.from_url(redis_url, decode_responses=True)
|
||||
|
||||
user_bp = Blueprint("user", __name__)
|
||||
|
||||
path_manager = PathManager()
|
||||
|
||||
@user_bp.route('/user/<string:username>')
|
||||
def user_data(username: str):
|
||||
"""
|
||||
@@ -42,13 +46,19 @@ def user_profile_picture_save():
|
||||
"""
|
||||
Saves user profile picture
|
||||
"""
|
||||
user_id = session.get("user_id")
|
||||
image = request.files['image']
|
||||
ext = image.filename.split('.')[-1]
|
||||
username = session.get("username")
|
||||
thumbnail_path = path_manager.get_profile_picture_file_path(username)
|
||||
|
||||
image.save(f"/web_server/stream_data/{user_id}.{ext}")
|
||||
# Check if the post request has the file part
|
||||
if 'image' not in request.files:
|
||||
return jsonify({"error": "No image found in request"}), 400
|
||||
|
||||
# Fetch image, convert to png, and save
|
||||
image = Image.open(request.files['image'])
|
||||
image.convert('RGB')
|
||||
image.save(thumbnail_path, "PNG")
|
||||
|
||||
return "Success", 200
|
||||
return jsonify({"message": "Profile picture saved"})
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/user/same/<string:username>')
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
from celery import Celery, shared_task, Task
|
||||
from utils.stream_utils import generate_thumbnail, get_streamer_live_status
|
||||
from time import sleep
|
||||
from os import listdir, remove
|
||||
from datetime import datetime
|
||||
from celery_tasks.preferences import user_preferences
|
||||
import subprocess
|
||||
|
||||
def celery_init_app(app) -> Celery:
|
||||
class FlaskTask(Task):
|
||||
@@ -23,53 +17,3 @@ def celery_init_app(app) -> Celery:
|
||||
celery_app.set_default()
|
||||
app.extensions["celery"] = celery_app
|
||||
return celery_app
|
||||
|
||||
|
||||
@shared_task
|
||||
def update_thumbnail(user_id, stream_file, thumbnail_file, sleep_time) -> None:
|
||||
"""
|
||||
Updates the thumbnail of a stream periodically
|
||||
"""
|
||||
|
||||
if get_streamer_live_status(user_id)['is_live']:
|
||||
print("Updating thumbnail...")
|
||||
generate_thumbnail(stream_file, thumbnail_file)
|
||||
update_thumbnail.apply_async((user_id, stream_file, thumbnail_file, sleep_time), countdown=sleep_time)
|
||||
else:
|
||||
print("Stream has ended, stopping thumbnail updates")
|
||||
|
||||
@shared_task
|
||||
def combine_ts_stream(stream_path, vods_path, vod_file_name):
|
||||
"""
|
||||
Combines all ts files into a single vod, and removes the ts files
|
||||
"""
|
||||
ts_files = [f for f in listdir(stream_path) if f.endswith(".ts")]
|
||||
ts_files.sort()
|
||||
|
||||
# Create temp file listing all ts files
|
||||
with open(f"{stream_path}/list.txt", "w") as f:
|
||||
for ts_file in ts_files:
|
||||
f.write(f"file '{ts_file}'\n")
|
||||
|
||||
# Concatenate all ts files into a single vod
|
||||
|
||||
vod_command = [
|
||||
"ffmpeg",
|
||||
"-f",
|
||||
"concat",
|
||||
"-safe",
|
||||
"0",
|
||||
"-i",
|
||||
f"{stream_path}/list.txt",
|
||||
"-c",
|
||||
"copy",
|
||||
f"{vods_path}/{vod_file_name}.mp4"
|
||||
]
|
||||
|
||||
subprocess.run(vod_command)
|
||||
|
||||
# Remove ts files
|
||||
for ts_file in ts_files:
|
||||
remove(f"{stream_path}/{ts_file}")
|
||||
# Remove m3u8 file
|
||||
remove(f"{stream_path}/index.m3u8")
|
||||
71
web_server/celery_tasks/streaming.py
Normal file
71
web_server/celery_tasks/streaming.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from celery import Celery, shared_task, Task
|
||||
from datetime import datetime
|
||||
from celery_tasks.preferences import user_preferences
|
||||
from utils.stream_utils import generate_thumbnail, get_streamer_live_status
|
||||
from time import sleep
|
||||
from os import listdir, remove
|
||||
import subprocess
|
||||
|
||||
@shared_task
|
||||
def update_thumbnail(user_id, stream_file, thumbnail_file, sleep_time) -> None:
|
||||
"""
|
||||
Updates the thumbnail of a stream periodically
|
||||
"""
|
||||
|
||||
if get_streamer_live_status(user_id)['is_live']:
|
||||
print("Updating thumbnail...")
|
||||
generate_thumbnail(stream_file, thumbnail_file)
|
||||
update_thumbnail.apply_async((user_id, stream_file, thumbnail_file, sleep_time), countdown=sleep_time)
|
||||
else:
|
||||
print("Stream has ended, stopping thumbnail updates")
|
||||
|
||||
@shared_task
|
||||
def combine_ts_stream(stream_path, vods_path, vod_file_name):
|
||||
"""
|
||||
Combines all ts files into a single vod, and removes the ts files
|
||||
"""
|
||||
ts_files = [f for f in listdir(stream_path) if f.endswith(".ts")]
|
||||
ts_files.sort()
|
||||
|
||||
# Create temp file listing all ts files
|
||||
with open(f"{stream_path}/list.txt", "w") as f:
|
||||
for ts_file in ts_files:
|
||||
f.write(f"file '{ts_file}'\n")
|
||||
|
||||
# Concatenate all ts files into a single vod
|
||||
|
||||
vod_command = [
|
||||
"ffmpeg",
|
||||
"-f",
|
||||
"concat",
|
||||
"-safe",
|
||||
"0",
|
||||
"-i",
|
||||
f"{stream_path}/list.txt",
|
||||
"-c",
|
||||
"copy",
|
||||
f"{vods_path}/{vod_file_name}.mp4"
|
||||
]
|
||||
|
||||
subprocess.run(vod_command)
|
||||
|
||||
# Remove ts files
|
||||
for ts_file in ts_files:
|
||||
remove(f"{stream_path}/{ts_file}")
|
||||
# Remove m3u8 file
|
||||
remove(f"{stream_path}/index.m3u8")
|
||||
|
||||
@shared_task
|
||||
def convert_image_to_png(image_path, png_path):
|
||||
"""
|
||||
Converts an image to a png
|
||||
"""
|
||||
image_command = [
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-i",
|
||||
image_path,
|
||||
png_path
|
||||
]
|
||||
|
||||
subprocess.run(image_command)
|
||||
Binary file not shown.
@@ -4,7 +4,29 @@ INSERT INTO users (username, password, email, num_followers, stream_key, is_part
|
||||
('MusicLover', 'music4life', 'musiclover@example.com', 1200, '2345', 0, 'I share my favorite tunes.', 1, 0),
|
||||
('ArtFan', 'artistic123', 'artfan@example.com', 300, '3456', 0, 'Exploring the world of art.', 1, 0),
|
||||
('EduGuru', 'learn123', 'eduguru@example.com', 800, '4567', 0, 'Teaching everything I know.', 1, 0),
|
||||
('SportsStar', 'sports123', 'sportsstar@example.com', 2000, '5678', 0, 'Join me for live sports updates!', 1, 0);
|
||||
('SportsStar', 'sports123', 'sportsstar@example.com', 2000, '5678', 0, 'Join me for live sports updates!', 1, 0),
|
||||
('TechEnthusiast', NULL, 'techenthusiast@example.com', 1500, '6789', 0, 'All about the latest tech news!', 1, 0),
|
||||
('ChefMaster', NULL, 'chefmaster@example.com', 700, '7890', 0, 'Cooking live and sharing recipes.', 1, 0),
|
||||
('TravelExplorer', NULL, 'travelexplorer@example.com', 900, '8901', 0, 'Exploring new places every day!', 1, 0),
|
||||
('BookLover', NULL, 'booklover@example.com', 450, '9012', 0, 'Sharing my thoughts on every book I read.', 1, 0),
|
||||
('FitnessFan', NULL, 'fitnessfan@example.com', 1300, '0123', 0, 'Join my fitness journey!', 1, 0),
|
||||
('NatureLover', NULL, 'naturelover@example.com', 1100, '2346', 0, 'Live streaming nature walks and hikes.', 1, 0),
|
||||
('MovieBuff', NULL, 'moviebuff@example.com', 800, '3457', 0, 'Sharing movie reviews and recommendations.', 1, 0),
|
||||
('ScienceGeek', NULL, 'sciencegeek@example.com', 950, '4568', 0, 'Exploring the wonders of science.', 1, 0),
|
||||
('Comedian', NULL, 'comedian@example.com', 650, '5679', 0, 'Bringing laughter with live stand-up comedy.', 1, 0),
|
||||
('Fashionista', NULL, 'fashionista@example.com', 1200, '6780', 0, 'Join me for live fashion tips and trends.', 1, 0),
|
||||
('HealthGuru', NULL, 'healthguru@example.com', 1400, '7891', 0, 'Live streaming health and wellness advice.', 1, 0),
|
||||
('CarLover', NULL, 'carlove@example.com', 1700, '8902', 0, 'Streaming car reviews and automotive content.', 1, 0),
|
||||
('PetLover', NULL, 'petlover@example.com', 1000, '9013', 0, 'Join me for fun and cute pet moments!', 1, 0),
|
||||
('Dancer', NULL, 'dancer@example.com', 2000, '0124', 0, 'Sharing live dance performances.', 1, 0),
|
||||
('Photographer', NULL, 'photographer@example.com', 1300, '1235', 0, 'Live streaming photography tutorials.', 1, 0),
|
||||
('Motivator', NULL, 'motivator@example.com', 850, '2347', 0, 'Join me for daily motivation and positivity.', 1, 0),
|
||||
('LanguageLearner', NULL, 'languagelearner@example.com', 950, '3458', 0, 'Live streaming language learning sessions.', 1, 0),
|
||||
('HistoryBuff', NULL, 'historybuff@example.com', 750, '4569', 0, 'Exploring history live and in depth.', 1, 0),
|
||||
('Blogger', NULL, 'blogger@example.com', 1200, '5670', 0, 'Sharing my experiences through live blogging.', 1, 0),
|
||||
('DIYer', NULL, 'diyer@example.com', 1300, '6781', 0, 'Live streaming DIY projects and tutorials.', 1, 0),
|
||||
('SportsAnalyst', NULL, 'sportsanalyst@example.com', 1400, '7892', 0, 'Live commentary and analysis on sports events.', 1, 0),
|
||||
('LBozo', NULL, 'lbozo@example.com', 0, '250', 0, 'I like taking Ls.', 1, 0);
|
||||
|
||||
INSERT INTO users (username, password, email, num_followers, stream_key, is_partnered, bio, is_live, is_admin) VALUES
|
||||
('GamerDude2', 'password123', 'gamerdude3@gmail.com', 3200, '7890', 0, 'Streaming my gaming adventures!', 0, 0),
|
||||
@@ -36,6 +58,7 @@ INSERT INTO subscribes (user_id, subscribed_id, since, expires) VALUES
|
||||
(4, 5, '2024-09-12', '2025-01-12'),
|
||||
(5, 1, '2024-08-30', '2025-02-28');
|
||||
|
||||
|
||||
-- Sample Data for followed_categories
|
||||
INSERT INTO followed_categories (user_id, category_id) VALUES
|
||||
(1, 1),
|
||||
@@ -83,7 +106,29 @@ INSERT INTO streams (user_id, title, start_time, category_id) VALUES
|
||||
(2, 'Live Music Jam', '2025-01-25 20:00:00', 2),
|
||||
(3, 'Sketching Live', '2025-01-24 15:00:00', 3),
|
||||
(4, 'Math Made Easy', '2025-01-23 10:00:00', 4),
|
||||
(5, 'Sports Highlights', '2025-02-15 23:00:00', 5);
|
||||
(5, 'Sports Highlights', '2025-02-15 23:00:00', 5),
|
||||
(6, 'Genshin1', '2025-02-17 18:00:00', 6),
|
||||
(7, 'Genshin2', '2025-02-18 19:00:00', 7),
|
||||
(8, 'Genshin3', '2025-02-19 20:00:00', 8),
|
||||
(9, 'Genshin4', '2025-02-20 14:00:00', 9),
|
||||
(10, 'Genshin5', '2025-02-21 09:00:00', 10),
|
||||
(11, 'Genshin6', '2025-02-22 11:00:00', 11),
|
||||
(12, 'Genshin7', '2025-02-23 21:00:00', 12),
|
||||
(13, 'Genshin8', '2025-02-24 16:00:00', 13),
|
||||
(14, 'Genshin9', '2025-02-25 22:00:00', 14),
|
||||
(15, 'Genshin10', '2025-02-26 18:30:00', 15),
|
||||
(16, 'Genshin11', '2025-02-27 17:00:00', 16),
|
||||
(17, 'Genshin12', '2025-02-28 15:00:00', 17),
|
||||
(18, 'Genshin13', '2025-03-01 10:00:00', 18),
|
||||
(19, 'Genshin14', '2025-03-02 20:00:00', 19),
|
||||
(20, 'Genshin15', '2025-03-03 13:00:00', 20),
|
||||
(21, 'Genshin16', '2025-03-04 09:00:00', 21),
|
||||
(22, 'Genshin17', '2025-03-05 12:00:00', 22),
|
||||
(23, 'Genshin18', '2025-03-06 14:00:00', 23),
|
||||
(24, 'Genshin19', '2025-03-07 16:00:00', 24),
|
||||
(25, 'Genshin20', '2025-03-08 19:00:00', 25),
|
||||
(26, 'Genshin21', '2025-03-09 21:00:00', 26),
|
||||
(27, 'Genshin22', '2025-03-10 17:00:00', 27);
|
||||
|
||||
-- Sample Data for vods
|
||||
INSERT INTO vods (user_id, title, datetime, category_id, length, views) VALUES
|
||||
|
||||
@@ -1,20 +1,52 @@
|
||||
import os
|
||||
# Description: This file contains the PathManager class which is responsible for managing the paths of the stream data.
|
||||
|
||||
class PathManager():
|
||||
def __init__(self) -> None:
|
||||
self.root_path = "stream_data"
|
||||
self.vods_path = os.path.join(self.root_path, "vods")
|
||||
self.stream_path = os.path.join(self.root_path, "stream")
|
||||
self.profile_pictures_path = os.path.join(self.root_path, "profile_pictures")
|
||||
|
||||
self._create_root_directories()
|
||||
|
||||
def _create_directory(self, path):
|
||||
"""
|
||||
Create a directory if it does not exist
|
||||
"""
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
os.chmod(path, 0o777)
|
||||
|
||||
def _create_root_directories(self):
|
||||
"""
|
||||
Create directories for stream data if they do not exist
|
||||
"""
|
||||
self._create_directory(self.root_path)
|
||||
self._create_directory(self.vods_path)
|
||||
self._create_directory(self.stream_path)
|
||||
self._create_directory(self.profile_pictures_path)
|
||||
|
||||
def get_vods_path(self, username):
|
||||
return f"stream_data/vods/{username}"
|
||||
return os.path.join(self.vods_path, username)
|
||||
|
||||
def get_stream_path(self, username):
|
||||
return f"stream_data/stream/{username}"
|
||||
return os.path.join(self.stream_path, username)
|
||||
|
||||
def get_stream_file_path(self, username):
|
||||
return f"{self.get_stream_path(username)}/index.m3u8"
|
||||
return os.path.join(self.get_stream_path(username), "index.m3u8")
|
||||
|
||||
def get_current_stream_thumbnail_file_path(self, username):
|
||||
return f"{self.get_stream_path(username)}/index.jpg"
|
||||
return os.path.join(self.get_stream_path(username), "index.png")
|
||||
|
||||
def get_vod_file_path(self, username, vod_id):
|
||||
return f"{self.get_vods_path(username)}/{vod_id}.mp4"
|
||||
return os.path.join(self.get_vods_path(username), f"{vod_id}.mp4")
|
||||
|
||||
def get_vod_thumbnail_file_path(self, username, vod_id):
|
||||
return f"{self.get_vods_path(username)}/{vod_id}.jpg"
|
||||
return os.path.join(self.get_vods_path(username), f"{vod_id}.png")
|
||||
|
||||
def get_profile_picture_file_path(self, username):
|
||||
return os.path.join(self.profile_pictures_path, f"{username}.png")
|
||||
|
||||
def get_profile_picture_path(self):
|
||||
return self.profile_pictures_path
|
||||
@@ -33,6 +33,20 @@ def get_followed_categories_recommendations(user_id: int, no_streams: int = 4) -
|
||||
""", (user_id, no_streams))
|
||||
return streams
|
||||
|
||||
def get_followed_your_categories(user_id: int) -> Optional[List[dict]]:
|
||||
"""
|
||||
Returns all user followed categories
|
||||
"""
|
||||
with Database() as db:
|
||||
categories = db.fetchall("""
|
||||
SELECT categories.category_name
|
||||
FROM categories
|
||||
JOIN followed_categories
|
||||
ON categories.category_id = followed_categories.category_id
|
||||
WHERE followed_categories.user_id = ?;
|
||||
""", (user_id,))
|
||||
return categories
|
||||
|
||||
|
||||
def get_streams_based_on_category(category_id: int, no_streams: int = 4, offset: int = 0) -> Optional[List[dict]]:
|
||||
"""
|
||||
@@ -81,7 +95,7 @@ def get_highest_view_categories(no_categories: int = 4, offset: int = 0) -> Opti
|
||||
""", (no_categories, offset))
|
||||
return categories
|
||||
|
||||
def get_user_category_recommendations(user_id: int, no_categories: int = 4) -> Optional[List[dict]]:
|
||||
def get_user_category_recommendations(user_id: 1, no_categories: int = 4) -> Optional[List[dict]]:
|
||||
"""
|
||||
Queries user_preferences database to find users top favourite streaming category and returns the category
|
||||
"""
|
||||
@@ -90,8 +104,8 @@ def get_user_category_recommendations(user_id: int, no_categories: int = 4) -> O
|
||||
SELECT categories.category_id, categories.category_name
|
||||
FROM categories
|
||||
JOIN user_preferences ON categories.category_id = user_preferences.category_id
|
||||
WHERE user_id = ?
|
||||
ORDER BY favourability DESC
|
||||
WHERE user_preferences.user_id = ?
|
||||
ORDER BY user_preferences.favourability DESC
|
||||
LIMIT ?
|
||||
""", (user_id, no_categories))
|
||||
return categories
|
||||
@@ -84,6 +84,14 @@ def get_user_vods(user_id: int):
|
||||
vods = db.fetchall("""SELECT * FROM vods WHERE user_id = ?;""", (user_id,))
|
||||
return vods
|
||||
|
||||
def get_all_vods():
|
||||
"""
|
||||
Returns data of all VODs by all streamers in a JSON-compatible format
|
||||
"""
|
||||
with Database() as db:
|
||||
vods = db.fetchall("""SELECT * FROM vods""")
|
||||
return vods
|
||||
|
||||
def generate_thumbnail(stream_file: str, thumbnail_file: str, retry_time=5, retry_count=3) -> None:
|
||||
"""
|
||||
Generates the thumbnail of a stream
|
||||
@@ -143,7 +151,7 @@ def get_vod_tags(vod_id: int):
|
||||
""", (vod_id,))
|
||||
return tags
|
||||
|
||||
def create_local_directories(username: str):
|
||||
def create_user_directories(username: str):
|
||||
"""
|
||||
Create directories for user stream data if they do not exist
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user