Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix(lyrics): survive a null duration from any lyrics source (#200)

My-Name-Is-Jeff3d613a8c493b
diff --git a/tweak/Sources/Shared/LyricsSources/BiniLyrics.m b/tweak/Sources/Shared/LyricsSources/BiniLyrics.m
index d456c5cc..d0a6ac5f 100644
--- a/tweak/Sources/Shared/LyricsSources/BiniLyrics.m
+++ b/tweak/Sources/Shared/LyricsSources/BiniLyrics.m
@@ -21,7 +21,8 @@ static NSDictionary *bestOf(id results, NSInteger seconds) {
NSInteger bestOff = NSIntegerMax;
for (NSDictionary *found in [results isKindOfClass:NSArray.class] ? results : @[]) {
if (![found isKindOfClass:NSDictionary.class] || ![found[@"lyricsUrl"] isKindOfClass:NSString.class]) continue;
- NSInteger length = [found[@"duration"] integerValue];
+ id duration = found[@"duration"];
+ NSInteger length = [duration respondsToSelector:@selector(integerValue)] ? [duration integerValue] : 0;
NSInteger off = seconds > 0 && length > 0 ? labs(length - seconds) : 0;
if (off > kLengthSlack) continue;
BOOL wordTimed = [found[@"timing_type"] isEqual:@"word"];
diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.m
index 1fbf1409..74124260 100644
--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m
+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m
@@ -121,6 +121,13 @@ static SGLyricsResult *resultFrom(NSDictionary *record, NSArray<SGKaraokeLine *>
return SGLyricsPlainResult([plain componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]);
}
+// A record's length in whole seconds, 0 when it has none. The JSON can carry `"duration": null`, and NSNull
+// answers no doubleValue.
+static NSInteger lengthOf(NSDictionary *record) {
+ id duration = record[@"duration"];
+ return [duration respondsToSelector:@selector(doubleValue)] ? (NSInteger)[duration doubleValue] : 0;
+}
+
// How good a recording is for the track: words timed beat lines timed, which beat untimed, then the
// closest length. With no length to go on, the fullest sheet wins — the search will happily rank a
// seventeen second stub of a song above the song, and a stub is the one thing a length would have
@@ -133,8 +140,10 @@ static BOOL betterRecord(NSDictionary *record, NSDictionary *best, NSInteger sec
BOOL wasSynced = [best[@"syncedLyrics"] isKindOfClass:NSString.class] && [best[@"syncedLyrics"] length];
if (synced != wasSynced) return synced;
if (seconds > 0) {
- NSInteger off = labs((NSInteger)[record[@"duration"] doubleValue] - seconds);
- NSInteger wasOff = labs((NSInteger)[best[@"duration"] doubleValue] - seconds);
+ // A record without a length is the farthest off: nothing says it is not a stub.
+ NSInteger length = lengthOf(record), wasLength = lengthOf(best);
+ NSInteger off = length > 0 ? labs(length - seconds) : NSIntegerMax;
+ NSInteger wasOff = wasLength > 0 ? labs(wasLength - seconds) : NSIntegerMax;
return off < wasOff;
}
NSString *text = record[@"syncedLyrics"] ?: record[@"plainLyrics"] ?: @"";
@@ -149,7 +158,7 @@ static NSArray<NSDictionary *> *matching(id found, SGLyricsQuery *query) {
NSMutableArray<NSDictionary *> *records = [NSMutableArray array];
for (NSDictionary *record in [found isKindOfClass:NSArray.class] ? found : @[]) {
if (![record isKindOfClass:NSDictionary.class]) continue;
- NSInteger length = (NSInteger)[record[@"duration"] doubleValue];
+ NSInteger length = lengthOf(record);
if (query.seconds > 0 && length > 0 && labs(length - query.seconds) > kLengthSlack) continue;
NSString *artists = [record[@"artistName"] isKindOfClass:NSString.class] ? record[@"artistName"] : @"";
if (!SGLyricsSongRank(query.title, query.artist, record[@"trackName"], @[artists])) continue;
diff --git a/tweak/Sources/Shared/LyricsSources/NetEase.m b/tweak/Sources/Shared/LyricsSources/NetEase.m
index d58f3930..610382d3 100644
--- a/tweak/Sources/Shared/LyricsSources/NetEase.m
+++ b/tweak/Sources/Shared/LyricsSources/NetEase.m
@@ -11,6 +11,12 @@ static const NSTimeInterval kTimeout = 3;
static const NSInteger kLengthSlack = 3;
static const NSUInteger kTriedSongs = 3;
+// A song's length in whole seconds, 0 when its duration is missing or null.
+static NSInteger lengthOf(NSDictionary *song) {
+ id duration = song[@"duration"];
+ return [duration respondsToSelector:@selector(integerValue)] ? [duration integerValue] / 1000 : 0;
+}
+
static void get(NSString *path, NSDictionary<NSString *, NSString *> *query, void (^done)(NSDictionary *root)) {
NSURLComponents *url = [NSURLComponents componentsWithString:[@"https://music.163.com/api/" stringByAppendingString:path]];
NSMutableArray<NSURLQueryItem *> *items = [NSMutableArray array];
@@ -107,7 +113,7 @@ SGLyricsAsk SGNetEaseAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *
id songs = [root[@"result"] isKindOfClass:NSDictionary.class] ? root[@"result"][@"songs"] : nil;
NSMutableArray<NSDictionary *> *fitting = [NSMutableArray array];
for (NSDictionary *song in [songs isKindOfClass:NSArray.class] ? songs : @[]) {
- if (![song isKindOfClass:NSDictionary.class] || labs([song[@"duration"] integerValue] / 1000 - seconds) > kLengthSlack) continue;
+ if (![song isKindOfClass:NSDictionary.class] || labs(lengthOf(song) - seconds) > kLengthSlack) continue;
id artists = song[@"artists"];
for (NSDictionary *credited in [artists isKindOfClass:NSArray.class] ? artists : @[]) {
NSString *name = [credited isKindOfClass:NSDictionary.class] && [credited[@"name"] isKindOfClass:NSString.class] ? [credited[@"name"] lowercaseString] : nil;
@@ -118,7 +124,7 @@ SGLyricsAsk SGNetEaseAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *
}
}
[fitting sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
- return [@(labs([a[@"duration"] integerValue] / 1000 - seconds)) compare:@(labs([b[@"duration"] integerValue] / 1000 - seconds))];
+ return [@(labs(lengthOf(a) - seconds)) compare:@(labs(lengthOf(b) - seconds))];
}];
NSArray *ids = [[fitting valueForKey:@"id"] subarrayWithRange:NSMakeRange(0, MIN(fitting.count, kTriedSongs))];
if (!ids.count) SGLog(@"netease: no recording of %@ by %@ within %lds of %lds", title, lead, (long)kLengthSlack, (long)seconds);
diff --git a/tweak/Sources/Shared/LyricsSources/Unison.m b/tweak/Sources/Shared/LyricsSources/Unison.m
index bf857bd1..ab8929a3 100644
--- a/tweak/Sources/Shared/LyricsSources/Unison.m
+++ b/tweak/Sources/Shared/LyricsSources/Unison.m
@@ -35,7 +35,8 @@ SGLyricsAsk SGUnisonAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
SGLog(@"unison: %@ by %@ is %@, not ttml", query.title, query.artist, data[@"format"]);
return nil;
}
- NSInteger length = [data[@"duration"] integerValue];
+ id duration = data[@"duration"];
+ NSInteger length = [duration respondsToSelector:@selector(integerValue)] ? [duration integerValue] : 0;
if (query.seconds > 0 && length > 0 && labs(length - query.seconds) > kLengthSlack) {
SGLog(@"unison: %@ is %lds against the track's %lds", query.title, (long)length, (long)query.seconds);
return nil;