Created
April 5, 2016 10:33
-
-
Save Fedjmike/d96a3d5399f045af8a2d14da7873c16a to your computer and use it in GitHub Desktop.
Work out artist mbids using release mbids, incomplete field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from model import Model, NotFound | |
import musicbrainzngs as mb | |
def mb_get_author_ids(release_mbid): | |
mb.set_useragent("Skiller", "0.0.0", "[email protected]") | |
response = mb.get_release_by_id(release_mbid, includes=["artists"])["release"] | |
return [artist["artist"]["id"] for artist in response["artist-credit"] if isinstance(artist, dict)] | |
class Unidentifiable(Exception): pass | |
def find_artist_mbid(model, artist): | |
def mbid_present(mbid): | |
try: | |
model.query_unique("select id from links where target=?", mbid) | |
return True | |
except NotFound: | |
return False | |
try: | |
print(artist.name, artist.id) | |
except: | |
print(artist.id) | |
(incomplete,) = model.query_unique("select incomplete from artists where id=?", artist.id) | |
if incomplete: | |
return incomplete | |
try: | |
#Find a solo release | |
release = next(release for release in artist.get_releases() if len(release.get_artists()) == 1) | |
except StopIteration: | |
releases = artist.get_releases() | |
if not releases: | |
raise Unidentifiable() | |
else: | |
release = releases[0] | |
release_mbid = model.get_link(release.id, "musicbrainz") | |
authors = mb_get_author_ids(release_mbid) | |
mbids_not_assigned = [mbid for mbid in authors if not mbid_present(mbid)] | |
if len(mbids_not_assigned) == 1: | |
return mbids_not_assigned[0] | |
else: | |
raise Unidentifiable() | |
with Model() as model: | |
for (artist_id,) in model.query("select id from artists"): | |
try: | |
model.get_link(artist_id, "musicbrainz") | |
except NotFound: | |
try: | |
mbid = find_artist_mbid(model, model.get_artist(artist_id)) | |
print("mbid!", mbid) | |
model.add_link(artist_id, "musicbrainz", mbid) | |
except Unidentifiable: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment