chore: fix login not working and source quality not good when high quality source not found

This commit is contained in:
Kingkor Roy Tirtho 2025-06-21 19:21:00 +06:00
parent 1538dc6d52
commit ee71dbf552
4 changed files with 51 additions and 35 deletions

View File

@ -16,12 +16,16 @@ class HistoryTable extends Table {
} }
extension HistoryItemParseExtension on HistoryTableData { extension HistoryItemParseExtension on HistoryTableData {
SpotubeSimplePlaylistObject? get playlist => type == HistoryEntryType.playlist SpotubeSimplePlaylistObject? get playlist =>
? SpotubeSimplePlaylistObject.fromJson(data) type == HistoryEntryType.playlist && !data.containsKey("external_urls")
: null; ? SpotubeSimplePlaylistObject.fromJson(data)
SpotubeSimpleAlbumObject? get album => type == HistoryEntryType.album : null;
? SpotubeSimpleAlbumObject.fromJson(data) SpotubeSimpleAlbumObject? get album =>
: null; type == HistoryEntryType.album && !data.containsKey("external_urls")
? SpotubeSimpleAlbumObject.fromJson(data)
: null;
SpotubeTrackObject? get track => SpotubeTrackObject? get track =>
type == HistoryEntryType.track ? SpotubeTrackObject.fromJson(data) : null; type == HistoryEntryType.track && !data.containsKey("external_urls")
? SpotubeTrackObject.fromJson(data)
: null;
} }

View File

@ -149,25 +149,48 @@ abstract class SourcedTrack extends BasicSourcedTrack {
return getUrlOfCodec(codec); return getUrlOfCodec(codec);
} }
/// Returns the URL of the track based on the codec and quality preferences.
/// If an exact match is not found, it will return the closest match based on
/// the user's audio quality preference.
///
/// If no sources match the codec, it will return the first or last source
/// based on the user's audio quality preference.
String getUrlOfCodec(SourceCodecs codec) { String getUrlOfCodec(SourceCodecs codec) {
final preferences = ref.read(userPreferencesProvider); final preferences = ref.read(userPreferencesProvider);
return sources final exactMatch = sources.firstWhereOrNull(
.firstWhereOrNull( (source) =>
(source) => source.codec == codec && source.quality == preferences.audioQuality,
source.codec == codec && );
source.quality == preferences.audioQuality,
) if (exactMatch != null) {
?.url ?? return exactMatch.url;
// fallback to the first available source of the same codec }
sources.firstWhereOrNull((source) => source.codec == codec)?.url ??
// fallback to the first available source of any codec final sameCodecSources = sources
sources .where((source) => source.codec == codec)
.firstWhereOrNull( .toList()
(source) => source.quality == preferences.audioQuality) .sorted((a, b) {
?.url ?? final aDiff = (a.quality.index - preferences.audioQuality.index).abs();
// fallback to the first available source final bDiff = (b.quality.index - preferences.audioQuality.index).abs();
sources.first.url; return aDiff != bDiff ? aDiff - bDiff : a.quality.index - b.quality.index;
}).toList();
if (sameCodecSources.isNotEmpty) {
return preferences.audioQuality != SourceQualities.low
? sameCodecSources.first.url
: sameCodecSources.last.url;
}
final fallbackSource = sources.sorted((a, b) {
final aDiff = (a.quality.index - preferences.audioQuality.index).abs();
final bDiff = (b.quality.index - preferences.audioQuality.index).abs();
return aDiff != bDiff ? aDiff - bDiff : a.quality.index - b.quality.index;
});
return preferences.audioQuality != SourceQualities.low
? fallbackSource.first.url
: fallbackSource.last.url;
} }
SourceCodecs get codec { SourceCodecs get codec {

View File

@ -95,17 +95,6 @@ class YoutubeSourcedTrack extends SourcedTrack {
} }
static List<TrackSource> toTrackSources(StreamManifest manifest) { static List<TrackSource> toTrackSources(StreamManifest manifest) {
var m4a = manifest.audioOnly
.where((audio) => audio.codec.mimeType == "audio/mp4")
.sortByBitrate();
var weba = manifest.audioOnly
.where((audio) => audio.codec.mimeType == "audio/webm")
.sortByBitrate();
m4a = m4a.isEmpty ? weba.toList() : m4a;
weba = weba.isEmpty ? m4a.toList() : weba;
return manifest.audioOnly.map((streamInfo) { return manifest.audioOnly.map((streamInfo) {
return TrackSource( return TrackSource(
url: streamInfo.url.toString(), url: streamInfo.url.toString(),

View File

@ -1209,7 +1209,7 @@ packages:
description: description:
path: "." path: "."
ref: main ref: main
resolved-ref: "1aa924281d2dbe09aab27d8c2de1cffc853b0d16" resolved-ref: eeb574c3fac0da07ba93c9d972b7eb38960538d2
url: "https://github.com/KRTirtho/hetu_spotube_plugin.git" url: "https://github.com/KRTirtho/hetu_spotube_plugin.git"
source: git source: git
version: "0.0.1" version: "0.0.1"