pylast: get_userplaycount() always returning 0

What did you do?

Tried to get the user play count for a track.

What did you expect to happen?

Get the correct value. 56 or more in this case.

What actually happened?

Always returns 0.

What versions of OS, Python and pylast are you using?

  • Linux 5.2.0-rc3
  • Python 3.7.3
  • pylast 3.1.0

Please include code that reproduces the issue.

>>> import pylast
>>> network = pylast.LastFMNetwork(api_key='xxx', api_secret='xxx')
>>> track = pylast.Track(artist='Slayer', title='Seasons in the Abyss', network=network, username='falso')
>>> track.get_userplaycount()
0

I also looked at the unit tests, and it seems do be doing it the same way I do, but it its testing for a track that doesnt exist so probably always returns 0.

https://github.com/pylast/pylast/blob/6a1a5a450ef14feb82e87f22805434873ce55b6d/tests/test_track.py#L48-L60 If I make the API request on the browser:

http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=xxx&artist=Slayer&track=Seasons in the Abyss&username=falso&format=json

It returns the “userplaycount” attribute there, and its not 0.


Is this a bug, or I’m using it wrongly?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (6 by maintainers)

Most upvoted comments

@hugovk It works !

Heres an updated diff:

--- ../src/pylast/__init__.py   2019-06-17 20:51:29.638747546 +0100
+++ pylast.py   2019-06-17 23:22:52.371755024 +0100
@@ -929,6 +929,13 @@
             data.append("=".join((name, url_quote_plus(_string(self.params[name])))))
         data = "&".join(data)

+        if "api_sig" in self.params.keys():
+            method = "POST"
+            url_parameters = ""
+        else:
+            method = "GET"
+            url_parameters = "?" + data
+
         headers = {
             "Content-type": "application/x-www-form-urlencoded",
             "Accept-Charset": "utf-8",
@@ -946,10 +953,8 @@

             try:
                 conn.request(
-                    method="POST",
-                    url="https://" + host_name + host_subdir,
-                    body=data,
-                    headers=headers,
+                    url="https://" + host_name + host_subdir + url_parameters,
+                    method=method, body=data, headers=headers
                 )
             except Exception as e:
                 raise NetworkError(self.network, e)
@@ -958,7 +963,9 @@
             conn = HTTPSConnection(context=SSL_CONTEXT, host=host_name)

             try:
-                conn.request(method="POST", url=host_subdir, body=data, headers=headers)
+                conn.request(
+                    url=host_subdir + url_parameters, body=data,
+                    method=method, headers=headers)
             except Exception as e:
                 raise NetworkError(self.network, e)