通过last.fm API第一次播放曲目的最有效方法是什么



我正试图从上一个.fm API中获得第一次播放曲目。我唯一能弄清楚如何第一次播放给定曲目的方法是迭代方法user.getRecentTracks的所有实例。这是非常低效的。还有其他人有其他建议吗?

API允许您获取此数据的唯一其他方法是使用user.getArtistTracks资源,该资源允许您获取用户的所有曲目播放,并按特定艺术家进行过滤。

API返回一个曲目列表,您可以解析和过滤这些曲目以获得所需的曲目,并可以检索该曲目的scrobble历史记录。

以下是使用Python pylast last.fm API wrapper:的示例

from __future__ import print_function
import pylast
API_KEY = 'API-KEY-HERE'
API_SECRET = 'API-SECRET-HERE'
username = 'your-user-to-authenticate'
password_hash = pylast.md5('your-password')
# Specfy artist and track name here
artist_name = 'Saori@destiny'
track_name = 'GAMBA JAPAN'
# Authenticate and get a session to last.fm (we're using standalone auth)
client = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET,
                              username = username, password_hash = password_hash)
# Get an object representing a specific user
user = client.get_user(username)
# Call get_artist_tracks to retrieve all tracks a user
# has played from a specific artist and filter out all
# playbacks that aren't your track
track_scrobbles = [x for x in user.get_artist_tracks(artist_name)
                   if x.track.title.lower() == track_name.lower()]
# It just so happens that the API returns things in reverse chronological order
# of playtimes (most recent plays first), so you can just take the last item
# in the scrobbled list for the track you want
first_played = track_scrobbles[-1]
# first_played is of type PlayedTrack, which is a named tuple in the lastpy lib
print("You first played {0} by artist {1} on {2}".format(first_played.track.title,
                                                         first_played.track.artist,
                                                         first_played.playback_date))

最新更新