#!/usr/bin/env python3 """ Mock Video Scraper để demo API Tạo video giả để test API functionality """ import time import os import requests import logging logger = logging.getLogger(__name__) class MockVideoScraper: def __init__(self, headless=True): """Khởi tạo mock scraper""" self.headless = headless os.makedirs("/home/ubuntu/downloads", exist_ok=True) def generate_video_simple(self, prompt, timeout=180): """ Mock tạo video từ prompt Tải xuống một video mẫu từ internet """ try: logger.info(f"Mock: Bắt đầu tạo video với prompt: {prompt}") # Giả lập thời gian xử lý time.sleep(5) # Tải xuống video mẫu từ internet sample_video_url = "https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4" # Tạo tên file safe_filename = "".join(c for c in str(prompt) if c.isalnum() or c in (' ', '-', '_')).rstrip() safe_filename = safe_filename.replace(' ', '_')[:30] filename = f"mock_{safe_filename}_{int(time.time())}.mp4" filepath = os.path.join("/home/ubuntu/downloads", filename) logger.info(f"Mock: Đang tải xuống video mẫu...") # Tải xuống video mẫu headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' } try: response = requests.get(sample_video_url, headers=headers, stream=True, timeout=30) response.raise_for_status() with open(filepath, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) # Kiểm tra file size file_size = os.path.getsize(filepath) if file_size > 1000: # Ít nhất 1KB logger.info(f"Mock: Video đã được tạo thành công: {filepath} ({file_size} bytes)") return filepath else: logger.error(f"Mock: File quá nhỏ: {file_size} bytes") os.remove(filepath) return None except Exception as e: logger.error(f"Mock: Lỗi khi tải video mẫu: {e}") # Fallback: tạo file video giả logger.info("Mock: Tạo file video giả...") with open(filepath, 'wb') as f: # Tạo file MP4 header đơn giản f.write(b'\x00\x00\x00\x20ftypmp41\x00\x00\x00\x00mp41isom') f.write(b'Mock video content for prompt: ' + prompt.encode('utf-8')) f.write(b'\x00' * 1000) # Padding để file có kích thước hợp lý logger.info(f"Mock: File video giả đã được tạo: {filepath}") return filepath except Exception as e: logger.error(f"Mock: Lỗi khi tạo video: {e}") return None def close(self): """Đóng scraper""" logger.info("Mock: Scraper đã được đóng") def test_mock(): """Test mock scraper""" scraper = MockVideoScraper() try: prompt = "A dog running in a park" print(f"Testing mock với prompt: {prompt}") video_path = scraper.generate_video_simple(prompt) if video_path: print(f"✅ Thành công! Video: {video_path}") return True else: print("❌ Thất bại!") return False finally: scraper.close() if __name__ == "__main__": test_mock()