CoolFace
Apppublic

FaceTrace/Sora2_Video_Downloader

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
app.py136 linesDownload Raw Back to root
1from flask import Flask, request, jsonify2from flask_cors import CORS3import requests4import urllib.parse5import re6import os7 8app = Flask(__name__)9CORS(app, resources={10    r"/api/*": {11        "origins": "*",12        "methods": ["GET", "POST"],13        "allow_headers": ["Content-Type"]14    }15})16 17# dyysy.com API endpoint18DYYSY_API = "https://api.dyysy.com/links"19 20@app.route('/api/analyze', methods=['POST'])21def analyze_video():22    """23    Analyze Sora video URL24    """25    try:26        data = request.get_json()27        sora_url = data.get('url', '').strip()28        29        if not sora_url:30            return jsonify({31                'success': False,32                'error': 'Missing URL'33            }), 40034        35        # Extract Sora ID36        sora_id = extract_sora_id(sora_url)37        if not sora_id:38            return jsonify({39                'success': False,40                'error': 'Invalid Sora URL. Format: https://sora.chatgpt.com/p/s_xxxxx'41            }), 40042        43        # Reconstruct full URL44        full_url = f"https://sora.chatgpt.com/p/{sora_id}"45        46        # Call dyysy.com API47        result = fetch_from_dyysy(full_url)48        49        if not result:50            return jsonify({51                'success': False,52                'error': 'Failed to fetch video. URL may be invalid or private.'53            }), 40054        55        return jsonify(result)56    57    except Exception as e:58        return jsonify({59            'success': False,60            'error': str(e)61        }), 50062 63@app.route('/api/test', methods=['GET'])64def test_endpoint():65    """Health check"""66    return jsonify({67        'success': True,68        'message': 'Backend is running'69    })70 71def extract_sora_id(url_or_id):72    """73    Extract Sora ID from:74    - Full URL: https://sora.chatgpt.com/p/s_xxxx75    - Path: /p/s_xxxx76    - Plain ID: s_xxxx77    """78    # Already plain ID79    if url_or_id.startswith('s_'):80        return url_or_id81    82    # Extract from URL/path83    match = re.search(r's_[a-zA-Z0-9_-]+', url_or_id)84    if match:85        return match.group(0)86    87    return None88 89def fetch_from_dyysy(sora_url):90    """Call dyysy.com API and parse response"""91    try:92        # Encode URL93        encoded_url = urllib.parse.quote(sora_url, safe='')94        api_url = f"{DYYSY_API}/{encoded_url}"95        96        headers = {97            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',98            'Referer': 'https://dyysy.com/',99            'Origin': 'https://dyysy.com'100        }101        102        # Make request103        response = requests.get(api_url, headers=headers, timeout=15)104        response.raise_for_status()105        106        data = response.json()107        108        # Parse response109        return {110            'success': True,111            'post_id': data.get('post_id'),112            'title': data.get('post_info', {}).get('title', 'Untitled'),113            'views': data.get('post_info', {}).get('view_count', 0),114            'likes': data.get('post_info', {}).get('like_count', 0),115            'prompt': data.get('post_info', {}).get('prompt'),116            'links': {117                'thumbnail': data.get('links', {}).get('thumbnail', ''),118                'video_hd': data.get('links', {}).get('mp4', ''),119                'video_with_watermark': data.get('links', {}).get('mp4_wm', ''),120                'video_md': data.get('links', {}).get('md', ''),121                'gif': data.get('links', {}).get('gif', '')122            }123        }124    125    except requests.Timeout:126        return None127    except Exception as e:128        print(f"Error: {str(e)}")129        return None130 131if __name__ == '__main__':132    # Production: Use gunicorn133    # Development: Use Flask134    port = int(os.environ.get('PORT', 7860))135    app.run(host='0.0.0.0', port=port, debug=False)136