我需要帮助调试在尝试使用 pylast 中的 library.add_track 函数时收到的错误。我已经检查了代码,但我不是 python 专家。我只是想利用这个包装器将曲目列表添加到我的库中。您可以在此处找到 pylast 代码:
http://code.google.com/p/pylast/source/browse/trunk/pylast.py
import pylast
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account for Last.fm
API_KEY = "your_key"
API_SECRET = "your_secret"
# In order to perform a write operation you need to authenticate yourself
username = "username"
password_hash = pylast.md5("password")
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret =
API_SECRET, username = username, password_hash = password_hash)
# This is the area causing trouble
library1 = pylast.Library(user = "Username", network = "LastFM")
track1 = network.get_track("Rob Dougan", "Clubbed To Death (Kurayamino Variation)")
library1.add_track(track1)
我收到的错误是:
Traceback (most recent call last):
File "addTrack.py", line 18, in <module>
library1.add_track(track1)
File "/usr/local/lib/python2.6/dist-packages/pylast.py", line 1976, in add_track
self._request("library.addTrack", False, params)
File "/usr/local/lib/python2.6/dist-packages/pylast.py", line 970, in _request
return _Request(self.network, method_name, params).execute(cacheable)
File "/usr/local/lib/python2.6/dist-packages/pylast.py", line 715, in __init__
(self.api_key, self.api_secret, self.session_key) = network._get_ws_auth()
AttributeError: 'str' object has no attribute '_get_ws_auth'
我很感激帮助。
更新:
对于其他遇到 pylast 问题的人,我将在下面发布一个正确的示例,用于将曲目添加到您的库中。我将错误的网络参数传递给 pylast.Library()。
修复上述问题后,代码开始抛出缺少参数错误。我必须在 Library 类的 add_track() 函数(第 1970 行)中将以下行添加到 pylast.py。这是 Last.fm API 的必要参数。
params['artist'] = track.get_artist().get_name()
将曲目添加到用户库的工作示例:
import pylast
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account for Last.fm
API_KEY = "your_key"
API_SECRET = "your_secret"
# In order to perform a write operation you need to authenticate yourself
username = "username"
password_hash = pylast.md5("password")
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret =
API_SECRET, username = username, password_hash = password_hash)
# Get the User's library
library = pylast.Library(user = "username", network = network)
# Add a track
track = network.get_track("Rob Dougan", "Clubbed To Death (Kurayamino Variation)")
library.add_track(track)