File size: 6,597 Bytes
7a11778
b1e3e0a
7a11778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1e3e0a
 
7a11778
b1e3e0a
 
 
 
 
7a11778
 
 
 
 
 
 
 
 
 
 
 
 
 
b1e3e0a
 
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
from flask import Blueprint, current_app, jsonify, request
import requests

bp_entities = Blueprint("entities", __name__)


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


@bp_entities.get("/songs/<video_id>")
def get_song(video_id: str):
	"""Get song details
	---
	parameters:
	  - name: video_id
	    in: path
	    type: string
	    required: true
	responses:
	  200:
	    description: Song metadata
	  500:
	    description: Song data unavailable
	"""
	try:
		data = _client().get_song(video_id)
		return jsonify(data)
	except Exception as e:
		return jsonify({"error": f"Song data unavailable: {str(e)}"}), 500


@bp_entities.get("/albums/<browse_id>")
def get_album(browse_id: str):
	"""Get album details
	---
	parameters:
	  - name: browse_id
	    in: path
	    type: string
	    required: true
	responses:
	  200:
	    description: Album metadata and tracks
	  500:
	    description: Album data unavailable
	"""
	try:
		data = _client().get_album(browse_id)
		return jsonify(data)
	except Exception as e:
		return jsonify({"error": f"Album data unavailable: {str(e)}"}), 500


@bp_entities.get("/artists/<browse_id>")
def get_artist(browse_id: str):
	"""Get artist details
	---
	parameters:
	  - name: browse_id
	    in: path
	    type: string
	    required: true
	responses:
	  200:
	    description: Artist info and releases
	  500:
	    description: Artist data unavailable
	"""
	try:
		data = _client().get_artist(browse_id)
		return jsonify(data)
	except Exception as e:
		return jsonify({"error": f"Artist data unavailable: {str(e)}"}), 500


@bp_entities.get("/playlists/<playlist_id>")
def get_playlist(playlist_id: str):
	"""Get playlist details
	---
	parameters:
	  - name: playlist_id
	    in: path
	    type: string
	    required: true
	  - name: limit
	    in: query
	    type: integer
	    required: false
	    default: 100
	responses:
	  200:
	    description: Playlist metadata and items
	  500:
	    description: Playlist data unavailable
	"""
	limit = request.args.get("limit", default=100, type=int)
	try:
		data = _client().get_playlist(playlist_id, limit=limit)
		return jsonify(data)
	except Exception as e:
		return jsonify({"error": f"Playlist data unavailable: {str(e)}"}), 500


@bp_entities.get("/artist/<artist_id>")
def get_artist_summary(artist_id: str):
	"""Get artist summary via youtubei browse (Top songs, recommendations, featured-on)
	---
	parameters:
	  - name: artist_id
	    in: path
	    type: string
	    required: true
	  - name: country
	    in: query
	    type: string
	    required: false
	    default: US
	responses:
	  200:
	    description: Artist summary payload
	  500:
	    description: Artist data unavailable
	"""
	YOUTUBE_MUSIC_API_URL = "https://summer-darkness-1435.bob17040246.workers.dev/youtubei/v1/browse?prettyPrint=false"
	country = request.args.get("country", default="US", type=str)
	try:
		body = {
			"browseId": artist_id,
			"context": {
				"client": {
					"clientName": "WEB_REMIX",
					"clientVersion": "1.20250915.03.00",
					"gl": country,
				}
			}
		}
		resp = requests.post(
			YOUTUBE_MUSIC_API_URL,
			headers={"Content-Type": "application/json"},
			json=body,
			timeout=15,
		)
		if not resp.ok:
			return jsonify({"error": f"HTTP error: {resp.status_code}"}), 500
		data = resp.json()

		# Parse like the TS logic
		contents = (
			data.get("contents", {})
				.get("singleColumnBrowseResultsRenderer", {})
				.get("tabs", [])[0]
				.get("tabRenderer", {})
				.get("content", {})
				.get("sectionListRenderer", {})
				.get("contents", [])
		)

		artist_header = (
			data.get("header", {})
				.get("musicImmersiveHeaderRenderer", {})
				.get("title", {})
				.get("runs", [{}])[0]
				.get("text")
		)

		# Top songs shelf
		top_songs_shelf = None
		for item in contents:
			if isinstance(item, dict) and "musicShelfRenderer" in item:
				title_runs = (
					item["musicShelfRenderer"]
						.get("title", {})
						.get("runs", [{}])
				)
				if title_runs and title_runs[0].get("text") == "Top songs":
					top_songs_shelf = item["musicShelfRenderer"]
					break
		playlist_id = None
		if top_songs_shelf:
			try:
				playlist_id = (
					top_songs_shelf["contents"][0]
						["musicResponsiveListItemRenderer"]["flexColumns"][0]
						["musicResponsiveListItemFlexColumnRenderer"]["text"]["runs"][0]
						.get("navigationEndpoint", {})
						.get("watchEndpoint", {})
						.get("playlistId")
				)
			except Exception:
				playlist_id = None

		# Recommended artists
		recommended_artists = None
		for item in contents:
			if isinstance(item, dict) and "musicCarouselShelfRenderer" in item:
				header = (
					item["musicCarouselShelfRenderer"]["header"]
						.get("musicCarouselShelfBasicHeaderRenderer", {})
				)
				header_title = header.get("title", {}).get("runs", [{}])[0].get("text")
				if header_title == "Fans might also like":
					recommended_artists = []
					for it in item["musicCarouselShelfRenderer"].get("contents", []):
						two_row = it.get("musicTwoRowItemRenderer", {})
						recommended_artists.append({
							"name": two_row.get("title", {}).get("runs", [{}])[0].get("text"),
							"browseId": two_row.get("navigationEndpoint", {}).get("browseEndpoint", {}).get("browseId"),
							"thumbnail": two_row.get("thumbnailRenderer", {}).get("musicThumbnailRenderer", {}).get("thumbnail", {}).get("thumbnails", [{}])[0].get("url"),
						})
					break

		# Featured on
		featured_on_playlists = None
		for item in contents:
			if isinstance(item, dict) and "musicCarouselShelfRenderer" in item:
				header = (
					item["musicCarouselShelfRenderer"]["header"]
						.get("musicCarouselShelfBasicHeaderRenderer", {})
				)
				header_title = header.get("title", {}).get("runs", [{}])[0].get("text")
				if header_title == "Featured on":
					featured_on_playlists = []
					for it in item["musicCarouselShelfRenderer"].get("contents", []):
						two_row = it.get("musicTwoRowItemRenderer", {})
						featured_on_playlists.append({
							"title": two_row.get("title", {}).get("runs", [{}])[0].get("text"),
							"browseId": two_row.get("navigationEndpoint", {}).get("browseEndpoint", {}).get("browseId"),
							"thumbnail": two_row.get("thumbnailRenderer", {}).get("musicThumbnailRenderer", {}).get("thumbnail", {}).get("thumbnails", [{}])[0].get("url"),
						})
					break

		return jsonify({
			"artistName": artist_header,
			"playlistId": playlist_id,
			"recommendedArtists": recommended_artists,
			"featuredOnPlaylists": featured_on_playlists,
		})
	except Exception as e:
		return jsonify({"error": f"Artist data unavailable: {str(e)}"}), 500