hdcode/sami-downloader
0
1from flask import Flask, request, jsonify2from flask_cors import CORS3import yt_dlp4import os5 6app = Flask(__name__)7CORS(app)8 9@app.route('/')10def home():11 return "Sami's Universal Cloud Downloader API is Live!"12 13@app.route('/get_link', methods=['GET'])14def extract_link():15 video_url = request.args.get('url')16 # Default quality 1080p, agar available na ho toh best quality khud utha lega17 quality_choice = request.args.get('quality', '1080') 18 19 if not video_url:20 return jsonify({"status": "error", "message": "URL missing"}), 40021 22 # Universal Options for All Platforms23 ydl_opts = {24 # Format logic: 1080p MP4 preference, then best available MP4, then any best format25 'format': f'bestvideo[height<={quality_choice}][ext=mp4]+bestaudio[ext=m4a]/best[height<={quality_choice}][ext=mp4]/best',26 'quiet': True,27 'no_warnings': True,28 'noplaylist': True,29 'nocheckcertificate': True,30 'merge_output_format': 'mp4',31 'ignoreerrors': True, # Taake agar ek platform slow ho toh crush na ho32 'add_header': [33 'Accept-Language: en-US,en;q=0.9',34 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'35 ]36 }37 38 try:39 with yt_dlp.YoutubeDL(ydl_opts) as ydl:40 # extract_info sab platforms (FB, Insta, TikTok) ke liye kaam karta hai41 info = ydl.extract_info(video_url, download=False)42 43 if not info:44 return jsonify({"status": "error", "message": "Could not extract data from this link"}), 40445 46 # Agar playlist ho toh sirf pehli video uthaye47 if 'entries' in info:48 video_data = info['entries'][0]49 else:50 video_data = info51 52 # Universal response jo har platform ke liye data nikalay53 return jsonify({54 "status": "success",55 "title": video_data.get('title', 'Video File'),56 "download_url": video_data.get('url'),57 "thumbnail": video_data.get('thumbnail'),58 "quality_detected": video_data.get('height', 'Unknown'),59 "platform": video_data.get('extractor_key', 'Generic'),60 "format": "mp4"61 })62 63 except Exception as e:64 return jsonify({"status": "error", "message": str(e)}), 50065 66if __name__ == '__main__':67 # Hugging Face default port68 port = int(os.environ.get("PORT", 7860))69 app.run(host='0.0.0.0', port=port)