Merge pull request #8 from lexitronic/fix-fallback-search
Fix fallback search when checking if track exists on Deezer
This commit is contained in:
@@ -72,6 +72,11 @@ def _sim(a: str, b: str) -> float:
|
|||||||
return 0.0
|
return 0.0
|
||||||
return SequenceMatcher(None, a, b).ratio()
|
return SequenceMatcher(None, a, b).ratio()
|
||||||
|
|
||||||
|
# Clean for searching on Deezer
|
||||||
|
def _remove_parentheses(string: str) -> str:
|
||||||
|
# remove () and [] and {}, as well as anything inside
|
||||||
|
return re.sub(r'\{[^)]*\}', '', re.sub(r'\[[^)]*\]', '', re.sub(r'\([^)]*\)', '', string)))
|
||||||
|
|
||||||
API()
|
API()
|
||||||
|
|
||||||
# Create a logger for the deezspot library
|
# Create a logger for the deezspot library
|
||||||
@@ -419,6 +424,8 @@ class DeeLogin:
|
|||||||
spo_title = track_json.get('name', '')
|
spo_title = track_json.get('name', '')
|
||||||
spo_album_title = (track_json.get('album') or {}).get('name', '')
|
spo_album_title = (track_json.get('album') or {}).get('name', '')
|
||||||
spo_tracknum = int(track_json.get('track_number') or 0)
|
spo_tracknum = int(track_json.get('track_number') or 0)
|
||||||
|
spo_artists = track_json.get('artists') or []
|
||||||
|
spo_main_artist = (spo_artists[0].get('name') if spo_artists else '') or ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dz = API.get_track_json(f"isrc:{spo_isrc}")
|
dz = API.get_track_json(f"isrc:{spo_isrc}")
|
||||||
@@ -435,15 +442,23 @@ class DeeLogin:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Fallback: search by title + album
|
# Fallback: search by title + artist + album
|
||||||
query = f'"{spo_title} {spo_album_title}"'
|
query = f'"track:\'{spo_title}\' artist:\'{spo_main_artist}\' album:\'{spo_album_title}\'"'
|
||||||
try:
|
try:
|
||||||
candidates = API.search_tracks_raw(query, limit=5)
|
candidates = API.search_tracks_raw(query, limit=5)
|
||||||
except Exception:
|
except Exception:
|
||||||
candidates = []
|
candidates = []
|
||||||
|
|
||||||
for cand in candidates:
|
for cand in candidates:
|
||||||
if max(_sim(spo_title, cand.get('title', '')), _sim(spo_title, cand.get('title_short', ''))) < 0.90:
|
title_match_1 = max(
|
||||||
|
_sim(spo_title, dz_json.get('title', '')),
|
||||||
|
_sim(spo_title, dz_json.get('title_short', ''))
|
||||||
|
)
|
||||||
|
title_match_2 = max(
|
||||||
|
_sim(_remove_parentheses(spo_title), _remove_parentheses(dz_json.get('title', ''))),
|
||||||
|
_sim(_remove_parentheses(spo_title), _remove_parentheses(dz_json.get('title_short', '')))
|
||||||
|
)
|
||||||
|
if max(title_match_1, title_match_2) < 0.90:
|
||||||
continue
|
continue
|
||||||
c_id = cand.get('id')
|
c_id = cand.get('id')
|
||||||
if not c_id:
|
if not c_id:
|
||||||
|
|||||||
Reference in New Issue
Block a user