我正在尝试获取我在 Spotify 上收听的播客的标题或 ID。
当前使用此端点来播放信息:
response = requests.get("https://api.spotify.com/v1/me/player", headers=headers)
在曲目上,它会返回有关“项目”下的歌曲和艺术家的信息
在播客剧集中,此项目始终为“无”。
这是我当前的代码:
def get_current_track():
if time.time() > globals.token_expiry:
print("Access token expired, refreshing...")
refresh_access_token()
if not globals.access_token:
print("No valid access token available after refresh.")
return None, None, None, 0, 0
headers = {"Authorization": f"Bearer {globals.access_token}"}
response = requests.get("https://api.spotify.com/v1/me/player", headers=headers)
try:
response_json = response.json()
print("JSON Content: -------- ", response_json)
except ValueError:
print("No JSON content found in the response.")
if response.status_code == 200:
playback_info = response.json()
if playback_info and playback_info['is_playing']:
currently_playing_type = playback_info.get('currently_playing_type')
item = playback_info.get('item')
if currently_playing_type == 'track' and item:
track_id = item['id']
track_name = item['name']
artist_name = item['artists'][0]['name']
progress_ms = playback_info['progress_ms']
duration_ms = item['duration_ms']
return track_id, track_name, artist_name, progress_ms, duration_ms
elif currently_playing_type == 'episode':
if item and 'id' in item:
episode_id = item['id']
else:
context = playback_info.get('context')
if context and 'uri' in context:
context_uri = context['uri']
if 'episode' in context_uri:
episode_id = context_uri.split(':')[-1]
else:
print("Context URI does not contain an episode ID.")
return None, None, None, 0, 0
else:
print("No context or item information available for the podcast episode.")
return None, None, None, 0, 0
episode_response = requests.get(f"https://api.spotify.com/v1/episodes/{episode_id}", headers=headers)
if episode_response.status_code == 200:
episode_info = episode_response.json()
track_name = episode_info['name']
artist_name = episode_info['show']['publisher']
progress_ms = playback_info['progress_ms']
duration_ms = episode_info['duration_ms']
return episode_id, track_name, artist_name, progress_ms, duration_ms
else:
print("Failed to get episode information. Response:", episode_response.text)
else:
print("No track or episode is currently playing.")
else:
print("Failed to get playback information. Response:", response.text)
return None, None, None, 0, 0
有人知道这个问题的解决方法吗?
找到解决方案!
从 playack 端点获取剧集信息时,请使用以下命令:
获取https://api.spotify.com/v1/me/player?additional_types=episode