python - 有没有一种方法可以使用 Spotipy 从播放列表中检索所有轨道?

标签 python spotipy

我正在尝试从 Spotify 的一个播放列表中提取所有歌曲。使用 Python 中的 spotipy 包来帮助实现这一点。下面是我目前的代码,它只有 100 首轨道。我尝试在互联网上寻找解决方案,但似乎没有任何效果。下面是我到目前为止的代码。

#Import Library
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import spotipy
from matplotlib import style
from spotipy import util
from spotipy.oauth2 import SpotifyClientCredentials 

#Set up Connection 
client_id = 'id' #Need to create developer profile
client_secret = 'secret'
username = 'username' #Store username
scope = 'user-library-read playlist-modify-public playlist-read-private'
redirect_uri='uri'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, 
client_secret=client_secret)#Create manager for ease
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
token = util.prompt_for_user_token(username, scope, client_id, 
client_secret, redirect_uri)

if token:
 sp = spotipy.Spotify(auth=token)
else:
 print("Can't get token for", username)

#Connect to the best playlist, like ever 
playlist_id = 'playlist'
playlist = sp.user_playlist(username,playlist_id)

#Extract list of songs/tracks
tracks = playlist["tracks"];
songs = tracks["items"];

track_ids = []
track_names = []

for i in range(0, len(songs)):
 if songs[i]['track']['id'] != None: # Removes  local tracks, if any
    track_ids.append(songs[i]['track']['id'])
    track_names.append(songs[i]['track']['name'])

最佳答案

您可以使用偏移量来收集多次调用的结果。此函数返回轨道对象列表。

def user_playlist_tracks_full(spotify_connection, user, playlist_id=None, fields=None, market=None):
    """ Get full details of the tracks of a playlist owned by a user.
        https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlists-tracks/

        Parameters:
            - user - the id of the user
            - playlist_id - the id of the playlist
            - fields - which fields to return
            - market - an ISO 3166-1 alpha-2 country code.
    """

    # first run through also retrieves total no of songs in library
    response = spotify_connection.user_playlist_tracks(user, playlist_id, fields=fields, limit=100, market=market)
    results = response["items"]

    # subsequently runs until it hits the user-defined limit or has read all songs in the library
    while len(results) < response["total"]:
        response = spotify_connection.user_playlist_tracks(
            user, playlist_id, fields=fields, limit=100, offset=len(results), market=market
        )
        results.extend(response["items"])

关于python - 有没有一种方法可以使用 Spotipy 从播放列表中检索所有轨道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55690063/

相关文章:

python - Spotify - 删除使用 None 值创建的访问 token

python - django spotify api python http发布500错误

python - Google Colab - Spotipy 没有将我重定向到指定的redirect_uri

python - 如何使用 Python 和 Django 创建 SaaS 应用程序

python - 使用 scrapy : defining path to Django project 访问 Django 模型

python-2.7 - 使用 Spotipy 设置 Spotify 凭据

python - 使用 Spotipy 将轨道添加到 Spotify 中的播放列表

python - 为什么 <path to conda> deactivate 不起作用?

Python theano : how does the R-operation work?

python - 如何在 Django 管理 View 中显示整个现有的 postgres 数据库