Skip to content

Commit

Permalink
Labs API upgrade fixes
Browse files Browse the repository at this point in the history
Some uses of the labs api endpoints upgraded recently went un-noticed. Fixing
those.
  • Loading branch information
amCap1712 committed May 8, 2024
1 parent c9883a4 commit a0639d6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 46 deletions.
4 changes: 2 additions & 2 deletions listenbrainz/db/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ def get_playlist_recordings_metadata(mb_curs, ts_curs, playlist: Playlist) -> Pl

for rec, row in zip(playlist.recordings, rows):
rec.artist_credit = row.get("artist_credit_name", "")
if "[artist_credit_mbids]" in row and row["[artist_credit_mbids]"] is not None:
rec.artist_mbids = [UUID(mbid) for mbid in row["[artist_credit_mbids]"]]
if "artist_credit_mbids" in row and row["artist_credit_mbids"] is not None:
rec.artist_mbids = [UUID(mbid) for mbid in row["artist_credit_mbids"]]
rec.title = row.get("recording_name", "")
rec.release_name = row.get("release_name", "")
rec.duration_ms = row.get("length", "")
Expand Down
2 changes: 1 addition & 1 deletion listenbrainz/db/popularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def get_top_recordings_for_artist(db_conn, ts_conn, artist_mbid, count=None):
data.pop("original_recording_mbid", None)
data.update({
"artist_name": data.pop("artist_credit_name"),
"artist_mbids": data.pop("[artist_credit_mbids]"),
"artist_mbids": data.pop("artist_credit_mbids"),
"total_listen_count": recording["total_listen_count"],
"total_user_count": recording["total_user_count"],
"release_color": releases_color.get(str(data["release_mbid"]), {})
Expand Down
1 change: 1 addition & 0 deletions listenbrainz/labs_api/labs/api/base_mbid_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BaseMBIDMappingOutput(BaseModel):
artist_mbids: Optional[list[UUID]]
release_mbid: Optional[UUID]
recording_mbid: Optional[UUID]
match_type: int


class BaseMBIDMappingQuery(Query):
Expand Down
7 changes: 5 additions & 2 deletions listenbrainz/labs_api/labs/tests/test_mbid_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"artist_mbids": ["a3cb23fc-acd3-4ce0-8f36-1e5aa6a18432"],
"artist_credit_name": "U2",
"index": 0,
"match_type": 4,
"recording_arg": "gloria",
"recording_mbid": "398a5f12-80ba-4d29-8b6b-bfe2176341a6",
"recording_name": "Gloria",
Expand All @@ -110,6 +111,7 @@
"artist_mbids": ["8f6bd1e4-fbe1-4f50-aa9b-94c450ec0f11"],
"artist_credit_name": "Portishead",
"index": 1,
"match_type": 4,
"recording_arg": "strangers",
"recording_mbid": "e97f805a-ab48-4c52-855e-07049142113d",
"recording_name": "Strangers",
Expand All @@ -122,6 +124,7 @@
"artist_mbids": ["8f6bd1e4-fbe1-4f50-aa9b-94c450ec0f11"],
"artist_credit_name": "Portishead",
"index": 2,
"match_type": 1,
"recording_arg": "glory box (feat. your mom)",
"recording_mbid": "145f5c43-0ac2-4886-8b09-63d0e92ded5d",
"recording_name": "Glory Box",
Expand All @@ -137,6 +140,7 @@
"artist_mbids": ["8f6bd1e4-fbe1-4f50-aa9b-94c450ec0f11"],
"artist_credit_name": "Portishead",
"index": 0,
"match_type": 3,
"recording_arg": "strangers a",
"recording_mbid": "e97f805a-ab48-4c52-855e-07049142113d",
"recording_name": "Strangers",
Expand Down Expand Up @@ -171,7 +175,7 @@ def test_basics(self):
q.outputs().__fields__.keys(),
['index', 'artist_credit_arg', 'recording_arg', 'artist_credit_name',
'artist_mbids', 'release_name', 'recording_name', 'release_mbid', 'recording_mbid',
'artist_credit_id'])
'artist_credit_id', 'match_type'])

