Chrunos commited on
Commit
408a4d2
·
verified ·
1 Parent(s): 63c1006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -65
app.py CHANGED
@@ -41,98 +41,164 @@ def searcht():
41
  return jsonify(first_song)
42
 
43
 
44
- # Function to extract track ID from Amazon Music URL
45
  def extract_amazon_track_id(url: str):
46
- if "music.amazon.com" in url:
47
- # Case 1: URL contains trackAsin (e.g., https://music.amazon.com/albums/B01N48U32A?trackAsin=B01NAE38YO&do=play)
48
- parsed_url = urlparse(url)
49
- query_params = parse_qs(parsed_url.query)
50
- if "trackAsin" in query_params:
51
- return query_params["trackAsin"][0]
52
-
53
- # Case 2: URL is a direct track link (e.g., https://music.amazon.com/tracks/B0DNTPYT5S)
54
- if "/tracks/" in url:
55
- return url.split("/tracks/")[-1].split("?")[0]
56
-
 
 
 
 
 
 
 
 
 
 
 
57
  return None
58
-
59
 
60
- # Function to get track info from Song.link API
61
  def get_song_link_info(url: str):
62
- # Check if the URL is from Amazon Music
 
 
 
 
 
 
63
  if "music.amazon.com" in url:
64
  track_id = extract_amazon_track_id(url)
65
  if track_id:
66
- # Use the working format for Amazon Music tracks
67
- api_url = f"https://api.song.link/v1-alpha.1/links?type=song&platform=amazonMusic&id={track_id}&userCountry=US"
 
68
  else:
69
- # If no track ID is found, use the original URL
70
- api_url = f"https://api.song.link/v1-alpha.1/links?url={url}&userCountry=US"
71
  else:
72
- # For non-Amazon Music URLs, use the standard format
73
- api_url = f"https://api.song.link/v1-alpha.1/links?url={url}&userCountry=US"
74
-
75
- # Make the API call
76
- response = requests.get(api_url)
77
- if response.status_code == 200:
78
  return response.json()
79
- else:
 
80
  return None
81
 
82
- # Function to extract Tidal or YouTube URL
83
  def extract_url(links_by_platform: dict, platform: str):
84
- if platform in links_by_platform:
 
 
 
 
85
  return links_by_platform[platform]["url"]
 
86
  return None
87
 
88
-
89
- # Function to extract track title and artist from entities
90
- def extract_track_info(entities_by_unique_id: dict, platform: str):
91
- for entity in entities_by_unique_id.values():
92
- if entity["apiProvider"] == platform:
93
- return entity["title"], entity["artistName"]
94
- return None, None
95
-
96
-
97
-
98
  @app.route('/match', methods=['POST'])
99
- async def match():
100
- data = request.json
 
 
 
 
 
 
 
 
101
  track_url = data.get('url')
 
 
 
 
102
 
103
- if not track_url:
104
- raise HTTPException(status_code=400, detail="No URL provided")
105
 
106
  track_info = get_song_link_info(track_url)
107
  if not track_info:
108
- raise HTTPException(status_code=404, detail="Could not fetch track info")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
- youtube_url = extract_url(track_info["linksByPlatform"], "youtube")
111
- entityUniqueId = track_info["entityUniqueId"]
112
- logger.info(f"songlink info: {entityUniqueId}")
113
- title = track_info["entitiesByUniqueId"][entityUniqueId]["title"]
114
- artist = track_info["entitiesByUniqueId"][entityUniqueId]["artistName"]
115
  if youtube_url:
116
- video_id = youtube_url.split("v=")[1] if "v=" in youtube_url else None
117
- if title and artist:
118
- filename = f"{title} - {artist}"
119
- return {"url": youtube_url, "filename": filename, "track_id": video_id}
120
- else:
121
- return {"url": youtube_url, "filename": "Unknown Track - Unknown Artist", "track_id": video_id}
 
 
 
 
 
122
  else:
123
-
124
- search_query = f'{title}+{artist}'
 
 
 
 
 
125
  search_results = ytmusic.search(search_query, filter="songs")
126
- first_song = next((song for song in search_results if 'videoId' in song and song['videoId']), {}) if search_results else {}
127
- if 'videoId' in first_song:
128
- videoId = first_song["videoId"]
129
- ym_url = f'https://www.youtube.com/watch?v={videoId}'
130
- return {"filename": search_query, "url": ym_url, "track_id": videoId}
 
 
 
 
 
 
 
 
 
 
 
 
131
  else:
132
- raise HTTPException(status_code=404, detail="Video ID not found")
133
-
134
- # If no URLs found, return an error
135
- raise HTTPException(status_code=404, detail="No matching URL found")
136
 
137
 
138
 
 
41
  return jsonify(first_song)
42
 
43
 
 
44
  def extract_amazon_track_id(url: str):
