Spotipy-如何从指定索引处的播放列表中获取歌曲?

时间:2019-12-22 04:26:06

标签: python pycharm spotipy

我已经阅读了文档中的offset参数;但是,我不知道如何使用它。到目前为止,这是我的代码。不幸的是,只有前100首歌曲是从播放列表中检索到的。如何更改索引,以便可以从播放列表中检索更多歌曲?

def __init__(self):
    self.root1=Tk()
    self.root1.title("Registration")
    self.root1.config(background='#800000')

    scrollbar = Scrollbar(self.root1,orient="vertical")
    Label(self.root1,text='',fg='lightblue',bg='lightblue',height=60,width=12).grid(row=0,column=0,rowspan=12)
    Label(self.root1,text='Registration Form: ',font=('arial',20,'bold'),bg='#800000').grid(row=0,column=1)

    l1=Label(self.root1,text='Username: ',font=('arial',20,'bold'),bg='#800000')
    l2=Label(self.root1,text='Contact No. :',font=('arial',20,'bold'),bg='#800000')
    l3=Label(self.root1,text='First Name: ',font=('arial',20,'bold'),bg='#800000')
    l4=Label(self.root1,text='Middle Name: ',font=('arial',20,'bold'),bg='#800000')
    l5=Label(self.root1,text='Last Name: ',font=('arial',20,'bold'),bg='#800000')
    l6=Label(self.root1,text='Gender: ',font=('arial',20,'bold'),bg='#800000')
    l7=Label(self.root1,text='Date of birth: ',font=('arial',20,'bold'),bg='#800000')
    l8=Label(self.root1,text='Age: ',font=('arial',20,'bold'),bg='#800000')
    l9=Label(self.root1,text='Aadhar card no. : ',font=('arial',20,'bold'),bg='#800000')
    l10=Label(self.root1,text='State: ',font=('arial',20,'bold'),bg='#800000')
    l11=Label(self.root1,text='City: ',font=('arial',20,'bold'),bg='#800000')
    l12=Label(self.root1,text='Flat,House no.: ',font=('arial',20,'bold'),bg='#800000')
    l13=Label(self.root1,text='Street,area,village: ',font=('arial',20,'bold'),bg='#800000')
    l14=Label(self.root1,text='landmark: ',font=('arial',20,'bold'),bg='#800000')

    self.uname=StringVar()
    self.lname=StringVar()
    self.cno=StringVar()
    self.fname=StringVar()
    self.mname=StringVar()
    self.gen=StringVar()
    self.dob=StringVar()
    self.age=IntVar()
    self.aadhar=StringVar()
    self.state=StringVar()
    self.city=StringVar()
    self.flat=StringVar()
    self.street=StringVar()
    self.landmark=StringVar()
    e1=Entry(self.root1,textvariable=self.uname,font=('arial',16))
    e2=Entry(self.root1,textvariable=self.cno,font=('arial',16))
    e3=Entry(self.root1,textvariable=self.fname,font=('arial',16))
    e4=Entry(self.root1,textvariable=self.mname,font=('arial',16))
    e5=Entry(self.root1, textvariable=self.lname,font=('arial',16))
    e6_0=Radiobutton(self.root1,text='MALE',variable=self.gen,value='male')
    e6_1=Radiobutton(self.root1,text='FEMALE',variable=self.gen,value='female')
    e7=Entry(self.root1, textvariable=self.dob,font=('arial',16))
    e8=Entry(self.root1, textvariable=self.age,font=('arial',16))
    e9=Entry(self.root1, textvariable=self.aadhar,font=('arial',16))
    e10=Entry(self.root1, textvariable=self.state,font=('arial',16))
    e11=Entry(self.root1, textvariable=self.city,font=('arial',16))
    e12=Entry(self.root1, textvariable=self.flat,font=('arial',16))
    e13=Entry(self.root1, textvariable=self.street,font=('arial',16))
    e14=Entry(self.root1, textvariable=self.landmark,font=('arial',16))
    b1=Button(self.root1,text='SUBMIT',font=('arial',20,'bold'),width=10,height=2,fg='white',bg='grey',activebackground='#669999')

    l1.grid(row=1,column=1)
    e1.grid(row=1,column=2)

    l2.grid(row=2,column=1)
    e2.grid(row=2,column=2)      

    l3.grid(row=3,column=1)
    e3.grid(row=3,column=2)

    l4.grid(row=3,column=4)
    e4.grid(row=3,column=5)

    l5.grid(row=4,column=1)
    e5.grid(row=4,column=2)    

    l6.grid(row=5,column=1)
    e6_0.grid(row=5,column=2)
    e6_1.grid(row=5,column=3)    

    l7.grid(row=6,column=1)
    e7.grid(row=6,column=2)

    l8.grid(row=6,column=4)
    e8.grid(row=6,column=5)

    l9.grid(row=7,column=1)
    e9.grid(row=7,column=2)

    l10.grid(row=8,column=1)
    e10.grid(row=8,column=2)

    l11.grid(row=8,column=4)
    e11.grid(row=8,column=5)

    l12.grid(row=9,column=1)
    e12.grid(row=9,column=2)

    l13.grid(row=10,column=1)
    e13.grid(row=10,column=2)

    l14.grid(row=11,column=1)
    e14.grid(row=11,column=2)

    b1.grid(row=12,column=1)
    #set the scroller in grid
    scrollbar.grid( row=0, column=7,rowspan=12)
    self.root1.mainloop()

1 个答案:

答案 0 :(得分:0)

我编写的这个自定义类扩展了Spotipy库提供的功能,它具有处理偏移量的包装器功能。

def user_playlist_tracks_full(spotify, user, playlist_id=None, fields=None, market=None):
    """ Get full details of the tracks of a playlist owned by a user.
        Parameters:
            - spotify - spotipy instance
            - 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.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.user_playlist_tracks(
            user, playlist_id, fields=fields, limit=100, offset=len(results), market=market
        )
        results.extend(response["items"])

    return results

此代码可能足以说明您必须执行的操作,循环遍历并每次更改偏移量。

完整的类是in a standalone gist that should work,在这个示例中,我只是将self替换为spotify