Skip to content

Instantly share code, notes, and snippets.

@pr3ssh
Last active October 2, 2018 09:56

Revisions

  1. pr3ssh revised this gist Oct 2, 2018. 3 changed files with 78 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions musicbrainz-multiple-artist-api.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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
    43 changes: 43 additions & 0 deletions musicbrainz-multiple-artist.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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
    File renamed without changes.
  2. pr3ssh created this gist Oct 1, 2018.
    42 changes: 42 additions & 0 deletions musicbrainz.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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])