Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it work on python3 #1

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Regular
album='OK Computer',
)

print resp
print(resp)


Asynchronous (uses ``tornado.httpclient.AsyncHTTPClient``)
Expand Down
9 changes: 5 additions & 4 deletions examples/tornado_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import json
import logging

import tornado.web
import tornado.gen
Expand Down Expand Up @@ -45,21 +46,21 @@ def back_from_lastfm(self):
)
token = self.get_argument('token')

print 'Fetching session...'
logging.info('Fetching session...')
session = yield client.auth.get_session(token)

client.session_key = session['key']

print 'Fetching user info...'
logging.info('Fetching user info...')
user = yield client.user.get_info()

print 'Fetching tracks and friends simultaneously...'
logging.info('Fetching tracks and friends simultaneously...')
tracks, friends = yield [
client.user.get_recent_tracks(user=user['name'], limit=3),
client.user.get_friends(user=user['name'], limit=3)
]

print 'Finishing.'
logging.info('Finishing.')

self.set_header('Content-Type', 'text/plain')
self.write('You:\n\n')
Expand Down
13 changes: 9 additions & 4 deletions lastfmclient/async.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from __future__ import unicode_literals
import json
from urllib import urlencode

try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode

from tornado.gen import coroutine, Return
from tornado.httpclient import AsyncHTTPClient
Expand Down Expand Up @@ -28,8 +33,7 @@ def call(self, http_method, method, auth, params):
url = API_URL

params = self._get_params(method, params, auth)
params = urlencode({k: unicode(v).encode('utf8')
for k, v in params.items()})
params = urlencode(params)
if http_method == 'POST':
body = params
else:
Expand All @@ -41,5 +45,6 @@ def call(self, http_method, method, auth, params):
body=body)
if response.error is not None:
response.rethrow()
data = self._process_response_data(json.loads(response.body))
body = response.body.decode('utf-8')
data = self._process_response_data(json.loads(body))
raise Return(data)
16 changes: 12 additions & 4 deletions lastfmclient/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from hashlib import md5

from .api import BaseClient
Expand Down Expand Up @@ -95,6 +96,14 @@ def _get_params(self, method, params, auth):
needs_auth = auth or getting_session or (
method == 'user.getInfo' and 'user' not in params)

for key, value in params.items():
if not isinstance(value, str):
try:
value = str(value)
except UnicodeEncodeError:
value = value.encode('utf-8')
params[key] = value

if needs_auth:
if not getting_session:
assert self.session_key, 'Missing session key.'
Expand All @@ -107,10 +116,10 @@ def _get_params(self, method, params, auth):
def _get_sig(self, params):
"""Create a signature as per http://www.last.fm/api/authspec#8."""
exclude = {'format', 'callback'}
sig = ''.join(k + unicode(v).encode('utf8') for k, v
sig = ''.join(k + v for k, v
in sorted(params.items()) if k not in exclude)
sig += self.api_secret
return md5(sig).hexdigest()
return md5(sig.encode('utf-8')).hexdigest()

def _process_response_data(self, data):
"""
Expand All @@ -126,9 +135,8 @@ def _process_response_data(self, data):
)

if isinstance(data, dict):
keys = data.keys()
keys = list(data.keys())
if len(keys) == 1:
return data[keys[0]]

return data

12 changes: 11 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@
packages=['lastfmclient'],
install_requires=[
'requests>=1.0.4'
]
],
classifiers=[
'Development Status :: 5 - Production/Stable',
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
"Topic :: Software Development :: Libraries :: Python Modules",
],
)