diff --git a/app/views.py b/app/views.py index 986b095..0c93db5 100644 --- a/app/views.py +++ b/app/views.py @@ -6,7 +6,6 @@ from urllib.parse import urlparse from flask import Blueprint, abort, current_app, g, jsonify, request from sqlalchemy import inspect from sqlalchemy.exc import SQLAlchemyError -from yt_dlp import YoutubeDL from app.database import get_db_session from app.models import SavedQueue, WatchHistory @@ -69,41 +68,63 @@ def add_video(): current_app.logger.error("Missing video_url field") return jsonify(message="Missing video_url field"), 400 - # Validate video URL format and allowed domains - if not is_valid_url( - data.get("video_url"), allowed_domains=["youtube.com", "youtu.be"] - ): - current_app.logger.error("Invalid video URL format or domain") - return jsonify(message="Invalid video URL"), 400 + video_url = data.get("video_url") - # Check if we only have video_url - if all(key == "video_url" for key in data.keys()): - current_app.logger.info( - "Only video_url provided. Fetching additional information..." - ) - video_info = fetch_video_info(data["video_url"]) + # Check if it's a local directory or file path (not starting with http/https) + is_local_path = not video_url.startswith("http://") and not video_url.startswith( + "https://" + ) - if not video_info: - return jsonify(message="Failed to fetch video information"), 400 + if is_local_path: + current_app.logger.info(f"Processing local path: {video_url}") + # Create basic metadata for local path + sanitized_data = { + "video_url": video_url, + "video_name": video_url.split("/")[-1] or "Local Directory", + "channel_url": "local://", + "channel_name": "Local Media", + "category": "Local", + "view_count": 0, + "subscribers": 0, + "thumbnail_url": "", + "upload_date": None, + } + else: + # Regular YouTube URL processing + # Validate video URL format and allowed domains + if not is_valid_url(video_url, allowed_domains=["youtube.com", "youtu.be"]): + current_app.logger.error("Invalid video URL format or domain") + return jsonify(message="Invalid video URL"), 400 - # Replace the data with our fetched info - data = video_info + # Check if we only have video_url + if all(key == "video_url" for key in data.keys()): + current_app.logger.info( + "Only video_url provided. Fetching additional information..." + ) + video_info = fetch_video_info(video_url) - # Validate all required fields exist - if not all( - k in data for k in ["video_url", "video_name", "channel_url", "channel_name"] - ): - current_app.logger.error("Missing required fields after fetching") - return jsonify(message="Missing required fields"), 400 + if not video_info: + return jsonify(message="Failed to fetch video information"), 400 - # Validate and sanitize all inputs - validation_errors = validate_video_data(data) - if validation_errors: - current_app.logger.error(f"Validation errors: {validation_errors}") - return jsonify(message="Invalid input data", errors=validation_errors), 400 + # Replace the data with our fetched info + data = video_info - # Sanitize string inputs - sanitized_data = sanitize_video_data(data) + # Validate all required fields exist + if not all( + k in data + for k in ["video_url", "video_name", "channel_url", "channel_name"] + ): + current_app.logger.error("Missing required fields after fetching") + return jsonify(message="Missing required fields"), 400 + + # Validate and sanitize all inputs + validation_errors = validate_video_data(data) + if validation_errors: + current_app.logger.error(f"Validation errors: {validation_errors}") + return jsonify(message="Invalid input data", errors=validation_errors), 400 + + # Sanitize string inputs + sanitized_data = sanitize_video_data(data) new_entry = WatchHistory( video_url=sanitized_data["video_url"],