45
+ """
46
+ Extracts track ID from various Amazon Music URL formats.
47
+ """
48
+ if "music.amazon.com" not in url: # MODIFIED: Slight logic inversion for early exit compared to original, same effective outcome.
49
+ return None
50
+
51
+ parsed_url = urlparse(url)
52
+ query_params = parse_qs(parsed_url.query)
53
+
54
+ if "trackAsin" in query_params:
55
+ return query_params["trackAsin"][0]
56
+
57
+ path_parts = parsed_url.path.split('/') # MODIFIED: Changed from simple `url.split` to more robust path parsing for Case 2.
58
+ if "tracks" in path_parts:
59
+ try:
60
+ track_id_index = path_parts.index("tracks") + 1
61
+ if track_id_index < len(path_parts):
62
+ return path_parts[track_id_index] # MODIFIED: Accessing specific part after "tracks".
63
+ except (ValueError, IndexError):
64
+ pass
65
+
66
+ logger.warning(f"Could not extract Amazon track ID from URL: {url}") # ADDED: Logging for when no ID is found.
67
  return None
 
68
 
69
+
70
  def get_song_link_info(url: str):
71
+ """
72
+ Fetches track information from the Song.link API.
73
+ Uses requests.get() which is a blocking call.
74
+ """
75
+ api_base_url = "https://api.song.link/v1-alpha.1/links" # ADDED: Defined base URL for clarity.
76
+ params = {"userCountry": "US"} # MODIFIED: Using a params dictionary for requests.get().
77
+
78
  if "music.amazon.com" in url:
79
  track_id = extract_amazon_track_id(url)
80
  if track_id:
81
+ params["platform"] = "amazonMusic" # MODIFIED: Populating params dict.
82
+ params["id"] = track_id
83
+ params["type"] = "song"
84
  else:
85
+ params["url"] = url # MODIFIED: Populating params dict.
 
86
  else:
87
+ params["url"] = url # MODIFIED: Populating params dict.
88
+
89
+ try: # ADDED: try-except block for robust error handling during API call.
90
+ logger.info(f"Querying Song.link API with params: {params}") # ADDED: Logging the API query.
91
+ response = requests.get(api_base_url, params=params, timeout=10) # MODIFIED: Call uses base_url and params. ADDED: timeout.
92
+ response.raise_for_status() # ADDED: Checks for HTTP errors (4xx or 5xx responses).
93
  return response.json()
94
+ except requests.exceptions.RequestException as e: # ADDED: Catching network/request related exceptions.
95
+ logger.error(f"Error fetching from Song.link API: {e}") # ADDED: Logging the specific error.
96
  return None
97
 
 
98
  def extract_url(links_by_platform: dict, platform: str):
99
+ """
100
+ Extracts a specific platform URL from Song.link API response.
101
+ """
102
+ # MODIFIED: Added .get("url") for safer access to prevent KeyError if "url" key is missing.
103
+ if platform in links_by_platform and links_by_platform[platform].get("url"):
104
  return links_by_platform[platform]["url"]
105
+ logger.warning(f"No URL found for platform '{platform}' in links: {links_by_platform.keys()}") # ADDED: Logging if platform URL not found.
106
  return None
107
 
 
 
 
 
 
 
 
 
 
 
108
  @app.route('/match', methods=['POST'])
109
+ def match(): # MODIFIED: Changed from `async def` to `def` for synchronous Flask.
110
+ """
111
+ Matches a given music track URL to a YouTube Music URL.
112
+ Expects a JSON body with "url".
113
+ """
114
+ data = request.get_json()
115
+ if not data: # ADDED: Check for empty JSON payload.
116
+ logger.error("Match endpoint: No JSON payload received.") # ADDED: Logging.
117
+ return jsonify({"detail": "No JSON payload received."}), 400 # MODIFIED: Flask-style JSON error response.
118
+
119
  track_url = data.get('url')
120
+ # MODIFIED: Added more specific validation for track_url presence and type.
121
+ if not track_url or not isinstance(track_url, str):
122
+ logger.error(f"Match endpoint: Invalid or missing URL: {track_url}") # ADDED: Logging.
123
+ return jsonify({"detail": "Valid 'url' string is required in request body."}), 400 # MODIFIED: Flask-style JSON error response.
124
 
125
+ logger.info(f"Match endpoint: Processing URL: {track_url}") # ADDED: Logging.
 
126
 
127
  track_info = get_song_link_info(track_url)
128
  if not track_info:
