File size: 10,852 Bytes
7a11778
 
b1e3e0a
 
7a11778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1e3e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a11778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1e3e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a11778
 
 
 
 
 
 
 
 
 
 
 
b1e3e0a
 
 
 
 
7a11778
 
 
 
 
 
 
 
 
 
b1e3e0a
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from flask import Blueprint, current_app, jsonify, request
from typing import Optional
import requests
import json

bp = Blueprint("api", __name__)


ALLOWED_FILTERS = {
	"songs",
	"videos",
	"albums",
	"artists",
	"playlists",
	"profiles",
	"podcasts",
	"episodes",
	"community_playlists",
}


def _client():
	return current_app.config["YTMUSIC_CLIENT"]


def _get_youtube_suggestions_fallback(query: str) -> list:
	"""Fallback method using a different approach"""
	try:
		# Try multiple fallback endpoints
		endpoints = [
			{
				"url": "https://clients1.google.com/complete/search",
				"params": {
					"client": "youtube",
					"hl": "en",
					"gl": "in",
					"q": query,
					"callback": "google.sbox.p50"
				}
			},
			{
				"url": "https://suggestqueries-clients6.youtube.com/complete/search",
				"params": {
					"ds": "yt",
					"hl": "en",
					"gl": "in",
					"client": "youtube",
					"q": query
				}
			}
		]
		
		headers = {
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
			"Accept": "*/*",
			"Referer": "https://round-darkness-b543.bob17040246.workers.dev/"
		}
		
		for endpoint in endpoints:
			try:
				response = requests.get(endpoint["url"], params=endpoint["params"], headers=headers, timeout=10)
				if response.status_code == 200:
					content = response.text
					
					# Handle different callback formats
					if content.startswith("google.sbox.p50(") and content.endswith(");"):
						content = content[16:-2]
					elif content.startswith("window.google.ac.h(") and content.endswith(");"):
						content = content[20:-2]
					elif content.startswith("window.google.ac.s(") and content.endswith(");"):
						content = content[20:-2]
					else:
						# Try to find the start and end of the JSON
						start_idx = content.find('(')
						end_idx = content.rfind(')')
						if start_idx != -1 and end_idx != -1:
							content = content[start_idx + 1:end_idx]
					
					data = json.loads(content)
					if len(data) >= 2 and isinstance(data[1], list):
						suggestions = []
						for item in data[1]:
							if isinstance(item, list) and len(item) > 0:
								suggestions.append(item[0])
							elif isinstance(item, str):
								suggestions.append(item)
						
						if suggestions:
							print(f"Fallback API returned {len(suggestions)} suggestions")
							return suggestions[:10]  # Limit to 10 suggestions
			except Exception as e:
				print(f"Fallback endpoint failed: {e}")
				continue
		
		# If all APIs fail, return basic suggestions based on query
		return _get_static_suggestions(query)
	except:
		return _get_static_suggestions(query)


def _get_static_suggestions(query: str) -> list:
	"""Static fallback suggestions for common queries"""
	query_lower = query.lower()
	
	# Common suggestion patterns
	suggestions_map = {
		"music": ["music", "music video", "music song", "music 2024", "music playlist"],
		"video": ["video", "video song", "video 2024", "video download", "video editing"],
		"movie": ["movie", "movie trailer", "movie song", "movie review", "movie 2024"],
		"song": ["song", "song 2024", "song download", "song lyrics", "song video"],
		"dance": ["dance", "dance video", "dance song", "dance tutorial", "dance 2024"],
		"comedy": ["comedy", "comedy video", "comedy show", "comedy movie", "comedy 2024"],
		"tutorial": ["tutorial", "tutorial video", "tutorial 2024", "tutorial hindi", "tutorial english"],
		"news": ["news", "news today", "news 2024", "news hindi", "news english"],
		"cooking": ["cooking", "cooking video", "cooking recipe", "cooking tutorial", "cooking 2024"],
		"fitness": ["fitness", "fitness video", "fitness workout", "fitness tips", "fitness 2024"]
	}
	
	# Check for exact matches first
	if query_lower in suggestions_map:
		return suggestions_map[query_lower]
	
	# Check for partial matches
	for key, suggestions in suggestions_map.items():
		if key in query_lower or query_lower in key:
			return suggestions
	
	# Default generic suggestions
	return [query, f"{query} video", f"{query} 2024", f"{query} tutorial", f"{query} song"]


