File size: 3,943 Bytes
3a02865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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()