我一直在使用Python来访问Rdio API,因此决定在Rdio模块中添加几个方法以简化生活。我一直受到阻碍。
这里,作为背景,是该公司提供的一些Rdio Python模块:
class Rdio:
def __init__(self, consumer, token=None):
self.__consumer = consumer
self.token = token
def __signed_post(self, url, params):
auth = om(self.__consumer, url, params, self.token)
req = urllib2.Request(url, urllib.urlencode(params), {'Authorization': auth})
res = urllib2.urlopen(req)
return res.read()
def call(self, method, params=dict()):
# make a copy of the dict
params = dict(params)
# put the method in the dict
params['method'] = method
# call to the server and parse the response
return json.loads(self.__signed_post('http://api.rdio.com/1/', params))
好的,一切都很好。这些功能很好。因此,我决定创建一种方法,将带有key1
的播放列表复制到包含key2
的播放列表中。这是代码:
def copy_playlist(self, key1, key2):
#get track keys from first playlist
playlist = self.call('get', {'keys': key1, 'extras' : 'tracks'})
track_keys = []
for track in tracks:
key = track['key']
track_keys.append(key)
#convert track list into single, comma-separated string (which the API requires)
keys_string = ', '.join(track_keys)
#add the tracks to the second playlist
self.call('addToPlaylist', {'playlist' : key2, 'tracks' : keys_string})
如果我从终端或外部Python文件中执行此代码,此代码可以正常工作,但由于某种原因,当我将其作为Rdio类的一部分包含在内时,然后将{}} {}发送到对象并调用播放列表方法,我总是得到以下错误:
rdio
我似乎无法解决这个问题。可能有一个简单的答案 - 我对编程很陌生 - 但我很难过。
更新:更新了代码格式,这是创建Rdio对象的实际代码:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rdio_extended.py", line 83, in copy_playlist
NameError: global name 'rdio' is not defined
然后这是调用播放列表复制功能的行:
rdio = Rdio((RDIO_CONSUMER_KEY, RDIO_CONSUMER_SECRET), (RDIO_TOKEN, RDIO_TOKEN_SECRET))
这导致上面描述的NameError。