我需要帮助使用pylast的library.add_track功能(python last.fm API封装)

2 投票
1 回答
800 浏览
提问于 2025-04-16 18:31

我在使用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()的网络参数是错的。

修正了上面的错误后,代码又开始报缺少参数的错误。我需要在pylast.py的Library类的add_track()函数中添加以下一行(第1970行)。这是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)

1 个回答

0

[将更新作为答案发布]


对于其他遇到pylast问题的人,我将在下面发布一个正确的示例,说明如何将曲目添加到你的音乐库。我之前传给pylast.Library()的网络参数是错的。

修复了上面的错误后,代码又开始报缺少参数的错误。我需要在pylast.py文件中Library类的add_track()函数(第1970行)添加以下一行。这是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)

撰写回答