129
+ logger.error(f"Match endpoint: Could not fetch track info for URL: {track_url}") # ADDED: Logging.
130
+ # MODIFIED: Flask-style JSON error response instead of HTTPException.
131
+ return jsonify({"detail": "Could not fetch track info from Song.link API."}), 404
132
+
133
+ entity_unique_id = track_info.get("entityUniqueId") # MODIFIED: Used .get() for safer access.
134
+ title = None
135
+ artist = None
136
+
137
+ # MODIFIED: More robust extraction of title and artist with checks and logging.
138
+ if entity_unique_id and entity_unique_id in track_info.get("entitiesByUniqueId", {}):
139
+ main_entity = track_info["entitiesByUniqueId"][entity_unique_id]
140
+ title = main_entity.get("title")
141
+ artist = main_entity.get("artistName")
142
+ logger.info(f"Match endpoint: Found main entity - Title: '{title}', Artist: '{artist}'") # ADDED: Logging.
143
+ else:
144
+ logger.warning(f"Match endpoint: Could not find main entity details for {track_url} using entityUniqueId: {entity_unique_id}") # ADDED: Logging.
145
+ # ADDED: Fallback logic to find title/artist from other entities if main one fails.
146
+ for entity_id, entity_data in track_info.get("entitiesByUniqueId", {}).items():
147
+ if entity_data.get("title") and entity_data.get("artistName"):
148
+ title = entity_data.get("title")
149
+ artist = entity_data.get("artistName")
150
+ logger.info(f"Match endpoint: Using fallback entity - Title: '{title}', Artist: '{artist}' from entity ID {entity_id}") # ADDED: Logging.
151
+ break
152
+ if not title or not artist: # ADDED: Check if title/artist still not found after fallback.
153
+ logger.error(f"Match endpoint: Could not determine title and artist for URL: {track_url}") # ADDED: Logging.
154
+ return jsonify({"detail": "Could not determine title and artist from Song.link info."}), 404 # MODIFIED: Flask-style JSON error.
155
+
156
+
157
+ youtube_url = extract_url(track_info.get("linksByPlatform", {}), "youtube") # MODIFIED: Used .get() for safer access.
158
 
 
 
 
 
 
159
  if youtube_url:
160
+ video_id = None
161
+ # MODIFIED: Improved video_id extraction from youtube_url, handles direct watch links and youtu.be, and strips extra params.
162
+ if "v=" in youtube_url:
163
+ video_id = youtube_url.split("v=")[1].split("&")[0]
164
+ elif "youtu.be/" in youtube_url: # MODIFIED: Handling for youtu.be links if present in song.link
165
+ video_id = youtube_url.split("youtu.be/")[1].split("?")[0]
166
+
167
+ filename = f"{title} - {artist}" if title and artist else "Unknown Track - Unknown Artist"
168
+ logger.info(f"Match endpoint: Found direct YouTube URL: {youtube_url}, Video ID: {video_id}") # ADDED: Logging.
169
+ # MODIFIED: Flask-style JSON response instead of returning dict directly.
170
+ return jsonify({"url": youtube_url, "filename": filename, "track_id": video_id}), 200
171
  else:
172
+ logger.info(f"Match endpoint: No direct YouTube URL. Searching YTMusic with: '{title} - {artist}'") # ADDED: Logging.
173
+ # ADDED: Explicit check if title or artist is missing before searching.
174
+ if not title or not artist:
175
+ logger.error("Match endpoint: Cannot search YTMusic without title and artist.") # ADDED: Logging.
176
+ return jsonify({"detail": "Cannot search on YouTube Music without title and artist information."}), 400 # MODIFIED: Flask-style JSON error.
177
+
178
+ search_query = f'{title} {artist}' # MODIFIED: Changed from '+' to space for a more natural search query.
179
  search_results = ytmusic.search(search_query, filter="songs")
180
+
181
+ if search_results:
182
+ # MODIFIED: Improved logic to pick the first song with a videoId using next() and .get().
183
+ first_song = next((song for song in search_results if song.get('videoId')), None)
184
+ if first_song and first_song.get('videoId'):
185
+ video_id = first_song["videoId"]
186
+ # MODIFIED: Changed ym_url to a standard YouTube watch URL format.
187
+ ym_url = f'https://music.youtube.com/watch?v={video_id}'
188
+ # MODIFIED: More robust filename generation using .get() and providing fallbacks.
189
+ filename = f"{first_song.get('title', title)} - {first_song.get('artists', [{'name': artist}])[0]['name']}"
190
+ logger.info(f"Match endpoint: Found YTMusic search result - URL: {ym_url}, Video ID: {video_id}") # ADDED: Logging.
191
+ # MODIFIED: Flask-style JSON response.
192
+ return jsonify({"filename": filename, "url": ym_url, "track_id": video_id}), 200
193
+ else:
194
+ logger.error(f"Match endpoint: YTMusic search for '{search_query}' yielded no results with a videoId.") # ADDED: Logging.
195
+ # MODIFIED: Flask-style JSON error response.
196
+ return jsonify({"detail": "No matching video ID found on YouTube Music after search."}), 404
197
  else:
198
+ logger.error(f"Match endpoint: YTMusic search for '{search_query}' yielded no results.") # ADDED: Logging.
199
+ # MODIFIED: Flask-style JSON error response.
200
+ return jsonify({"detail": "No results found on YouTube Music for the track."}), 404
201
+ # REMOVED: The final `raise HTTPException` was determined to be unreachable and removed.
202
 
203
 
204