Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Last.FM API Help!
#1
Hello,

I am looking to create and share a MusicBrainz Picard plugin that pulls information that automatically rates your music library using info from Last.FM API. However, I am not a very experienced programmer and am not sure how to get the data I need.

I am looking to get the listeners and playcount values from Last.FM GetInfo webservice:

http://ws.audioscrobbler.com/2.0/?method...6e8e33f2ee

I was able to get this data using xml.etree.ElementTree and pylast libraries in python, but I do not know how to get these to work with MusicBrainz Picard.

I wanted to see if anybody had any other ideas to get those values.

Thank you!
Reply
#2
(Jul-26-2018, 08:49 PM)zaz102 Wrote: I wanted to see if anybody had any other ideas to get those values.
A common way is to use Requests and BS.
from bs4 import BeautifulSoup
import requests

url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=0a8b8f968b285654f9b4f16e8e33f2ee'
url_get = requests.get(url)
soup = BeautifulSoup(url_get.content, 'html.parser')
listeners = soup.find('listeners').text
play_count = soup.find('playcount').text
print(listeners)
print(play_count)
Output:
1058399 14397145
Reply
#3
Thanks. This is where I am ignorant though.

I extracted the BS4 library and copied it into my plugin's folder. I was expecting for it to pick it up, but it does not. Instead, I get an error message "ImportError: No module named bs4".

Note that I do not want to install the library the traditional way since it will be used as a plugin. From my experience, MusicBrainz Picard does not pick it up that way.

Any ideas how to have it be picked up by the application.
Reply
#4
In order to import the bs4 library, I tried adding the code:

import sys
sys.path.append('./bs4')
import bs4

I am receiving the same error message.

My path is:

testplugin\
testplugin.py
bs4

Any ideas?
Reply
#5
(Jul-27-2018, 06:36 PM)zaz102 Wrote: Any ideas?
Can do it in a more dirty way using no 3-party modules,can make it easier if using this in a plugin.
import urllib.request
import re

url = urllib.request.urlopen('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=0a8b8f968b285654f9b4f16e8e33f2ee')
site = url.read().decode('utf-8')
listeners = re.search('<listeners>(\d+)', site).group(1)
play_count = re.search('<playcount>(\d+)', site).group(1)
print(listeners, play_count)
Output:
1058705 14402632
Reply
#6
Thank you for your help.

Interestingly enough, the plugin still couldn't find it, but I was able to get it work by copying the library into the plugin folder.

Also, I had to remove the request part from urllib. See below:

import urllib

url = urllib.urlopen('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=0a8b8f968b285654f9b4f16e8e33f2ee')

Not sure why I had to make the changes above, but it's working so I am very happy. Thanks again!
Reply
#7
(Jul-28-2018, 02:58 AM)zaz102 Wrote: Not sure why I had to make the changes above, but it's working so I am very happy. Thanks again!
Because you(or library) use Python 2,i always write code for Python 3 if not specify that code has to be for Python 2.
If it's this library you use MusicBrainz Picard,so do it use Python 3,also a 3 :: Only tag.
Here form setup.py:
'classifiers': [
        'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
        'Development Status :: 5 - Production/Stable',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3 :: Only',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: MacOS',
        'Operating System :: POSIX :: Linux',
        'Topic :: Multimedia :: Sound/Audio',
        'Topic :: Multimedia :: Sound/Audio :: Analysis'
    ]
}
If it woks then all is okay Cool
Reply
#8
You have already helped me so much, but I have one more question.

For the URL request, I actually have artist (Cher) as a variable. It seems when the request is made to the website, it's doesn't properly encode the variable. This becomes apparent when there is a special character (e.g. Sonny & Cher). I believe it is coming up as "&" rather than percent-encoding it to "%26". I tried messing around with urlencode, urlquote, and replace, but I am obviously not doing it correctly. Any ideas on how to solve this issue?
Reply
#9
>>> from urllib.parse import urlencode
>>> 
>>> artist = 'Sonny & Cher'
>>> s = urlencode({'artist': artist})
>>> s
'artist=Sonny+%26+Cher'

>>> # Only last part
>>> s.split('=')[1]
'Sonny+%26+Cher'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020