通过 last.fm API 获取曲目首次播放时间的最有效方法是什么

What is the most efficient way to get the first time a track was played via last.fm API

我正在尝试从 last.fm API 获取第一次播放曲目的时间。我能弄清楚如何获得给定曲目的第一次播放时间的唯一方法是遍历方法 user.getRecentTracks 的所有实例。这是非常低效的。还有其他人有其他建议吗?

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

APIreturns 一个曲目列表,您可以解析和过滤这些曲目以获得您想要的曲目,您还可以检索该曲目的涂鸦历史记录。

下面是一个使用 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))