@patch('typesense.documents.Documents.search')
def test_fetch(self, search):
Expand All @@ -180,7 +184,6 @@ def test_fetch(self, search):
q = MBIDMappingQuery()
resp = q.fetch(json_request_0, RequestSource.json_post)
self.assertEqual(len(resp), 3)
self.maxDiff = None
self.assertDictEqual(json.loads(resp[0].json()), json_response_0[0])
self.assertDictEqual(json.loads(resp[1].json()), json_response_0[1])
self.assertDictEqual(json.loads(resp[2].json()), json_response_0[2])
Expand Down
61 changes: 33 additions & 28 deletions listenbrainz/mbid_mapping_writer/matcher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import uuid
from operator import itemgetter
from operator import attrgetter

import sqlalchemy
import psycopg2
from flask import current_app
from psycopg2.extras import execute_values
from listenbrainz.labs_api.labs.api.mbid_mapping import MBIDMappingQuery
from listenbrainz.mbid_mapping_writer.mbid_mapper import MATCH_TYPES, MATCH_TYPE_NO_MATCH, MATCH_TYPE_EXACT_MATCH
from listenbrainz.labs_api.labs.api.artist_credit_recording_lookup import ArtistCreditRecordingLookupQuery
from datasethoster import RequestSource
from listenbrainz.labs_api.labs.api.mbid_mapping import MBIDMappingQuery, MBIDMappingInput
from listenbrainz.mbid_mapping_writer.mbid_mapper import MATCH_TYPES, MATCH_TYPE_EXACT_MATCH
from listenbrainz.labs_api.labs.api.artist_credit_recording_lookup import ArtistCreditRecordingLookupQuery, \
ArtistCreditRecordingLookupInput
from listenbrainz.db import timescale


Expand Down Expand Up @@ -69,7 +68,7 @@ def process_listens(app, listens, priority):
for typ in MATCH_TYPES:
stats[typ] = 0

msids = {str(listen['recording_msid']): listen for listen in listens}
msids = {str(listen["recording_msid"]): listen for listen in listens}
stats["total"] = len(msids)
if priority == NEW_LISTEN:
stats["listen_count"] += len(msids)
Expand Down Expand Up @@ -108,8 +107,8 @@ def process_listens(app, listens, priority):
# For all listens that are not matched, enter a no match entry, so we don't
# keep attempting to look up more listens.
for listen in remaining_listens:
matches.append((listen['recording_msid'], None, None, None, None, None, None, None, MATCH_TYPES[0]))
stats['no_match'] += 1
matches.append((listen["recording_msid"], None, None, None, None, None, None, None, MATCH_TYPES[0]))
stats["no_match"] += 1

stats["processed"] += len(matches)

Expand Down Expand Up @@ -199,40 +198,46 @@ def lookup_listens(app, listens, stats, exact, debug):
return [], [], stats

if debug:
app.logger.info(f"""Lookup (exact {exact}) '{listens[0]["data"]["artist_name"]}', '{listens[0]["data"]["track_name"]}'""")
app.logger.info(f"""Lookup (exact {exact}) "{listens[0]["data"]["artist_name"]}", "{listens[0]["data"]["track_name"]}" """)

if exact:
q = ArtistCreditRecordingLookupQuery(debug=debug)
ModelT = ArtistCreditRecordingLookupInput
else:
q = MBIDMappingQuery(timeout=SEARCH_TIMEOUT, remove_stop_words=True, debug=debug)
ModelT = MBIDMappingInput

params = []
for listen in listens:
params.append({'[artist_credit_name]': listen["track_metadata"]["artist_name"],
'[recording_name]': listen["track_metadata"]["track_name"]})
params.append(ModelT(
artist_credit_name=listen["track_metadata"]["artist_name"],
recording_name=listen["track_metadata"]["track_name"]
))

rows = []
hits = q.fetch(params)
for hit in sorted(hits, key=itemgetter("index"), reverse=True):
listen = listens[hit["index"]]
hits = q.fetch(params, RequestSource.json_post)
for hit in sorted(hits, key=attrgetter("index"), reverse=True):
listen = listens[hit.index]

