from flask import Flask, request, jsonify import os from werkzeug.utils import secure_filename app = Flask(__name__) # Configuration UPLOAD_FOLDER = 'uploads' ALLOWED_EXTENSIONS = {'mp4', 'mov', 'avi'} app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # Data storage (for demo purposes, use a database like PostgreSQL in production) videos = [] likes = {} # Ensure upload folder exists if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) # Helper to check allowed file extensions def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS # Upload a video @app.route('/upload', methods=['POST']) def upload_video(): if 'video' not in request.files: return jsonify({'error': 'No video file provided'}), 400 file = request.files['video'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) # Save video metadata video_id = len(videos) + 1 videos.append({'id': video_id, 'filename': filename, 'likes': 0}) likes[video_id] = 0 return jsonify({'message': 'Video uploaded successfully', 'video_id': video_id}), 201 else: return jsonify({'error': 'Invalid file format'}), 400 # Retrieve all videos @app.route('/videos', methods=['GET']) def get_videos(): return jsonify(videos) # Like a video @app.route('/like/', methods=['POST']) def like_video(video_id): for video in videos: if video['id'] == video_id: video['likes'] += 1 likes[video_id] += 1 return jsonify({'message': 'Video liked successfully', 'total_likes': video['likes']}), 200 return jsonify({'error': 'Video not found'}), 404 # Run the app if __name__ == '__main__': app.run(debug=True)