mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
feat: add ISRC track search for YouTube (#2594)
* Add ISRC track search for YouTube * Do not probe Song.Link when ISRC results are valid, fix rate limit
This commit is contained in:
parent
0ec9f3535b
commit
2c4cc94985
@ -94,6 +94,7 @@ abstract class FakeData {
|
|||||||
..trackNumber = 1
|
..trackNumber = 1
|
||||||
..type = "type"
|
..type = "type"
|
||||||
..uri = "uri"
|
..uri = "uri"
|
||||||
|
..externalIds = externalIds
|
||||||
..isPlayable = true
|
..isPlayable = true
|
||||||
..explicit = false
|
..explicit = false
|
||||||
..linkedFrom = trackLink;
|
..linkedFrom = trackLink;
|
||||||
|
@ -4,7 +4,9 @@ import 'dart:typed_data';
|
|||||||
import 'package:metadata_god/metadata_god.dart';
|
import 'package:metadata_god/metadata_god.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:spotify/spotify.dart';
|
import 'package:spotify/spotify.dart';
|
||||||
|
import 'package:spotube/provider/spotify/spotify.dart';
|
||||||
import 'package:spotube/services/audio_player/audio_player.dart';
|
import 'package:spotube/services/audio_player/audio_player.dart';
|
||||||
|
import 'package:spotube/services/logger/logger.dart';
|
||||||
|
|
||||||
extension TrackExtensions on Track {
|
extension TrackExtensions on Track {
|
||||||
Track fromFile(
|
Track fromFile(
|
||||||
@ -67,27 +69,40 @@ extension TrackExtensions on Track {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension TrackSimpleExtensions on TrackSimple {
|
extension IterableTrackSimpleExtensions on Iterable<TrackSimple> {
|
||||||
Track asTrack(AlbumSimple album) {
|
Future<List<Track>> asTracks(AlbumSimple album, ref) async {
|
||||||
|
try {
|
||||||
|
final spotify = ref.read(spotifyProvider);
|
||||||
|
final tracks = await spotify.invoke(
|
||||||
|
(api) => api.tracks.list(map((trackSimple) => trackSimple.id!).toList()));
|
||||||
|
return tracks.toList();
|
||||||
|
} catch (e, stack) {
|
||||||
|
// Ignore errors and create the track locally
|
||||||
|
AppLogger.reportError(e, stack);
|
||||||
|
|
||||||
|
List<Track> tracks = [];
|
||||||
|
for (final trackSimple in this) {
|
||||||
Track track = Track();
|
Track track = Track();
|
||||||
track.name = name;
|
|
||||||
track.album = album;
|
track.album = album;
|
||||||
track.artists = artists;
|
track.name = trackSimple.name;
|
||||||
track.availableMarkets = availableMarkets;
|
track.artists = trackSimple.artists;
|
||||||
track.discNumber = discNumber;
|
track.availableMarkets = trackSimple.availableMarkets;
|
||||||
track.durationMs = durationMs;
|
track.discNumber = trackSimple.discNumber;
|
||||||
track.explicit = explicit;
|
track.durationMs = trackSimple.durationMs;
|
||||||
track.externalUrls = externalUrls;
|
track.explicit = trackSimple.explicit;
|
||||||
track.href = href;
|
track.externalUrls = trackSimple.externalUrls;
|
||||||
track.id = id;
|
track.href = trackSimple.href;
|
||||||
track.isPlayable = isPlayable;
|
track.id = trackSimple.id;
|
||||||
track.linkedFrom = linkedFrom;
|
track.isPlayable = trackSimple.isPlayable;
|
||||||
track.name = name;
|
track.linkedFrom = trackSimple.linkedFrom;
|
||||||
track.previewUrl = previewUrl;
|
track.previewUrl = trackSimple.previewUrl;
|
||||||
track.trackNumber = trackNumber;
|
track.trackNumber = trackSimple.trackNumber;
|
||||||
track.type = type;
|
track.type = trackSimple.type;
|
||||||
track.uri = uri;
|
track.uri = trackSimple.uri;
|
||||||
return track;
|
tracks.add(track);
|
||||||
|
}
|
||||||
|
return tracks;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ 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 album.tracks!.map((track) => track.asTrack(album)).toList();
|
return album.tracks!.asTracks(album, ref);
|
||||||
}
|
}
|
||||||
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();
|
||||||
|
@ -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 items = tracks.items?.map((e) => e.asTrack(arg)).toList() ?? [];
|
final items = await tracks.items!.asTracks(arg, ref);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
items: items,
|
items: items,
|
||||||
|
@ -236,29 +236,67 @@ class YoutubeSourcedTrack extends SourcedTrack {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<List<YoutubeVideoInfo>> fetchFromIsrc({
|
||||||
|
required Track track,
|
||||||
|
required Provider provider,
|
||||||
|
required Ref ref,
|
||||||
|
}) async {
|
||||||
|
final isrcResults = <YoutubeVideoInfo>[];
|
||||||
|
final isrc = track.externalIds?.isrc;
|
||||||
|
if (isrc != null && isrc.isNotEmpty) {
|
||||||
|
final searchedVideos = await ref
|
||||||
|
.read(provider)
|
||||||
|
.searchVideos(isrc.toString());
|
||||||
|
if (searchedVideos.isNotEmpty) {
|
||||||
|
isrcResults.addAll(searchedVideos
|
||||||
|
.map<YoutubeVideoInfo>(YoutubeVideoInfo.fromVideo)
|
||||||
|
.map((YoutubeVideoInfo videoInfo) {
|
||||||
|
final ytWords =
|
||||||
|
videoInfo.title
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
|
||||||
|
.split(RegExp(r'\s+'))
|
||||||
|
.where((item) => item.isNotEmpty);
|
||||||
|
final spWords =
|
||||||
|
track.name!
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(RegExp(r'\((.*)\)'), '')
|
||||||
|
.replaceAll(RegExp(r'[^a-zA-Z0-9\s]+'), '')
|
||||||
|
.split(RegExp(r'\s+'))
|
||||||
|
.where((item) => item.isNotEmpty);
|
||||||
|
// Word match to filter out unrelated results
|
||||||
|
final matchCount =
|
||||||
|
ytWords.where((word) => spWords.contains(word)).length;
|
||||||
|
if (matchCount > spWords.length ~/ 2) {
|
||||||
|
return videoInfo;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
).whereType<YoutubeVideoInfo>().toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isrcResults;
|
||||||
|
}
|
||||||
|
|
||||||
static Future<List<SiblingType>> fetchSiblings({
|
static Future<List<SiblingType>> fetchSiblings({
|
||||||
required Track track,
|
required Track track,
|
||||||
required Ref ref,
|
required Ref ref,
|
||||||
}) async {
|
}) async {
|
||||||
|
final videoResults = <YoutubeVideoInfo>[];
|
||||||
|
|
||||||
|
final isrcResults = await fetchFromIsrc(track: track, provider: youtubeEngineProvider, ref: ref);
|
||||||
|
videoResults.addAll(isrcResults);
|
||||||
|
|
||||||
final links = await SongLinkService.links(track.id!);
|
final links = await SongLinkService.links(track.id!);
|
||||||
final ytLink = links.firstWhereOrNull((link) => link.platform == "youtube");
|
final ytLink = links.firstWhereOrNull((link) => link.platform == "youtube");
|
||||||
|
|
||||||
if (ytLink?.url != null
|
if (isrcResults.isEmpty && ytLink?.url != null) {
|
||||||
// allows to fetch siblings more results for already sourced track
|
|
||||||
&&
|
|
||||||
track is! SourcedTrack) {
|
|
||||||
try {
|
try {
|
||||||
return [
|
videoResults.add(
|
||||||
await toSiblingType(
|
|
||||||
0,
|
|
||||||
YoutubeVideoInfo.fromVideo(
|
YoutubeVideoInfo.fromVideo(
|
||||||
await ref.read(youtubeEngineProvider).getVideo(
|
await ref.read(youtubeEngineProvider)
|
||||||
Uri.parse(ytLink!.url!).queryParameters["v"]!,
|
.getVideo(Uri.parse(ytLink!.url!).queryParameters["v"]!)
|
||||||
),
|
));
|
||||||
),
|
|
||||||
ref,
|
|
||||||
)
|
|
||||||
];
|
|
||||||
} 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);
|
||||||
@ -271,20 +309,28 @@ class YoutubeSourcedTrack extends SourcedTrack {
|
|||||||
await ref.read(youtubeEngineProvider).searchVideos(query);
|
await ref.read(youtubeEngineProvider).searchVideos(query);
|
||||||
|
|
||||||
if (ServiceUtils.onlyContainsEnglish(query)) {
|
if (ServiceUtils.onlyContainsEnglish(query)) {
|
||||||
return await Future.wait(searchResults
|
videoResults.addAll(
|
||||||
.map(YoutubeVideoInfo.fromVideo)
|
searchResults.map(YoutubeVideoInfo.fromVideo).toList()
|
||||||
.mapIndexed((index, info) => toSiblingType(index, info, ref)));
|
);
|
||||||
}
|
} else {
|
||||||
|
videoResults.addAll(rankResults(
|
||||||
final rankedSiblings = rankResults(
|
|
||||||
searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
|
searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
|
||||||
track,
|
track,
|
||||||
);
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
final seenIds = <String>{};
|
||||||
|
int index = 0;
|
||||||
return await Future.wait(
|
return await Future.wait(
|
||||||
rankedSiblings
|
videoResults.map((videoResult) async {
|
||||||
.mapIndexed((index, info) => toSiblingType(index, info, ref)),
|
// Deduplicate results
|
||||||
);
|
if (!seenIds.contains(videoResult.id)) {
|
||||||
|
seenIds.add(videoResult.id);
|
||||||
|
return await toSiblingType(index++, videoResult, ref);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
).then((s) => s.whereType<SiblingType>().toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Loading…
Reference in New Issue
Block a user