diff --git a/lb_content_resolver/unresolved_recording.py b/lb_content_resolver/unresolved_recording.py index 29f1f7e..1bed543 100755 --- a/lb_content_resolver/unresolved_recording.py +++ b/lb_content_resolver/unresolved_recording.py @@ -1,8 +1,7 @@ -import os +from collections import defaultdict import datetime -import requests -import sys from math import ceil +import requests import peewee @@ -43,7 +42,10 @@ def add(self, recording_mbids): for mbid in recording_mbids: db.execute_sql(query, (mbid, datetime.datetime.now())) - def get(self, num_items, lookup_count): + def get_releases(self, num_items, lookup_count): + """ + Organize the unresolved recordings into releases with a list of recordings. + """ if lookup_count is not None: where_clause = f"WHERE lookup_count >= {lookup_count}" @@ -67,7 +69,7 @@ def get(self, num_items, lookup_count): for chunk in self.chunks(recording_mbids, self.LOOKUP_BATCH_SIZE): args = ",".join(chunk) - params = { "recording_mbids": args, "inc": "artist release" } + params = {"recording_mbids": args, "inc": "artist release"} while True: r = requests.get("https://api.listenbrainz.org/1/metadata/recording", params=params) if r.status_code != 200: @@ -81,18 +83,28 @@ def get(self, num_items, lookup_count): break recording_data.update(dict(r.json())) - results = [] + releases = defaultdict(list) for mbid in recording_mbids: rec = recording_data[mbid] - results.append({ + releases[rec["release"]["mbid"]].append({ "artist_name": rec["artist"]["name"], "artists": rec["artist"]["artists"], "release_name": rec["release"]["name"], "release_mbid": rec["release"]["mbid"], "release_group_mbid": rec["release"]["release_group_mbid"], - "recording_name": "Contact", + "recording_name": rec["recording"]["name"], "recording_mbid": mbid, "lookup_count": lookup_counts[mbid] }) - return results + return releases + + def print_releases(self, releases): + + print("%-50s %-50s" % ("RELEASE", "ARTIST")) + for release_mbid in sorted(releases.keys(), key=lambda a: releases[a][0]["release_name"]): + rel = releases[release_mbid] + print("%-60s %-50s" % (rel[0]["release_name"][:59], rel[0]["artist_name"][:49])) + for rec in rel: + print(" %-57s %d lookups" % (rec["recording_name"][:56], rec["lookup_count"])) + print() diff --git a/resolve.py b/resolve.py index d71ceba..b77a7f2 100755 --- a/resolve.py +++ b/resolve.py @@ -162,9 +162,8 @@ def unresolved_releases(count, lookup_count, index_dir): db = SubsonicDatabase(index_dir) db.open_db() urt = UnresolvedRecordingTracker() - recordings = urt.get(num_items=count, lookup_count=lookup_count) - from icecream import ic - ic(recordings) + releases = urt.get_releases(num_items=count, lookup_count=lookup_count) + urt.print_releases(releases) cli.add_command(create)