if exact:
hit["match_type"] = MATCH_TYPE_EXACT_MATCH
stats[MATCH_TYPES[hit["match_type"]]] += 1
rows.append((listen['recording_msid'],
hit["recording_mbid"],
hit["release_mbid"],
hit["release_name"],
hit["artist_mbids"],
hit["artist_credit_id"],
hit["artist_credit_name"],
hit["recording_name"],
MATCH_TYPES[hit["match_type"]]))
match_type = MATCH_TYPE_EXACT_MATCH
else:
match_type = hit.match_type
stats[MATCH_TYPES[match_type]] += 1
rows.append((listen["recording_msid"],
str(hit.recording_mbid),
str(hit.release_mbid),
hit.release_name,
[str(artist_mbid) for artist_mbid in hit.artist_mbids],
hit.artist_credit_id,
hit.artist_credit_name,
hit.recording_name,
MATCH_TYPES[match_type]))

if debug:
app.logger.info("\n".join(q.get_debug_log_lines()))

listens.pop(hit["index"])
listens.pop(hit.index)
if len(listens) == 0:
break

Expand Down
27 changes: 14 additions & 13 deletions listenbrainz/webserver/views/metadata_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from brainzutils.ratelimit import ratelimit
from datasethoster import RequestSource
from flask import Blueprint, request, jsonify, current_app

from listenbrainz.db.mbid_manual_mapping import create_mbid_manual_mapping, get_mbid_manual_mapping
from listenbrainz.db.metadata import get_metadata_for_recording, get_metadata_for_artist, get_metadata_for_release_group
from listenbrainz.db.model.mbid_manual_mapping import MbidManualMapping
from listenbrainz.labs_api.labs.api.artist_credit_recording_lookup import ArtistCreditRecordingLookupQuery
from listenbrainz.labs_api.labs.api.artist_credit_recording_release_lookup import \
ArtistCreditRecordingReleaseLookupQuery
ArtistCreditRecordingReleaseLookupQuery, ArtistCreditRecordingReleaseLookupInput
from listenbrainz.mbid_mapping_writer.mbid_mapper import MBIDMapper
from listenbrainz.webserver import ts_conn
from listenbrainz.webserver.decorators import crossdomain
Expand Down Expand Up @@ -210,11 +211,11 @@ def metadata_release_group():


def process_results(match, metadata, incs):
recording_mbid = match["recording_mbid"]
recording_mbid = str(match["recording_mbid"])
result = {
"recording_mbid": recording_mbid,
"release_mbid": match["release_mbid"],
"artist_mbids": match["artist_mbids"],
"release_mbid": str(match["release_mbid"]),
"artist_mbids": [str(artist_mbid) for artist_mbid in match["artist_mbids"]],
"recording_name": match["recording_name"],
"release_name": match["release_name"],
"artist_credit_name": match["artist_credit_name"]
Expand Down Expand Up @@ -263,24 +264,24 @@ def get_mbid_mapping():
incs = parse_incs(request.args.get("inc")) if metadata else []

params = [
{
"[artist_credit_name]": artist_name,
"[recording_name]": recording_name,
"[release_name]": release_name
}
ArtistCreditRecordingReleaseLookupInput(
artist_credit_name=artist_name,
recording_name=recording_name,
release_name=release_name or "",
)
]

try:
if release_name:
q = ArtistCreditRecordingReleaseLookupQuery(debug=False)
exact_results = q.fetch(params)
exact_results = q.fetch(params, RequestSource.json_post)
if exact_results:
return process_results(exact_results[0], metadata, incs)
return process_results(exact_results[0].dict(), metadata, incs)

q = ArtistCreditRecordingLookupQuery(debug=False)
exact_results = q.fetch(params)
exact_results = q.fetch(params, RequestSource.json_post)
if exact_results:
return process_results(exact_results[0], metadata, incs)
return process_results(exact_results[0].dict(), metadata, incs)

q = MBIDMapper(timeout=10, remove_stop_words=True, debug=False, retry_on_timeout=False)
fuzzy_result = q.search(artist_name, recording_name, release_name)
Expand Down

0 comments on commit a0639d6

Please sign in to comment.