def _get_youtube_suggestions(query: str) -> list:
	"""Get search suggestions from YouTube's internal API"""
	try:
		# YouTube's internal suggestions API
		url = "https://suggestqueries-clients6.youtube.com/complete/search"
		params = {
			"ds": "yt",
			"hl": "en",
			"gl": "in", 
			"client": "youtube",
			"gs_ri": "youtube",
			"sugexp": "yrpeb_p,ytpso.bo.rwjb=50,ytpso.bo.rlcft=0.01",
			"tok": "cmbVoqd59wmnnS3CDslsDQ",
			"h": "180",
			"w": "320",
			"ytvs": "1",
			"gs_id": "1",
			"q": query,
			"cp": "1"
		}
		
		# Use a more generic user agent that's less likely to be blocked
		# Remove Accept-Encoding to avoid compression issues on server
		headers = {
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
			"Accept": "*/*",
			"Accept-Language": "en-US,en;q=0.9",
			"Referer": "https://round-darkness-b543.bob17040246.workers.dev/",
			"Origin": "https://round-darkness-b543.bob17040246.workers.dev",
			"Cache-Control": "no-cache",
			"Pragma": "no-cache"
		}
		
		response = requests.get(url, params=params, headers=headers, timeout=15)
		
		# Log response details for debugging
		print(f"YouTube API Response - Status: {response.status_code}, Length: {len(response.text)}")
		
		if response.status_code != 200:
			print(f"YouTube API Error - Status: {response.status_code}, Response: {response.text[:200]}")
			return []
		
		# Parse the JSONP response
		content = response.text
		
		# Check if response is empty or invalid
		if not content or len(content) < 10:
			print(f"YouTube API returned empty/invalid response: {content}")
			return []
		
		# Check if content looks like binary/compressed data
		if any(ord(c) < 32 and c not in '\t\n\r' for c in content[:50]):
			print(f"YouTube API returned binary/compressed data, trying fallback...")
			return _get_youtube_suggestions_fallback(query)
		
		# Remove the callback wrapper (e.g., "window.google.ac.h(" and ");")
		if content.startswith("window.google.ac.h(") and content.endswith(");"):
			content = content[20:-2]
		elif content.startswith("window.google.ac.s(") and content.endswith(");"):
			content = content[20:-2]
		else:
			# Try to find the start and end of the JSON
			start_idx = content.find('(')
			end_idx = content.rfind(')')
			if start_idx != -1 and end_idx != -1:
				content = content[start_idx + 1:end_idx]
			else:
				print(f"Could not parse YouTube API response: {content[:100]}...")
				return []
		
		data = json.loads(content)
		
		# Extract suggestions from the response
		# The response format is: [query, [[suggestion, 0, [512]], ...], {}, {}]
		if len(data) >= 2 and isinstance(data[1], list):
			suggestions = []
			for item in data[1]:
				if isinstance(item, list) and len(item) > 0:
					suggestions.append(item[0])  # First element is the suggestion text
			print(f"YouTube API returned {len(suggestions)} suggestions")
			return suggestions
		
		print(f"YouTube API response format unexpected: {data}")
		# Try fallback method
		print("Trying fallback method...")
		return _get_youtube_suggestions_fallback(query)
		
	except requests.exceptions.RequestException as e:
		print(f"Network error getting YouTube suggestions: {e}")
		# Try fallback method
		print("Trying fallback method...")
		return _get_youtube_suggestions_fallback(query)
	except json.JSONDecodeError as e:
		print(f"JSON decode error getting YouTube suggestions: {e}")
		# Try fallback method
		print("Trying fallback method...")
		return _get_youtube_suggestions_fallback(query)
	except Exception as e:
		# Log the error but don't fail completely
		print(f"Unexpected error getting YouTube suggestions: {e}")
		# Try fallback method
		print("Trying fallback method...")
		return _get_youtube_suggestions_fallback(query)


@bp.get("/search")
def search():
	"""Search YouTube Music
	---
	parameters:
	  - name: q
	    in: query
	    type: string
	    required: true
	    description: Search query string
	  - name: filter
	    in: query
	    type: string
	    required: false
	    enum: [songs, videos, albums, artists, playlists, profiles, podcasts, episodes, community_playlists]
	    description: Restrict results to a specific type
	  - name: limit
	    in: query
	    type: integer
	    required: false
	    default: 25
	    description: Maximum number of results
	responses:
	  200:
	    description: Search results
	  400:
	    description: Missing/invalid params
	"""
	query = request.args.get("q", type=str)
	if not query:
		return jsonify({"error": "Missing required query parameter 'q'"}), 400

	flt: Optional[str] = request.args.get("filter", type=str)
	limit: int = request.args.get("limit", default=25, type=int)

	if flt and flt not in ALLOWED_FILTERS:
		return jsonify({"error": f"Invalid filter. Allowed: {sorted(list(ALLOWED_FILTERS))}"}), 400

	try:
		results = _client().search(query, filter=flt, limit=limit, ignore_spelling=False)
		return jsonify({"results": results})
	except Exception as e:
		return jsonify({"error": f"Search failed: {str(e)}"}), 500


@bp.get("/search/suggestions/debug")
def suggestions_debug():
	"""Debug endpoint to test YouTube suggestions API directly"""
	query = request.args.get("q", type=str)
	if not query:
		return jsonify({"error": "Missing required query parameter 'q'"}), 400
	
	try:
		# Test the YouTube suggestions function directly
		suggestions = _get_youtube_suggestions(query)
		return jsonify({
			"query": query,
			"suggestions": suggestions,
			"count": len(suggestions),
			"source": "youtube_debug"
		})
	except Exception as e:
		return jsonify({
			"error": f"Debug failed: {str(e)}",
			"query": query
		}), 500


@bp.get("/search/suggestions")
def suggestions():
	"""Get search suggestions
	---
	parameters:
	  - name: q
	    in: query
	    type: string
	    required: true
	    description: Partial query to get suggestions for
	  - name: music
	    in: query
	    type: integer
	    required: false
	    description: If 1, get suggestions from YouTube Music. If not present or 0, get suggestions from YouTube
	responses:
	  200:
	    description: Suggestions
	  400:
	    description: Missing/invalid params
	"""
	query = request.args.get("q", type=str)
	if not query:
		return jsonify({"error": "Missing required query parameter 'q'"}), 400

	music = request.args.get("music", type=int)
	
	try:
		if music == 1:
			# Get suggestions from YouTube Music
			sugs = _client().get_search_suggestions(query)
			return jsonify({"suggestions": sugs, "source": "youtube_music"})
		else:
			# Get suggestions from YouTube
			sugs = _get_youtube_suggestions(query)
			return jsonify({"suggestions": sugs, "source": "youtube"})
	except Exception as e:
		return jsonify({"error": f"Suggestions failed: {str(e)}"}), 500