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

refactor: Genius's search lives with the song match

My-Name-Is-Jeff03fdee960602
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 4566d6f4..809bc602 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -125,7 +125,7 @@ Shared:
source's row; checked on the Mac against harness/spicy-lyrics/. QQMusic.m (line timed LRC) and KuGou.m (word timed KRC, unpacked with
zlib and read by NetEase.m's parser) for Chinese and other Asian songs, matched by title, singer
and length; checked on the Mac against harness/lyrics-sources/. Genius.m, last: untimed words for a track no
- other source has, the song found by the line meanings' search (SGGeniusSongIn in LyricsMeanings/Meanings.m, the match itself in SongMatch.m, which LRCLIB's search shares)
+ other source has, the song found by the search the line meanings share (SGGeniusSongIn in SongMatch.m, whose match LRCLIB's search shares too)
and the words read from its embed script, which is scraping (no API gives the text); checked on
the Mac against harness/genius-lyrics/. color-lyrics is answered with whichever won
(LyricsHook.x): Spotify's own 200 gets our lines swapped in; a track Spotify's metadata says has none has
diff --git a/harness/genius-lyrics/README.md b/harness/genius-lyrics/README.md
index 10003906..76ea9ada 100644
--- a/harness/genius-lyrics/README.md
+++ b/harness/genius-lyrics/README.md
@@ -2,7 +2,7 @@
`./build.sh` checks, on the Mac as a Mac Catalyst binary, with no simulator and no network:
-- the song Genius's search reply gives a track (`SGGeniusSongIn` in `Shared/LyricsMeanings/Meanings.m`),
+- the song Genius's search reply gives a track (`SGGeniusSongIn` in `Shared/LyricsSources/SongMatch.m`),
on replies carrying the titles and artists genius.com really answered for these tracks: a song of the
same name by someone else is passed over (Yesterday by a HearonTrackz featuring Queen Key is not
Queen's), a cover gets the singer's own page and nothing when Genius has none, a remix gets the remix
diff --git a/harness/genius-lyrics/check.m b/harness/genius-lyrics/check.m
index 833ce7cc..442acf48 100644
--- a/harness/genius-lyrics/check.m
+++ b/harness/genius-lyrics/check.m
@@ -1,12 +1,11 @@
// Checks the Genius lyrics source without the phone: the search's pick of a song for a track
-// (SGGeniusSongIn, Meanings.m) on replies shaped like Genius's, with the titles and artists its search
+// (SGGeniusSongIn, SongMatch.m) on replies shaped like Genius's, with the titles and artists its search
// really gave for these tracks, and the lines read out of a made-up embed script (Genius.m). With
// `live`, the same tracks are searched on genius.com, the embed of each match is read, and the whole ask
// runs once; only line counts are printed, since the words are real lyrics. Exits non-zero on the first
// wrong answer.
#import <UIKit/UIKit.h>
#import "Shared/LyricsSources/LyricsSources.h"
-#import "Shared/LyricsMeanings/Meanings.h"
#import "Settings/SGModPage.h"
#define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "FAILED line %d: %s\n", __LINE__, #cond); exit(1); } } while (0)
diff --git a/harness/lyrics-translation/check.m b/harness/lyrics-translation/check.m
index 4b6d5f39..4a58d282 100644
--- a/harness/lyrics-translation/check.m
+++ b/harness/lyrics-translation/check.m
@@ -11,6 +11,7 @@
@implementation SGLyricsResult
@end
void SGLyricsNoteReply(NSURLResponse *response, NSError *error) {}
+SGLyricsResult *SGLyricsPlainResult(NSArray<NSString *> *rows) { return nil; }
NSString *SGLyricsTranslationLanguage(void) { return nil; }
SGModRow *SGStatActionRow(NSString *t, NSString *s, NSString *(^v)(void), void (^a)(void)) { return nil; }
SGModRow *SGOptionRow(NSString *t, NSString *s, NSString *k) { return nil; }
diff --git a/tweak/Sources/Shared/LyricsMeanings/Meanings.h b/tweak/Sources/Shared/LyricsMeanings/Meanings.h
index acff8129..e67099f7 100644
--- a/tweak/Sources/Shared/LyricsMeanings/Meanings.h
+++ b/tweak/Sources/Shared/LyricsMeanings/Meanings.h
@@ -36,12 +36,3 @@ SGModRow *SGLyricsMeaningsRow(void);
// per track; the matching is done again for each set of lines.
void SGLyricsMeaningsFor(NSString *trackID, NSArray<SGKaraokeLine *> *lines,
void (^done)(NSDictionary<NSNumber *, NSArray<SGLyricsMeaning *> *> *byLine));
-
-// Genius's search, which the lyrics source asks too (Shared/LyricsSources/Genius.m): its address, what
-// it is asked for a track, and the song in its reply that is the track, as SGLyricsSongRank
-// (Shared/LyricsSources/SongMatch.m) tells: of several, the same version wins, then the plain song.
-// nil when none is, or the track is an instrumental or karaoke version, since Genius files the words
-// under the song.
-#define SGGeniusSearchURL @"https://genius.com/api/search/song"
-NSDictionary<NSString *, NSString *> *SGGeniusSearchQuery(NSString *title, NSString *artist);
-NSNumber *SGGeniusSongIn(NSDictionary *response, NSString *title, NSString *artist);
diff --git a/tweak/Sources/Shared/LyricsMeanings/Meanings.m b/tweak/Sources/Shared/LyricsMeanings/Meanings.m
index 620f8e93..a7c5d6cf 100644
--- a/tweak/Sources/Shared/LyricsMeanings/Meanings.m
+++ b/tweak/Sources/Shared/LyricsMeanings/Meanings.m
@@ -90,41 +90,6 @@ static NSString *stringIn(id value) {
return [value isKindOfClass:NSString.class] ? value : nil;
}
-NSDictionary<NSString *, NSString *> *SGGeniusSearchQuery(NSString *title, NSString *artist) {
- return @{@"q": [NSString stringWithFormat:@"%@ %@", SGLyricsBareTitle(title), artist], @"per_page": @"10"};
-}
-
-NSNumber *SGGeniusSongIn(NSDictionary *response, NSString *title, NSString *artist) {
- if (![response isKindOfClass:NSDictionary.class] || !title.length || !artist.length) return nil;
- // Genius files a song's words under the song; a track with none sung would get them anyway.
- NSSet<NSString *> *version = SGLyricsTitleVersion(title);
- if ([version containsObject:@"instrumental"] || [version containsObject:@"karaoke"]) return nil;
- // Best first: the same version, then the plain song, then any other version of it.
- NSNumber *best = nil;
- NSInteger bestRank = 0;
- id sections = response[@"sections"];
- for (NSDictionary *section in [sections isKindOfClass:NSArray.class] ? sections : @[]) {
- id hits = [section isKindOfClass:NSDictionary.class] ? section[@"hits"] : nil;
- for (NSDictionary *hit in [hits isKindOfClass:NSArray.class] ? hits : @[]) {
- NSDictionary *song = [hit isKindOfClass:NSDictionary.class] ? hit[@"result"] : nil;
- if (![song isKindOfClass:NSDictionary.class] || ![song[@"id"] isKindOfClass:NSNumber.class]) continue;
- if ([song[@"instrumental"] isEqual:@YES]) continue;
- NSMutableArray<NSString *> *names = [NSMutableArray array];
- id primaries = song[@"primary_artists"];
- for (NSDictionary *primary in [primaries isKindOfClass:NSArray.class] ? primaries : @[]) {
- NSString *name = [primary isKindOfClass:NSDictionary.class] ? stringIn(primary[@"name"]) : nil;
- if (name) [names addObject:name];
- }
- NSInteger rank = SGLyricsSongRank(title, artist, stringIn(song[@"title"]), names);
- if (rank > bestRank) {
- best = song[@"id"];
- bestRank = rank;
- }
- }
- }
- return best;
-}
-
// By the annotation, not the referent: a referent the artist spoke to is "verified" as a whole, the
// editors' annotations under it too.
static SGLyricsMeaningAuthor authorOf(NSDictionary *annotation) {
diff --git a/tweak/Sources/Shared/LyricsSources/Genius.m b/tweak/Sources/Shared/LyricsSources/Genius.m
index ef4eb62e..06e16051 100644
--- a/tweak/Sources/Shared/LyricsSources/Genius.m
+++ b/tweak/Sources/Shared/LyricsSources/Genius.m
@@ -1,6 +1,6 @@
// Genius, under every other source: the words as its users transcribed them, with no timing at all, for
-// a track no source has lyrics for. The song is found by the same search the line meanings use
-// (Shared/LyricsMeanings/Meanings.m).
+// a track no source has lyrics for. The song is found by the search the line meanings share
+// (SongMatch.m).
//
// Genius's API gives a song's details and not its words; /api/songs/:id/lyrics wants a signed-in user.
// The words are read from the embed it offers other sites (songs/:id/embed.js, about 15 KB against the
@@ -10,18 +10,20 @@
// own gets through.
#import "Core/SGCore.h"
#import "LyricsSources.h"
-#import "Shared/LyricsMeanings/Meanings.h"
// The string the script hands JSON.parse, its JavaScript escapes undone: a JSON string.
static NSString *scriptString(NSString *js) {
NSRange open = [js rangeOfString:@"JSON.parse('"];
if (open.location == NSNotFound) return nil;
NSMutableString *json = [NSMutableString string];
- for (NSUInteger i = NSMaxRange(open); i < js.length; i++) {
+ NSUInteger from = NSMaxRange(open);
+ for (NSUInteger i = from; i < js.length; i++) {
unichar c = [js characterAtIndex:i];
+ if (c != '\'' && c != '\\') continue;
+ [json appendString:[js substringWithRange:NSMakeRange(from, i - from)]];
if (c == '\'') return json;
- if (c == '\\' && ++i < js.length) c = [js characterAtIndex:i];
- [json appendFormat:@"%C", c];
+ if (++i < js.length) [json appendFormat:@"%C", [js characterAtIndex:i]];
+ from = i + 1;
}
return nil;
}
diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.m
index 912c5d9b..1fbf1409 100644
--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m
+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m
@@ -66,8 +66,9 @@ static NSArray<SGKaraokeLine *> *linesFromLRC(NSString *lrc) {
}
// Without word sync a record's Lyricsfile only repeats its LRC's line times, so it is not read.
+// Whether a record has words to read: it says it times them, and it is not an instrumental.
static BOOL timesWords(NSDictionary *record) {
- return [record isKindOfClass:NSDictionary.class] && [record[@"hasWordSync"] isEqual:@YES];
+ return [record isKindOfClass:NSDictionary.class] && [record[@"hasWordSync"] isEqual:@YES] && ![record[@"instrumental"] boolValue];
}
static SGLyricsResult *syncedResult(NSArray<SGKaraokeLine *> *lines) {
@@ -171,7 +172,7 @@ static NSDictionary *bestOf(NSArray<NSDictionary *> *records, NSInteger seconds)
// another queue: a long one takes a good part of a second to read, and the main queue would stall for all of it.
static void resultFor(NSArray<NSDictionary *> *records, NSInteger seconds, void (^done)(SGLyricsResult *result)) {
NSDictionary *best = bestOf(records, seconds);
- if (!timesWords(best) || [best[@"instrumental"] boolValue]) {
+ if (!timesWords(best)) {
done(resultFrom(best, nil));
return;
}
@@ -179,7 +180,7 @@ static void resultFor(NSArray<NSDictionary *> *records, NSInteger seconds, void
NSMutableArray<NSDictionary *> *left = [records mutableCopy];
NSDictionary *top = best;
NSArray<SGKaraokeLine *> *worded = nil;
- while (timesWords(top) && ![top[@"instrumental"] boolValue]) {
+ while (timesWords(top)) {
worded = SGLyricsfileLines(top[@"lyricsfile"]);
if (worded) break;
SGLog(@"lrclib: record %@ times words in a Lyricsfile this cannot read", top[@"id"]);
@@ -188,8 +189,7 @@ static void resultFor(NSArray<NSDictionary *> *records, NSInteger seconds, void
left[[left indexOfObjectIdenticalTo:top]] = onLines;
top = bestOf(left, seconds);
}
- NSDictionary *chosen = top;
- dispatch_async(dispatch_get_main_queue(), ^{ done(resultFrom(chosen, worded)); });
+ dispatch_async(dispatch_get_main_queue(), ^{ done(resultFrom(top, worded)); });
});
}
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
index c275cecd..9691437d 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
@@ -208,6 +208,13 @@ NSString *SGLyricsFoldedText(NSString *text);
NSString *SGLyricsBareTitle(NSString *title);
NSSet<NSString *> *SGLyricsTitleVersion(NSString *title);
NSInteger SGLyricsSongRank(NSString *title, NSString *artist, NSString *songTitle, NSArray<NSString *> *songArtists);
+// Genius's search, which the lyrics source (Genius.m) and the line meanings (LyricsMeanings/Meanings.m)
+// both ask: its address, what it is asked for a track, and the song in its reply that is the track, as
+// SGLyricsSongRank tells: of several, the same version wins, then the plain song. nil when none is, or
+// the track is an instrumental or karaoke version, since Genius files the words under the song.
+#define SGGeniusSearchURL @"https://genius.com/api/search/song"
+NSDictionary<NSString *, NSString *> *SGGeniusSearchQuery(NSString *title, NSString *artist);
+NSNumber *SGGeniusSongIn(NSDictionary *response, NSString *title, NSString *artist);
// SpicyLyrics.m. Its key in the order; whether the user's own API key is in the Keychain; what keeps
// it from answering, for its row on the sources page ("Needs a key", why the key was refused), nil
diff --git a/tweak/Sources/Shared/LyricsSources/Musixmatch.m b/tweak/Sources/Shared/LyricsSources/Musixmatch.m
index e958a950..500534d4 100644
--- a/tweak/Sources/Shared/LyricsSources/Musixmatch.m
+++ b/tweak/Sources/Shared/LyricsSources/Musixmatch.m
@@ -194,17 +194,7 @@ static SGLyricsResult *fromSubtitles(id body) {
static SGLyricsResult *fromPlain(id body) {
if (![body isKindOfClass:NSString.class] || ![body length]) return nil;
- NSMutableArray<NSNumber *> *starts = [NSMutableArray array];
- NSMutableArray<NSString *> *texts = [NSMutableArray array];
- for (NSString *line in [body componentsSeparatedByString:@"\n"]) {
- [starts addObject:@0];
- [texts addObject:line.length ? line : @"♪"];
- }
- SGLyricsResult *lyrics = [SGLyricsResult new];
- lyrics.starts = starts;
- lyrics.texts = texts;
- lyrics.karaokeLines = SGKaraokeStaticLines(texts);
- return lyrics;
+ return SGLyricsPlainResult([body componentsSeparatedByString:@"\n"]);
}
// macro.subtitles.get runs the matcher and every lookup in one call; the best shape it has wins.
diff --git a/tweak/Sources/Shared/LyricsSources/SongMatch.m b/tweak/Sources/Shared/LyricsSources/SongMatch.m
index 230deb05..78fdf1ba 100644
--- a/tweak/Sources/Shared/LyricsSources/SongMatch.m
+++ b/tweak/Sources/Shared/LyricsSources/SongMatch.m
@@ -1,5 +1,6 @@
// Whether a song a catalog offers is the track playing, for the searches that answer with whole songs
-// and no id to match on: Genius's (LyricsMeanings/Meanings.m, Genius.m) and LRCLIB's (LrcLib.m).
+// and no id to match on: Genius's (Genius.m, LyricsMeanings/Meanings.m) and LRCLIB's (LrcLib.m). Genius's
+// search is here too, since both of its askers share it.
#import "Core/SGCore.h"
#import "LyricsSources.h"
@@ -107,3 +108,42 @@ NSInteger SGLyricsSongRank(NSString *title, NSString *artist, NSString *songTitl
NSSet<NSString *> *version = versionOf(songTitle);
return [version isEqualToSet:SGLyricsTitleVersion(title)] ? 3 : version.count == 0 ? 2 : 1;
}
+
+static NSString *stringIn(id value) {
+ return [value isKindOfClass:NSString.class] ? value : nil;
+}
+
+NSDictionary<NSString *, NSString *> *SGGeniusSearchQuery(NSString *title, NSString *artist) {
+ return @{@"q": [NSString stringWithFormat:@"%@ %@", SGLyricsBareTitle(title), artist], @"per_page": @"10"};
+}
+
+NSNumber *SGGeniusSongIn(NSDictionary *response, NSString *title, NSString *artist) {
+ if (![response isKindOfClass:NSDictionary.class] || !title.length || !artist.length) return nil;
+ // Genius files a song's words under the song; a track with none sung would get them anyway.
+ NSSet<NSString *> *version = SGLyricsTitleVersion(title);
+ if ([version containsObject:@"instrumental"] || [version containsObject:@"karaoke"]) return nil;
+ // Best first: the same version, then the plain song, then any other version of it.
+ NSNumber *best = nil;
+ NSInteger bestRank = 0;
+ id sections = response[@"sections"];
+ for (NSDictionary *section in [sections isKindOfClass:NSArray.class] ? sections : @[]) {
+ id hits = [section isKindOfClass:NSDictionary.class] ? section[@"hits"] : nil;
+ for (NSDictionary *hit in [hits isKindOfClass:NSArray.class] ? hits : @[]) {
+ NSDictionary *song = [hit isKindOfClass:NSDictionary.class] ? hit[@"result"] : nil;
+ if (![song isKindOfClass:NSDictionary.class] || ![song[@"id"] isKindOfClass:NSNumber.class]) continue;
+ if ([song[@"instrumental"] isEqual:@YES]) continue;
+ NSMutableArray<NSString *> *names = [NSMutableArray array];
+ id primaries = song[@"primary_artists"];
+ for (NSDictionary *primary in [primaries isKindOfClass:NSArray.class] ? primaries : @[]) {
+ NSString *name = [primary isKindOfClass:NSDictionary.class] ? stringIn(primary[@"name"]) : nil;
+ if (name) [names addObject:name];
+ }
+ NSInteger rank = SGLyricsSongRank(title, artist, stringIn(song[@"title"]), names);
+ if (rank > bestRank) {
+ best = song[@"id"];
+ bestRank = rank;
+ }
+ }
+ }
+ return best;
+}