Skip to content

Commit

Permalink
Add some client-side filtering/sorting of releases
Browse files Browse the repository at this point in the history
If the MusicBrainz API won't do this for us, let's do it ourselves. This
significantly improves the quality of the search results when searching
for things that include a release.
  • Loading branch information
mutantmonkey committed Dec 18, 2023
1 parent 0a150d0 commit 09a6b07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ requests
sentry-sdk[flask]
sqlalchemy>=1.2.0
SQLAlchemy-Utils
thefuzz
Unidecode
-e git+https://github.com/wuvt/python-musicbrainzngs#egg=musicbrainzngs
8 changes: 6 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile
#
Expand Down Expand Up @@ -148,6 +148,8 @@ pytz==2021.1
# flask-restful
# google-api-core
# tzlocal
rapidfuzz==3.5.2
# via thefuzz
redis==3.5.3
# via -r requirements.in
requests==2.25.1
Expand Down Expand Up @@ -179,6 +181,8 @@ sqlalchemy==1.4.14
# sqlalchemy-utils
sqlalchemy-utils==0.37.2
# via -r requirements.in
thefuzz==0.20.0
# via -r requirements.in
tzlocal==2.1
# via apscheduler
unidecode==1.2.0
Expand Down
29 changes: 22 additions & 7 deletions trackman/api/v1/track.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import request, session, current_app
from flask_restful import abort
from thefuzz import fuzz, process
from trackman import db, models
from trackman.forms import TrackAddForm
from trackman.lib import find_or_add_track
Expand Down Expand Up @@ -188,14 +189,15 @@ def get(self):
db.func.lower(models.Track.album) == db.func.lower(album))
musicbrainz_search['release'] = album

label = request.args.get('label', '').strip()
if len(label) > 0:
somesearch = True
tracks = tracks.filter(
db.func.lower(models.Track.label) == db.func.lower(label))
# FIXME: disabled since MusicBrainz cannot search by label
#label = request.args.get('label', '').strip()
#if len(label) > 0:
# somesearch = True
# tracks = tracks.filter(
# db.func.lower(models.Track.label) == db.func.lower(label))

# This means there was a bad search, stop searching
# FIXME: MusicBrainz cannot search on label
# FIXME: MusicBrainz cannot search by label
if somesearch is False:
return {
'success': False,
Expand All @@ -205,10 +207,23 @@ def get(self):

results = []

def process_release(r):
if type(r) == dict:
return r['title']
else:
return r

# Search MusicBrainz
mb_results = musicbrainzngs.search_recordings(**musicbrainz_search)
for recording in mb_results['recording-list']:
for release in recording['release-list']:
all_releases = [r for r in recording['release-list']]
if len(album) > 0:
releases = process.extract(album, all_releases,
processor=process_release)
else:
releases = [(r, 0) for r in all_releases]

for release, _ in releases:
t = models.Track(
title=recording['title'],
album=release['title'],
Expand Down

0 comments on commit 09a6b07

Please sign in to comment.