Compare commits

..

2 Commits

Author SHA1 Message Date
Seungmin Kim
2341e5ca06
Merge 50d729f8a0 into 0ec9f3535b 2025-03-24 15:48:28 +00:00
Seungmin Kim
50d729f8a0 Add ISRC track search for YouTube 2025-03-24 08:48:18 -07:00

View File

@ -242,28 +242,30 @@ class YoutubeSourcedTrack extends SourcedTrack {
}) async { }) async {
List<SiblingType> siblings = []; List<SiblingType> siblings = [];
final isrc = track.externalIds?.isrc.toString(); final isrc = track.externalIds?.isrc;
if (isrc != null && isrc.isNotEmpty) { if (isrc != null && isrc.isNotEmpty) {
final isrcResults = final isrcResults =
await ref.read(youtubeEngineProvider).searchVideos(isrc); await ref.read(youtubeEngineProvider).searchVideos(isrc.toString());
if (isrcResults.isNotEmpty) { if (isrcResults.isNotEmpty) {
final rankedResults = rankResults( final rankedResults = rankResults(
isrcResults.map(YoutubeVideoInfo.fromVideo).toList(), track); isrcResults.map(YoutubeVideoInfo.fromVideo).toList(), track);
final matchingResults = <YoutubeVideoInfo>[]; final matchingResults = <YoutubeVideoInfo>[];
for (final video in rankedResults) { for (final video in rankedResults) {
final titleWords = video.title final titleWords = video.title
.replaceAll(RegExp(r'\((.*)\)'), '')
.toLowerCase() .toLowerCase()
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
.split(RegExp(r'\s+')) .split(RegExp(r'\s+'))
.where((item) => item.isNotEmpty)
.toList(); .toList();
final nameLower = track.name! final nameLower = track.name!
.replaceAll(RegExp(r'\((.*)\)'), '')
.toLowerCase() .toLowerCase()
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
.split(RegExp(r'\s+')) .split(RegExp(r'\s+'))
.where((item) => item.isNotEmpty)
.toList(); .toList();
final matchCount = final matchCount =
titleWords.where((word) => nameLower.contains(word)).length; titleWords.where((word) => nameLower.contains(word)).length;
if (matchCount >= nameLower.length) { if (matchCount > nameLower.length / 2) {
matchingResults.add(video); matchingResults.add(video);
} }
} }
@ -322,18 +324,16 @@ class YoutubeSourcedTrack extends SourcedTrack {
.mapIndexed((index, info) => toSiblingType(index, info, ref)), .mapIndexed((index, info) => toSiblingType(index, info, ref)),
)); ));
} }
return await (() async {
// Deduplicate siblings by info.id, keeping the first occurrence final seenIds = <String>{};
final seenIds = <String>{}; // Deduplicate siblings by info.id, keeping the first occurrence
final uniqueSiblings = <SiblingType>[]; return await Future.wait(siblings.map((sibling) async {
for (final sibling in siblings) { if (!seenIds.contains(sibling.info.id)) {
if (!seenIds.contains(sibling.info.id)) { seenIds.add(sibling.info.id);
seenIds.add(sibling.info.id); return sibling;
uniqueSiblings.add(sibling);
}
} }
return uniqueSiblings; return null;
})(); })).then((s) => s.whereType<SiblingType>().toList());
} }
@override @override