开发者

I need help using the library.add_track feature of pylast (python last.fm api wrapper)

开发者 https://www.devze.com 2023-03-09 12:15 出处:网络
I need help debugging an error received when trying to use the library.add_track function in pylast. I\'ve examined the code but I am not expert in python. I am just trying to utilize this wrapper to

I need help debugging an error received when trying to use the library.add_track function in pylast. I've examined the code but I am not expert in python. I am just trying to utilize this wrapper to add a list of tracks to my library. You can find the pylast code here:

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)

The error I receive is:

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'

I appreciate the help.

UPDATE:

For anyone else having this issue with pylast, I will post a correct example below for adding a track to your library. I was passing the wrong network parameter to pylast.Library().

After fixing the above problem the code started throwing a missing parameter error. I had to add the following line to pylast.py within the Library class's add_track() function (line 1970). This is a necessary parameter for the Last.fm API.

params['artist'] = track.get_artist().get_name()

Working example for adding a track to a User's library:

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
user开发者_运维技巧name = "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)


[Posting the UPDATE as an answer]


For anyone else having this issue with pylast, I will post a correct example below for adding a track to your library. I was passing the wrong network parameter to pylast.Library().

After fixing the above problem the code started throwing a missing parameter error. I had to add the following line to pylast.py within the Library class's add_track() function (line 1970). This is a necessary parameter for the Last.fm API.

params['artist'] = track.get_artist().get_name()

Working example for adding a track to a User's library:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消