mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-12-10 09:07:29 +00:00
Compare commits
1 Commits
311af069d8
...
cfe07d0b66
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cfe07d0b66 |
@ -94,7 +94,6 @@ 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,9 +4,7 @@ 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(
|
||||||
@ -69,40 +67,27 @@ extension TrackExtensions on Track {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension IterableTrackSimpleExtensions on Iterable<TrackSimple> {
|
extension TrackSimpleExtensions on TrackSimple {
|
||||||
Future<List<Track>> asTracks(AlbumSimple album, ref) async {
|
Track asTrack(AlbumSimple album) {
|
||||||
try {
|
Track track = Track();
|
||||||
final spotify = ref.read(spotifyProvider);
|
track.name = name;
|
||||||
final tracks = await spotify.invoke(
|
track.album = album;
|
||||||
(api) => api.tracks.list(map((trackSimple) => trackSimple.id!).toList()));
|
track.artists = artists;
|
||||||
return tracks.toList();
|
track.availableMarkets = availableMarkets;
|
||||||
} catch (e, stack) {
|
track.discNumber = discNumber;
|
||||||
// Ignore errors and create the track locally
|
track.durationMs = durationMs;
|
||||||
AppLogger.reportError(e, stack);
|
track.explicit = explicit;
|
||||||
|
track.externalUrls = externalUrls;
|
||||||
List<Track> tracks = [];
|
track.href = href;
|
||||||
for (final trackSimple in this) {
|
track.id = id;
|
||||||
Track track = Track();
|
track.isPlayable = isPlayable;
|
||||||
track.album = album;
|
track.linkedFrom = linkedFrom;
|
||||||
track.name = trackSimple.name;
|
track.name = name;
|
||||||
track.artists = trackSimple.artists;
|
track.previewUrl = previewUrl;
|
||||||
track.availableMarkets = trackSimple.availableMarkets;
|
track.trackNumber = trackNumber;
|
||||||
track.discNumber = trackSimple.discNumber;
|
track.type = type;
|
||||||
track.durationMs = trackSimple.durationMs;
|
track.uri = uri;
|
||||||
track.explicit = trackSimple.explicit;
|
return track;
|
||||||
track.externalUrls = trackSimple.externalUrls;
|
|
||||||
track.href = trackSimple.href;
|
|
||||||
track.id = trackSimple.id;
|
|
||||||
track.isPlayable = trackSimple.isPlayable;
|
|
||||||
track.linkedFrom = trackSimple.linkedFrom;
|
|
||||||
track.previewUrl = trackSimple.previewUrl;
|
|
||||||
track.trackNumber = trackSimple.trackNumber;
|
|
||||||
track.type = trackSimple.type;
|
|
||||||
track.uri = trackSimple.uri;
|
|
||||||
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!.asTracks(album, ref);
|
return album.tracks!.map((track) => track.asTrack(album)).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();
|
||||||
|
|||||||
@ -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 = await tracks.items!.asTracks(arg, ref);
|
final items = tracks.items?.map((e) => e.asTrack(arg)).toList() ?? [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
items: items,
|
items: items,
|
||||||
|
|||||||
@ -236,67 +236,29 @@ 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 (isrcResults.isEmpty && ytLink?.url != null) {
|
if (ytLink?.url != null
|
||||||
|
// allows to fetch siblings more results for already sourced track
|
||||||
|
&&
|
||||||
|
track is! SourcedTrack) {
|
||||||
try {
|
try {
|
||||||
videoResults.add(
|
return [
|
||||||
|
await toSiblingType(
|
||||||
|
0,
|
||||||
YoutubeVideoInfo.fromVideo(
|
YoutubeVideoInfo.fromVideo(
|
||||||
await ref.read(youtubeEngineProvider)
|
await ref.read(youtubeEngineProvider).getVideo(
|
||||||
.getVideo(Uri.parse(ytLink!.url!).queryParameters["v"]!)
|
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);
|
||||||
@ -309,28 +271,20 @@ class YoutubeSourcedTrack extends SourcedTrack {
|
|||||||
await ref.read(youtubeEngineProvider).searchVideos(query);
|
await ref.read(youtubeEngineProvider).searchVideos(query);
|
||||||
|
|
||||||
if (ServiceUtils.onlyContainsEnglish(query)) {
|
if (ServiceUtils.onlyContainsEnglish(query)) {
|
||||||
videoResults.addAll(
|
return await Future.wait(searchResults
|
||||||
searchResults.map(YoutubeVideoInfo.fromVideo).toList()
|
.map(YoutubeVideoInfo.fromVideo)
|
||||||
);
|
.mapIndexed((index, info) => toSiblingType(index, info, ref)));
|
||||||
} else {
|
|
||||||
videoResults.addAll(rankResults(
|
|
||||||
searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
|
|
||||||
track,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final seenIds = <String>{};
|
final rankedSiblings = rankResults(
|
||||||
int index = 0;
|
searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
|
||||||
|
track,
|
||||||
|
);
|
||||||
|
|
||||||
return await Future.wait(
|
return await Future.wait(
|
||||||
videoResults.map((videoResult) async {
|
rankedSiblings
|
||||||
// Deduplicate results
|
.mapIndexed((index, info) => toSiblingType(index, info, ref)),
|
||||||
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