Skip to content

Instantly share code, notes, and snippets.

@pr3ssh
Last active October 2, 2018 09:56
Show Gist options
  • Save pr3ssh/451f81f35e78357c8def23510c47a6a5 to your computer and use it in GitHub Desktop.
Save pr3ssh/451f81f35e78357c8def23510c47a6a5 to your computer and use it in GitHub Desktop.
Python wrapper to get musical tags of an artist using MusicBrainz as a database
import requests
import argparse
import sys
import json
# LOCAL FUNCTIONS
def encode_artist_name(value):
return value.replace(' ', '+')
# ARGUMENT
parser = argparse.ArgumentParser(description='Python wrapper to get musical tags of an artist using MusicBrainz as a database')
parser.add_argument('artists', type=str, nargs='+', help='Artists name')
args = parser.parse_args()
# MAIN CODE
for artist in args.artists:
try:
response = requests.get("https://musicbrainz.org/ws/2/artist/?query={}&fmt=json".format(encode_artist_name(artist)))
artist_data = json.loads(response.text)['artists'][0]
if 'tags' in artist_data.keys():
tags = artist_data['tags']
if tags == []:
print("Nobody has tagged {} yet".format(artist))
else:
print(artist + ": " + ', '.join([tag['name'] for tag in tags]))
else:
print("Nobody has tagged {} yet".format(artist))
except:
print(sys.exc_info()[0])
raise
from bs4 import BeautifulSoup
import requests
import argparse
import sys
# LOCAL FUNCTIONS
def get_soup_from_url(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
return soup
def encode_artist_name(value):
return value.replace(' ', '+')
# ARGUMENT
parser = argparse.ArgumentParser(description='Python wrapper to get musical tags of an artist using MusicBrainz as a database')
parser.add_argument('artists', type=str, nargs='+', help='Artists name')
args = parser.parse_args()
# MAIN CODE
for artist in args.artists:
try:
baseurl = 'https://musicbrainz.org'
soup = get_soup_from_url(
"{}/search?query={}&type=artist&method=indexed"
.format(baseurl, encode_artist_name(artist)))
artist_link = soup.select('#content > table > tbody > tr > td > a')
soup = get_soup_from_url(
"{}{}/tags"
.format(baseurl, artist_link[0]['href']))
tags = soup.select('#all-tags > .tag-list > li > a > bdi')
if tags == []:
print("Nobody has tagged {} yet".format(artist))
else:
print(artist + ": " + ', '.join([tag.text for tag in tags]))
except:
print(sys.exc_info()[0])
raise
from bs4 import BeautifulSoup
import requests
import argparse
# LOCAL FUNCTIONS
def get_soup_from_url(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
return soup
def encode_artist_name(value):
return value.replace(' ', '+')
# ARGUMENT
parser = argparse.ArgumentParser(description='Python wrapper to get musical tags of an artist using MusicBrainz as a database')
parser.add_argument('artist', type=str, help='Artist name')
args = parser.parse_args()
# MAIN CODE
try:
artist = args.artist
baseurl = 'https://musicbrainz.org'
soup = get_soup_from_url(
"{}/search?query={}&type=artist&method=indexed"
.format(baseurl, encode_artist_name(artist)))
artist_link = soup.select('#content > table > tbody > tr > td > a')
soup = get_soup_from_url(
"{}{}/tags"
.format(baseurl, artist_link[0]['href']))
tags = soup.select('#all-tags > .tag-list > li > a > bdi')
except e:
tags = []
if tags == []:
print("Nobody has tagged this artist yet")
else:
print([tag.text for tag in tags])
@pr3ssh
Copy link
Author

pr3ssh commented Oct 1, 2018

How to use it

Install beautifulsoup and requests packages (with pip) and then execute

python musicbrainz-multiple-artist-api.py "East 17" "Jackson 5"
python musicbrainz-multiple-artist.py Oasis U2
python musicbrainz-one-artist.py Triana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment