Compare commits

..

2 Commits

Author SHA1 Message Date
Seungmin Kim
3bf85e07cd
Merge 4441d6cb59 into 0ec9f3535b 2025-03-26 04:43:08 +00:00
Seungmin Kim
4441d6cb59 Add ISRC track search for YouTube 2025-03-25 21:42:58 -07:00
4 changed files with 36 additions and 34 deletions

View File

@ -75,7 +75,7 @@ extension TrackSimpleExtensions on TrackSimple {
final spotify = ref.read(spotifyProvider); final spotify = ref.read(spotifyProvider);
return await spotify.invoke((api) => api.tracks.get(id!)); return await spotify.invoke((api) => api.tracks.get(id!));
} catch (e, stack) { } catch (e, stack) {
// Ignore errors and create the track locally // Ignore this error and create the Track locally
AppLogger.reportError(e, stack); AppLogger.reportError(e, stack);
Track track = Track(); Track track = Track();

View File

@ -54,8 +54,8 @@ class AlbumCard extends HookConsumerWidget {
Future<List<Track>> fetchAllTrack() async { Future<List<Track>> fetchAllTrack() async {
if (album.tracks != null && album.tracks!.isNotEmpty) { if (album.tracks != null && album.tracks!.isNotEmpty) {
return await Future.wait( return Future.wait(
album.tracks!.map((track) => track.asTrack(album, ref))); album.tracks!.map((track) => track.asTrack(album, ref)).toList());
} }
await ref.read(albumTracksProvider(album).future); await ref.read(albumTracksProvider(album).future);
return ref.read(albumTracksProvider(album).notifier).fetchAll(); return ref.read(albumTracksProvider(album).notifier).fetchAll();

View File

@ -33,7 +33,7 @@ class AlbumTracksNotifier extends AutoDisposeFamilyPaginatedAsyncNotifier<Track,
final tracks = await spotify.invoke( final tracks = await spotify.invoke(
(api) => api.albums.tracks(arg.id!).getPage(limit, offset), (api) => api.albums.tracks(arg.id!).getPage(limit, offset),
); );
final List<Track> items = await Future.wait(tracks.items?.map((e) => e.asTrack(arg, ref)) ?? []); final List<Track> items = await Future.wait(tracks.items?.map((e) => e.asTrack(arg, ref)).toList() ?? []);
return ( return (
items: items, items: items,

View File

@ -248,31 +248,30 @@ class YoutubeSourcedTrack extends SourcedTrack {
.read(provider) .read(provider)
.searchVideos(isrc.toString()); .searchVideos(isrc.toString());
if (searchedVideos.isNotEmpty) { if (searchedVideos.isNotEmpty) {
isrcResults.addAll(searchedVideos isrcResults.addAll(await Future.wait(
.map<YoutubeVideoInfo>(YoutubeVideoInfo.fromVideo) searchedVideos
.map((YoutubeVideoInfo videoInfo) { .map<YoutubeVideoInfo>(YoutubeVideoInfo.fromVideo)
final ytWords = .map((YoutubeVideoInfo videoInfo) async {
videoInfo.title final titleWords =
.toLowerCase() videoInfo.title
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '') .toLowerCase()
.split(RegExp(r'\s+')) .replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
.where((item) => item.isNotEmpty); .split(RegExp(r'\s+'))
final spWords = .where((item) => item.isNotEmpty)
track.name! .toList();
.toLowerCase() final nameLower =
.replaceAll(RegExp(r'\((.*)\)'), '') track.name!
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '') .toLowerCase()
.split(RegExp(r'\s+')) .replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
.where((item) => item.isNotEmpty); .split(RegExp(r'\s+'))
// Word match to filter out unrelated results .where((item) => item.isNotEmpty)
final matchCount = .toList();
ytWords.where((word) => spWords.contains(word)).length; if (titleWords.any((word) => nameLower.contains(word))) {
if (matchCount > spWords.length ~/ 2) { return videoInfo;
return videoInfo;
}
return null;
} }
).whereType<YoutubeVideoInfo>().toList()); return null;
}
)).then((s) => s.whereType<YoutubeVideoInfo>().toList()));
} }
} }
return isrcResults; return isrcResults;
@ -293,9 +292,11 @@ class YoutubeSourcedTrack extends SourcedTrack {
try { try {
videoResults.add( videoResults.add(
YoutubeVideoInfo.fromVideo( YoutubeVideoInfo.fromVideo(
await ref.read(youtubeEngineProvider) await ref
.getVideo(Uri.parse(ytLink!.url!).queryParameters["v"]!) .read(youtubeEngineProvider)
)); .getVideo(Uri.parse(ytLink!.url!).queryParameters["v"]!),
)
);
} on VideoUnplayableException catch (e, stack) { } on VideoUnplayableException catch (e, stack) {
// Ignore this error and continue with the search // Ignore this error and continue with the search
AppLogger.reportError(e, stack); AppLogger.reportError(e, stack);
@ -304,12 +305,13 @@ class YoutubeSourcedTrack extends SourcedTrack {
final query = SourcedTrack.getSearchTerm(track); final query = SourcedTrack.getSearchTerm(track);
final searchResults = final searchResults = await ref
await ref.read(youtubeEngineProvider).searchVideos(query); .read(youtubeEngineProvider)
.searchVideos(query);
if (ServiceUtils.onlyContainsEnglish(query)) { if (ServiceUtils.onlyContainsEnglish(query)) {
videoResults.addAll( videoResults.addAll(
searchResults.map(YoutubeVideoInfo.fromVideo).toList() searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
); );
} else { } else {
videoResults.addAll(rankResults( videoResults.addAll(rankResults(