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

时间:2015-12-14 00:14:40

标签: python api last.fm

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

1 个答案:

答案 0 :(得分:1)

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

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

以下是使用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))