Singhp08 commited on
Commit
0ab065c
·
verified ·
1 Parent(s): c0e060b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -46
app.py CHANGED
@@ -141,60 +141,53 @@ import os
141
 
142
  # ... आपके अन्य सभी इम्पोर्ट और एंडपॉइंट ...
143
 
 
 
 
 
 
 
 
144
  @app.route('/get-stream', methods=['POST'])
145
  def get_stream():
146
- """YouTube से ऑडियो स्ट्रीम URL निकालकर लौटाएं (रीट्राई के साथ)"""
147
  data = request.json
148
  query = data.get('query')
149
  if not query:
150
  return jsonify({"error": "Query is required"}), 400
151
 
152
- # yt-dlp की सेटिंग्स (एक बार डिफाइन करें)
153
- ydl_opts = {
154
- 'format': 'bestaudio/best[height<=720]',
155
- 'quiet': True,
156
- 'no_warnings': True,
157
- 'extract_flat': False,
158
- 'force_ipv4': True, # 👈 IPv6 के झंझट से बचने के लिए
159
- 'socket_timeout': 30, # 👈 टाइमआउट बढ़ाया
160
- 'http_headers': {
161
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
162
- },
163
- 'extractor_args': {'youtube': {'player_client': ['android', 'web']}},
164
- 'cookiefile': 'cookies.txt' if os.path.exists('cookies.txt') else None,
165
- }
166
-
167
- # रीट्राई लॉजिक (3 बार कोशिश करें)
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():