This commit is contained in:
Lars Noack 2022-10-19 12:56:56 +02:00
parent a1fcbf12aa
commit 9baca4d446

View File

@ -1,6 +1,4 @@
import os.path import os.path
from tkinter.messagebox import NO
import musicbrainzngs import musicbrainzngs
import pandas as pd import pandas as pd
@ -9,6 +7,15 @@ musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeI
KNOWN_KIND_OF_OPTIONS = ["artist", "release", "track"] KNOWN_KIND_OF_OPTIONS = ["artist", "release", "track"]
def get_elem_from_obj(current_object, keys: list, after_process=lambda x: x):
current_object = current_object
for key in keys:
if key in current_object or (type(key) == int and key < len(current_object)):
current_object = current_object[key]
else:
return None
return after_process(current_object)
class Search: class Search:
def __init__(self, query: str = None, artist: str = None, temp: str = "temp"): def __init__(self, query: str = None, artist: str = None, temp: str = "temp"):
@ -117,15 +124,15 @@ class Search:
recording_data = result['recording'] recording_data = result['recording']
print(result) print(result)
isrc = None if 'isrc-list' not in recording_data else recording_data['isrc-list'][0] isrc = get_elem_from_obj(recording_data, ['isrc-list', 0])
print(isrc)
release_data = recording_data['release-list'][0] release_data = recording_data['release-list'][0]
mb_release_id = release_data['id'] mb_release_id = release_data['id']
title = recording_data['title'] title = recording_data['title']
artist = [artist_['artist']['name'] for artist_ in recording_data['artist-credit']]
mb_artist_ids = [artist_['artist']['id'] for artist_ in recording_data['artist-credit']] artist = [get_elem_from_obj(artist_, ['artist', 'name']) for artist_ in recording_data['artist-credit']]
mb_artist_ids = [get_elem_from_obj(artist_, ['artist', 'id']) for artist_ in recording_data['artist-credit']]
def get_additional_artist_info(mb_id_): def get_additional_artist_info(mb_id_):
r = musicbrainzngs.get_artist_by_id(mb_id_, includes=["releases"]) r = musicbrainzngs.get_artist_by_id(mb_id_, includes=["releases"])
@ -153,8 +160,8 @@ class Search:
album_id = release_data['id'] album_id = release_data['id']
album = release_data['title'] album = release_data['title']
year = release_data['date'].split("-")[0] year = get_elem_from_obj(release_data, ['date'], lambda x: x.split("-")[0])
date = release_data['date'] date = get_elem_from_obj(release_data, ['date'])
if is_various_artist is None or track is None or total_tracks is None: if is_various_artist is None or track is None or total_tracks is None:
is_various_artist, track, total_tracks = get_additional_release_info(album_id) is_various_artist, track, total_tracks = get_additional_release_info(album_id)
if album_sort is None: if album_sort is None: