Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -141,60 +141,53 @@ import os
|
|
| 141 |
|
| 142 |
# ... आपके अन्य सभी इम्पोर्ट और एंडपॉइंट ...
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
@app.route('/get-stream', methods=['POST'])
|
| 145 |
def get_stream():
|
| 146 |
-
|
| 147 |
data = request.json
|
| 148 |
query = data.get('query')
|
| 149 |
if not query:
|
| 150 |
return jsonify({"error": "Query is required"}), 400
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
max_retries = 3
|
| 169 |
-
for attempt in range(max_retries):
|
| 170 |
-
try:
|
| 171 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 172 |
-
info = ydl.extract_info(f"ytsearch:{query}", download=False)
|
| 173 |
-
if not info or 'entries' not in info or not info['entries']:
|
| 174 |
-
return jsonify({"error": "No video found"}), 404
|
| 175 |
-
|
| 176 |
-
video = info['entries'][0]
|
| 177 |
-
stream_url = video.get('url')
|
| 178 |
-
if not stream_url:
|
| 179 |
-
return jsonify({"error": "Could not extract stream URL"}), 500
|
| 180 |
-
|
| 181 |
-
return jsonify({
|
| 182 |
-
"title": video.get('title'),
|
| 183 |
-
"webpage_url": video.get('webpage_url'),
|
| 184 |
-
"duration": video.get('duration'),
|
| 185 |
-
"stream_url": stream_url,
|
| 186 |
-
"requester": data.get('requester', 'Unknown')
|
| 187 |
-
})
|
| 188 |
-
|
| 189 |
-
except Exception as e:
|
| 190 |
-
print(f"❌ Attempt {attempt+1} failed for '{query}': {e}")
|
| 191 |
-
if attempt < max_retries - 1:
|
| 192 |
-
time.sleep(2) # थोड़ा रुककर फिर कोशिश करें
|
| 193 |
-
continue
|
| 194 |
-
else:
|
| 195 |
-
# सारी कोशिशें फेल होने पर एरर भेजें
|
| 196 |
-
return jsonify({"error": f"All retries failed: {str(e)}"}), 500
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
# ---------- TTS ----------
|
| 199 |
@app.route('/tts', methods=['POST'])
|
| 200 |
def tts_api():
|
|
|
|
| 141 |
|
| 142 |
# ... आपके अन्य सभी इम्पोर्ट और एंडपॉइंट ...
|
| 143 |
|
| 144 |
+
from flask import Flask, request, jsonify
|
| 145 |
+
from piped_api import PipedClient
|
| 146 |
+
import time
|
| 147 |
+
|
| 148 |
+
app = Flask(__name__)
|
| 149 |
+
PIPED_CLIENT = PipedClient()
|
| 150 |
+
|
| 151 |
@app.route('/get-stream', methods=['POST'])
|
| 152 |
def get_stream():
|
| 153 |
+
start_time = time.time()
|
| 154 |
data = request.json
|
| 155 |
query = data.get('query')
|
| 156 |
if not query:
|
| 157 |
return jsonify({"error": "Query is required"}), 400
|
| 158 |
|
| 159 |
+
try:
|
| 160 |
+
# Piped API से केवल 1 रिजल्ट खोजें (तेज़ी के लिए)
|
| 161 |
+
search_results = PIPED_CLIENT.search(query, limit=1)
|
| 162 |
+
if not search_results:
|
| 163 |
+
return jsonify({"error": "No video found"}), 404
|
| 164 |
+
|
| 165 |
+
first_result = search_results[0]
|
| 166 |
+
video_id = first_result['url'].split('=')[-1]
|
| 167 |
+
|
| 168 |
+
# वीडियो की डिटेल लें
|
| 169 |
+
video_detail = PIPED_CLIENT.get_video(video_id)
|
| 170 |
+
if not video_detail.audio_streams:
|
| 171 |
+
return jsonify({"error": "No audio stream found"}), 500
|
| 172 |
+
|
| 173 |
+
# सबसे अच्छी क्वालिटी की ऑडियो स्ट्रीम चुनें
|
| 174 |
+
audio_stream = video_detail.audio_streams[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
processing_time = time.time() - start_time
|
| 177 |
+
print(f"✅ Stream URL generated in {processing_time:.2f} seconds")
|
| 178 |
+
|
| 179 |
+
return jsonify({
|
| 180 |
+
"title": video_detail.title,
|
| 181 |
+
"webpage_url": f"https://youtube.com/watch?v={video_id}",
|
| 182 |
+
"duration": video_detail.duration,
|
| 183 |
+
"stream_url": audio_stream.url,
|
| 184 |
+
"requester": data.get('requester', 'Unknown')
|
| 185 |
+
})
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"❌ Error in /get-stream: {e}")
|
| 189 |
+
return jsonify({"error": str(e)}), 500
|
| 190 |
+
|
| 191 |
# ---------- TTS ----------
|
| 192 |
@app.route('/tts', methods=['POST'])
|
| 193 |
def tts_api():
|