Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: QQ Music and KuGou provide lyrics for Chinese and other Asian songs
My-Name-Is-Jeff5cee1d6f7ef9
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex b3b2c935..d9207317 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -92,7 +92,9 @@ Shared:Spotify's track id with an anonymous token, word timed where it has richsync; NetEase.m, word timing from yrc for what the others only line time; LrcLib.m, open andkeyless and timed by the line, the floor under the rest. SpicyLyrics.m, Spicy Lyrics' Developer Platformby track id on the user's own key (Keychain), syllable timed where it has a sync; checked on the Mac- against harness/spicy-lyrics/. color-lyrics is answered with whichever won+ 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/. 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 hasits request sent to a donor track that does, so the reply is a real 200 (a 404 answered as a 200 in thedelegate alone never showed the card on 9.1.78); a 404 for a track not seen yet is held until the chaindiff --git a/harness/lyrics-sources/README.md b/harness/lyrics-sources/README.mdnew file mode 100644index 00000000..07291f6f--- /dev/null+++ b/harness/lyrics-sources/README.md@@ -0,0 +1,13 @@+# Lyrics sources check++`./build.sh` checks, on the Mac as a Mac Catalyst binary, with no simulator and no network:++- KuGou's KRC (`KuGou.m`, read by `SGLyricsPieceLines` in `NetEase.m`): packed here the way KuGou packs+ it, then unpacked and read, with the word times, the joining and the line ends checked;+- the QQ Music and KuGou asks against made-up search replies: titles, singers and lengths that do not+ fit are passed over, the closest KuGou lengths are tried first and at most three, and QQ's LRC is read.++`./build.sh krc.json qq.json` also reads real replies saved from `lyrics.kugou.com/download` and QQ's+`GetPlayLyricInfo`, and prints how many lines each has. None are kept here, since they are real lyrics.++It does not cover the requests themselves, the order on the Lyrics page, or the lines on the lyrics view.diff --git a/harness/lyrics-sources/build.sh b/harness/lyrics-sources/build.shnew file mode 100755index 00000000..4b5a9d90--- /dev/null+++ b/harness/lyrics-sources/build.sh@@ -0,0 +1,12 @@+#!/bin/sh+# build.sh: builds check.m against this checkout's lyrics sources and word splitting as a Mac Catalyst+# binary and runs it, so no simulator is needed. Arguments go to the check (saved replies to read).+set -e+HERE=$(cd "$(dirname "$0")" && pwd)+SRC=$(cd "$HERE/../../tweak/Sources" && pwd)+LS="$SRC/Shared/LyricsSources"+mkdir -p "$HERE/build"+xcrun -sdk macosx clang -target arm64-apple-ios17.0-macabi -isysroot "$(xcrun --sdk macosx --show-sdk-path)" -iframework "$(xcrun --sdk macosx --show-sdk-path)/System/iOSSupport/System/Library/Frameworks" -fobjc-arc -g -Wall -I"$SRC" -DSG_VERSION=\"0.0.0\" \+ "$HERE/check.m" "$LS/KuGou.m" "$LS/QQMusic.m" "$LS/NetEase.m" "$LS/LrcLib.m" "$SRC/Shared/Lyrics/KaraokeTiming.m" "$SRC/Shared/AdBlock/Protobuf.m" \+ -framework UIKit -framework Foundation -lz -o "$HERE/build/check"+"$HERE/build/check" "$@"diff --git a/harness/lyrics-sources/check.m b/harness/lyrics-sources/check.mnew file mode 100644index 00000000..d640da6d--- /dev/null+++ b/harness/lyrics-sources/check.m@@ -0,0 +1,181 @@+// Checks what is new in the lyrics sources without the phone or the network: KuGou's KRC unpacked+// and read (KuGou.m, through NetEase.m's parser), and the QQ Music and KuGou asks matching a track+// against made-up search replies. The requests are answered here, by host. Any file named on the+// command line is a real KuGou download or QQ lyrics reply, saved as JSON, and only its line count is+// printed. Exits non-zero on the first wrong answer.+#import <UIKit/UIKit.h>+#import <zlib.h>+#import "Shared/LyricsSources/LyricsSources.h"++#define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "FAILED line %d: %s\n", __LINE__, #cond); exit(1); } } while (0)++// What the sources reach outside themselves for: the requests, answered by sg_answer.+@implementation SGLyricsResult+@end+@implementation SGLyricsQuery+@end+void SGLyricsPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts) {+ NSMutableArray *at = [NSMutableArray array], *said = [NSMutableArray array];+ for (SGKaraokeLine *line in lines) {+ [at addObject:@(line.start)];+ [said addObject:SGKaraokeLineText(line)];+ }+ *starts = at;+ *texts = said;+}+void SGLyricsNoteReply(NSURLResponse *response, NSError *error) {}+NSURL *SGLyricsURL(NSString *base, NSDictionary<NSString *, NSString *> *query) {+ NSURLComponents *url = [NSURLComponents componentsWithString:base];+ NSMutableArray<NSURLQueryItem *> *items = [NSMutableArray array];+ [query enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {+ [items addObject:[NSURLQueryItem queryItemWithName:name value:value]];+ }];+ url.queryItems = items;+ return url.URL;+}+static id (^sg_answer)(NSURL *url, id body);+void SGLyricsGetJSON(NSURL *url, NSDictionary *headers, void (^done)(id root)) { done(sg_answer(url, nil)); }+void SGLyricsPostJSON(NSURL *url, NSDictionary *headers, id body, void (^done)(id root)) { done(sg_answer(url, body)); }++static NSString *param(NSURL *url, NSString *name) {+ for (NSURLQueryItem *item in [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO].queryItems) {+ if ([item.name isEqualToString:name]) return item.value;+ }+ return nil;+}++// KRC as KuGou packs it: "krc1", then the zlib of the text XORed with the key, all in base64.+static NSString *packed(NSString *krc, NSString *magic) {+ NSData *text = [krc dataUsingEncoding:NSUTF8StringEncoding];+ uLongf length = compressBound(text.length);+ NSMutableData *zipped = [NSMutableData dataWithLength:length];+ compress(zipped.mutableBytes, &length, text.bytes, text.length);+ zipped.length = length;+ static const uint8_t key[16] = {64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105};+ uint8_t *bytes = zipped.mutableBytes;+ for (NSUInteger i = 0; i < zipped.length; i++) bytes[i] ^= key[i % 16];+ NSMutableData *all = [[magic dataUsingEncoding:NSASCIIStringEncoding] mutableCopy];+ [all appendData:zipped];+ return [all base64EncodedStringWithOptions:0];+}++static NSString *const kKRC = @"[id:$00000000]\r\n[ar:Someone]\r\n"+ "[1000,2000]<0,500,0>Hel<500,300,0>lo <800,700,0>world\r\n"+ "[4000,1500]<0,400,0>你<400,400,0>好<800,0,0>啊\r\n"+ "[7000,100]<0,300,0>late\r\n";++static void checkLive(NSString *path) {+ NSDictionary *root = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:0 error:nil];+ if (root[@"content"]) {+ NSArray<SGKaraokeLine *> *lines = SGKuGouLines(root[@"content"]);+ printf("%s: KRC, %lu word timed lines; first \"%s\" at %ld\n", path.lastPathComponent.UTF8String, (unsigned long)lines.count,+ SGKaraokeLineText(lines.firstObject).UTF8String, (long)lines.firstObject.start);+ CHECK(lines.count);+ return;+ }+ id lyric = root[@"music.musichallSong.PlayLyricInfo.GetPlayLyricInfo"][@"data"][@"lyric"];+ NSString *lrc = [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:lyric options:0] encoding:NSUTF8StringEncoding];+ SGLyricsResult *result = SGLyricsLRCResult(lrc);+ printf("%s: QQ LRC, %lu timed lines\n", path.lastPathComponent.UTF8String, (unsigned long)result.karaokeLines.count);+ CHECK(result.synced && result.karaokeLines.count);+}++int main(int argc, char **argv) {+ @autoreleasepool {+ // The keys the two catalogues are matched by.+ CHECK([SGLyricsMatchKey(@"晴天(Live版)") isEqualToString:SGLyricsMatchKey(@"晴天 (Live版)")]);+ CHECK([SGLyricsMatchKey(@"Hello, World!") isEqualToString:@"helloworld"]);+ CHECK([SGLyricsMatchKey(@"《稻香》") isEqualToString:@"稻香"]);+ CHECK([SGLyricsLeadArtist(@"周杰伦 feat. 费玉清") isEqualToString:@"周杰伦"]);+ CHECK(SGLyricsSameArtist(@"周杰伦、费玉清", @"周杰伦 Feat. Someone") && !SGLyricsSameArtist(@"林俊杰", @"周杰伦"));+ CHECK(!SGLyricsSameArtist(@"", @"周杰伦") && !SGLyricsSameArtist(nil, @"x"));++ // KRC: pieces timed from their line's start, a spaced word's pieces run together, a syllable each+ // in Chinese, a piece of no length shown for 1 ms, a line ending at its last word when that is later.+ NSArray<SGKaraokeLine *> *lines = SGKuGouLines(packed(kKRC, @"krc1"));+ CHECK(lines.count == 3);+ CHECK([SGKaraokeLineText(lines[0]) isEqualToString:@"Hello world"] && lines[0].words.count == 2);+ CHECK(lines[0].words[0].start == 1000 && lines[0].words[0].end == 1800 && lines[0].words[1].start == 1800);+ CHECK(lines[0].start == 1000 && lines[0].end == 3000 && lines[0].timing == SGKaraokeTimingWords);+ CHECK(lines[1].words.count == 3 && [SGKaraokeLineText(lines[1]) isEqualToString:@"你好啊"]);+ CHECK(lines[1].words[1].joined && lines[1].words[2].start == 4800 && lines[1].words[2].end == 4801);+ CHECK(lines[2].end == 7300);+ CHECK(!SGKuGouLines(packed(kKRC, @"krc2")) && !SGKuGouLines(@"bm90IGtyYw==") && !SGKuGouLines(nil));+ CHECK(!SGKuGouLines(packed(@"[ar:only tags]", @"krc1")));++ // KuGou: the user uploads, another title, another singer, a length 21 s off and a candidate with+ // no key are passed over; the rest are tried closest in length first, three at most.+ NSArray *candidates = @[+ @{@"id": @"a", @"accesskey": @"k", @"product_from": @"ugc", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @269000},+ @{@"id": @"b", @"accesskey": @"k", @"song": @"晴天 (Live)", @"singer": @"周杰伦", @"duration": @269000},+ @{@"id": @"c", @"accesskey": @"k", @"song": @"晴天", @"singer": @"Other", @"duration": @269000},+ @{@"id": @"d", @"accesskey": @"k", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @290000},+ @{@"id": @"e", @"accesskey": @"k", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @265000},+ @{@"id": @7, @"accesskey": @"k", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @269500},+ @{@"id": @"g", @"accesskey": @"k", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @262000},+ @{@"id": @"h", @"song": @"晴天", @"singer": @"周杰伦", @"duration": @270000},+ @{@"id": @"i", @"accesskey": @"k", @"song": @" 晴天!", @"singer": @"周杰伦、费玉清", @"duration": @"268000"},+ ];+ NSMutableArray<NSString *> *tried = [NSMutableArray array];+ __block NSString *keyword, *duration, *good = @"e";+ sg_answer = ^id(NSURL *url, id body) {+ if ([url.host isEqualToString:@"krcs.kugou.com"]) {+ keyword = param(url, @"keyword");+ duration = param(url, @"duration");+ return @{@"status": @200, @"candidates": candidates};+ }+ NSString *song = param(url, @"id");+ [tried addObject:song];+ if ([song isEqualToString:good]) return @{@"status": @200, @"content": packed(kKRC, @"krc1")};+ return [song isEqualToString:@"7"] ? @{@"status": @200, @"content": @"garbage"} : @{@"status": @404};+ };+ SGLyricsQuery *query = [SGLyricsQuery new];+ query.title = @"晴天";+ query.artist = @"周杰伦 feat. Someone";+ query.seconds = 269;+ __block SGLyricsResult *got = nil;+ SGKuGouAsk(query, ^(SGLyricsResult *result) { got = result; });+ CHECK([keyword isEqualToString:@"周杰伦 - 晴天"] && [duration isEqualToString:@"269000"]);+ CHECK([tried isEqualToArray:(@[@"7", @"i", @"e"])]);+ CHECK(got.synced && got.wordTimed && got.karaokeLines.count == 3 && got.texts.count == 3);+ [tried removeAllObjects];+ good = @"g"; // fourth in line: never asked+ got = [SGLyricsResult new];+ SGKuGouAsk(query, ^(SGLyricsResult *result) { got = result; });+ CHECK([tried isEqualToArray:(@[@"7", @"i", @"e"])] && !got);+ query.artist = @"";+ got = [SGLyricsResult new];+ [tried removeAllObjects];+ SGKuGouAsk(query, ^(SGLyricsResult *result) { got = result; });+ CHECK(!got && !tried.count);++ // QQ Music: the first result with the title, a singer and the length is taken, and its LRC read.+ NSString *lrc = @"[ti:x]\n[ar:y]\n[00:01.00]第一行\n[00:03.50]第二行\n";+ __block NSDictionary *asked = nil;+ sg_answer = ^id(NSURL *url, NSDictionary *body) {+ if (body[@"req"]) return @{@"req": @{@"data": @{@"body": @{@"song": @{@"list": @[+ @{@"title": @"晴天", @"interval": @269, @"singer": @[@{@"name": @"Someone Else"}], @"id": @1},+ @{@"title": @"晴天", @"interval": @300, @"singer": @[@{@"name": @"周杰伦"}], @"id": @2},+ @{@"title": @"雨天", @"interval": @269, @"singer": @[@{@"name": @"周杰伦"}], @"id": @3},+ @{@"songname": @"晴天", @"interval": @270, @"singer": @[@{@"name": @"x"}, @{@"name": @"周杰伦"}], @"id": @97773},+ ]}}}}};+ asked = body[@"music.musichallSong.PlayLyricInfo.GetPlayLyricInfo"][@"param"];+ NSString *encoded = [[lrc dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];+ return @{@"music.musichallSong.PlayLyricInfo.GetPlayLyricInfo": @{@"data": @{@"lyric": encoded}}};+ };+ query.artist = @"周杰伦";+ got = nil;+ SGQQMusicAsk(query, ^(SGLyricsResult *result) { got = result; });+ CHECK([asked[@"songID"] isEqual:@97773] && [asked[@"qrc"] isEqual:@0] && [asked[@"crypt"] isEqual:@0]);+ CHECK(got.synced && !got.wordTimed && got.karaokeLines.count == 2 && got.karaokeLines[1].start == 3500);+ query.seconds = 200; // no recording that long: nothing asked for lyrics+ asked = nil;+ got = [SGLyricsResult new];+ SGQQMusicAsk(query, ^(SGLyricsResult *result) { got = result; });+ CHECK(!got && !asked);++ for (int i = 1; i < argc; i++) checkLive(@(argv[i]));+ printf("lyrics-sources: all checks passed\n");+ }+ return 0;+}diff --git a/tweak/Makefile b/tweak/Makefileindex 370cf163..d8a67fe1 100644--- a/tweak/Makefile+++ b/tweak/Makefile@@ -17,6 +17,8 @@ TWEAK_NAME := spotifyglassspotifyglass_FILES := $(shell find Sources -name '*.m' -o -name '*.x' -o -name '*.swift' | sort)spotifyglass_CFLAGS := -fobjc-arc -ISources -DSG_VERSION=\"$(SG_VERSION)\"spotifyglass_FRAMEWORKS := UIKit QuartzCore UniformTypeIdentifiers MediaPlayer CoreImage AudioToolbox AVFoundation CoreHaptics Accelerate CoreMotion CoreML+# zlib unpacks KuGou's KRC lyrics (Shared/LyricsSources/KuGou.m).+spotifyglass_LIBRARIES := z# Runtime swizzling instead of Substrate so the dylib has no extra dependency inside the IPA.spotifyglass_LOGOS_DEFAULT_GENERATOR := internaldiff --git a/tweak/Sources/Shared/LyricsSources/KuGou.m b/tweak/Sources/Shared/LyricsSources/KuGou.mnew file mode 100644index 00000000..c76aef30--- /dev/null+++ b/tweak/Sources/Shared/LyricsSources/KuGou.m@@ -0,0 +1,110 @@+// KuGou, word timing for the Mandarin, Cantonese and other Asian songs the rest only time by the line+// or not at all. Its KRC is NetEase's yrc in all but brackets and clock (SGLyricsPieceLines), packed:+// base64 of "krc1", then the zlib of the text XORed with a key KuGou's clients all carry. Searched by+// artist, title and length; the user uploads are left out, and a recording is only taken when its+// title, singer and length agree with the track's.+#import <zlib.h>+#import "Core/SGCore.h"+#import "LyricsSources.h"++static NSString *const kSearch = @"https://krcs.kugou.com/search";+static NSString *const kDownload = @"https://lyrics.kugou.com/download";+static const NSInteger kLengthSlackMs = 8000;+static const NSUInteger kTriedSongs = 3;+// Lyrics take a few kilobytes; anything that unpacks past this is not lyrics, and is refused.+static const NSUInteger kMostBytes = 1 << 20;+static const uint8_t kKey[16] = {64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105};++NSArray<SGKaraokeLine *> *SGKuGouLines(NSString *content) {+ NSData *packed = [content isKindOfClass:NSString.class]+ ? [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters] : nil;+ if (packed.length <= 4 || packed.length > kMostBytes || memcmp(packed.bytes, "krc1", 4) != 0) return nil;+ NSMutableData *zipped = [[packed subdataWithRange:NSMakeRange(4, packed.length - 4)] mutableCopy];+ uint8_t *bytes = zipped.mutableBytes;+ for (NSUInteger i = 0; i < zipped.length; i++) bytes[i] ^= kKey[i % sizeof kKey];+ NSMutableData *text = [NSMutableData dataWithLength:kMostBytes];+ uLongf length = kMostBytes;+ if (uncompress(text.mutableBytes, &length, bytes, zipped.length) != Z_OK) return nil;+ text.length = length;+ NSString *krc = [[NSString alloc] initWithData:text encoding:NSUTF8StringEncoding];+ return krc ? SGLyricsPieceLines(krc, YES) : nil;+}++// KuGou writes its ids as strings and its lengths as numbers, but either may come as the other.+static NSString *stringIn(id value) {+ return [value isKindOfClass:NSString.class] ? value : [value isKindOfClass:NSNumber.class] ? [value stringValue] : nil;+}++static NSInteger msIn(NSDictionary *candidate) {+ id duration = candidate[@"duration"];+ return [duration respondsToSelector:@selector(integerValue)] ? [duration integerValue] : 0;+}++static NSDictionary<NSString *, NSString *> *headers(void) {+ return @{@"User-Agent": SGLyricsBrowserAgent};+}++static void tryLyrics(NSArray<NSDictionary *> *candidates, NSUInteger index, void (^done)(NSArray<SGKaraokeLine *> *)) {+ if (index >= candidates.count || index >= kTriedSongs) {+ done(nil);+ return;+ }+ NSDictionary *candidate = candidates[index];+ NSURL *url = SGLyricsURL(kDownload, @{@"ver": @"1", @"client": @"pc", @"id": stringIn(candidate[@"id"]),+ @"accesskey": stringIn(candidate[@"accesskey"]), @"fmt": @"krc", @"charset": @"utf8"});+ SGLyricsGetJSON(url, headers(), ^(id root) {+ id status = [root isKindOfClass:NSDictionary.class] ? root[@"status"] : nil;+ NSArray<SGKaraokeLine *> *lines = [status respondsToSelector:@selector(integerValue)] && [status integerValue] == 200+ ? SGKuGouLines(root[@"content"]) : nil;+ if (lines) {+ SGLog(@"kugou: lyrics %@ have %lu word timed lines", stringIn(candidate[@"id"]), (unsigned long)lines.count);+ done(lines);+ } else {+ tryLyrics(candidates, index + 1, done);+ }+ });+}++SGLyricsAsk SGKuGouAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {+ NSString *title = SGLyricsMatchKey(query.title), *lead = SGLyricsLeadArtist(query.artist);+ if (!title.length || !SGLyricsMatchKey(lead).length) {+ done(nil);+ return;+ }+ NSInteger ms = query.seconds * 1000;+ NSMutableDictionary<NSString *, NSString *> *search = [@{@"ver": @"1", @"man": @"yes", @"client": @"mobi", @"hash": @"",+ @"album_audio_id": @"", @"keyword": [NSString stringWithFormat:@"%@ - %@", lead, query.title]} mutableCopy];+ if (ms > 0) search[@"duration"] = @(ms).stringValue;+ SGLyricsGetJSON(SGLyricsURL(kSearch, search), headers(), ^(id root) {+ id candidates = [root isKindOfClass:NSDictionary.class] ? root[@"candidates"] : nil;+ NSMutableArray<NSDictionary *> *fitting = [NSMutableArray array];+ for (NSDictionary *candidate in [candidates isKindOfClass:NSArray.class] ? candidates : @[]) {+ if (![candidate isKindOfClass:NSDictionary.class]) continue;+ if (!stringIn(candidate[@"id"]).length || !stringIn(candidate[@"accesskey"]).length) continue;+ if ([candidate[@"product_from"] isEqual:@"ugc"]) continue;+ if (![SGLyricsMatchKey(candidate[@"song"]) isEqualToString:title]) continue;+ if (!SGLyricsSameArtist(candidate[@"singer"], lead)) continue;+ if (ms > 0 && msIn(candidate) > 0 && labs(msIn(candidate) - ms) > kLengthSlackMs) continue;+ [fitting addObject:candidate];+ }+ [fitting sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {+ return [@(labs(msIn(a) - ms)) compare:@(labs(msIn(b) - ms))];+ }];+ if (!fitting.count) SGLog(@"kugou: no recording of %@ by %@ near %lds", query.title, lead, (long)query.seconds);+ tryLyrics(fitting, 0, ^(NSArray<SGKaraokeLine *> *lines) {+ if (!lines.count) {+ done(nil);+ return;+ }+ SGLyricsResult *result = [SGLyricsResult new];+ result.wordTimed = result.synced = YES;+ result.karaokeLines = lines;+ NSArray<NSNumber *> *starts;+ NSArray<NSString *> *texts;+ SGLyricsPageLines(lines, &starts, &texts);+ result.starts = starts;+ result.texts = texts;+ done(result);+ });+ });+};diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.mindex dce66395..5de31232 100644--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m@@ -64,23 +64,28 @@ static NSArray<SGKaraokeLine *> *linesFromLRC(NSString *lrc) {return SGKaraokeEstimatedLines(starts, texts);}+SGLyricsResult *SGLyricsLRCResult(NSString *lrc) {+ NSArray<SGKaraokeLine *> *lines = [lrc isKindOfClass:NSString.class] ? linesFromLRC(lrc) : nil;+ if (!lines) return nil;+ SGLyricsResult *result = [SGLyricsResult new];+ result.synced = YES;+ result.karaokeLines = lines;+ NSArray<NSNumber *> *starts;+ NSArray<NSString *> *texts;+ SGLyricsPageLines(lines, &starts, &texts);+ result.starts = starts;+ result.texts = texts;+ return result;+}+static SGLyricsResult *resultFrom(NSDictionary *record) {if (![record isKindOfClass:NSDictionary.class]) return nil;SGLyricsResult *result = [SGLyricsResult new];result.instrumental = [record[@"instrumental"] boolValue];if (result.instrumental) return result;id synced = record[@"syncedLyrics"], plain = record[@"plainLyrics"];- NSArray<SGKaraokeLine *> *lines = [synced isKindOfClass:NSString.class] ? linesFromLRC(synced) : nil;- if (lines) {- result.synced = YES;- result.karaokeLines = lines;- NSArray<NSNumber *> *starts;- NSArray<NSString *> *texts;- SGLyricsPageLines(lines, &starts, &texts);- result.starts = starts;- result.texts = texts;- return result;- }+ SGLyricsResult *timed = SGLyricsLRCResult(synced);+ if (timed) return timed;// Nothing timed, but the words still beat an empty page when no other source has any.if (![plain isKindOfClass:NSString.class] || ![plain length]) return nil;NSMutableArray<NSNumber *> *starts = [NSMutableArray array];diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.hindex 8da7d898..f6917b97 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h@@ -136,6 +136,24 @@ extern SGLyricsAsk SGUnisonAsk;extern SGLyricsAsk SGNetEaseAsk;extern SGLyricsAsk SGLrcLibAsk;extern SGLyricsAsk SGSpicyLyricsAsk;+extern SGLyricsAsk SGQQMusicAsk;+extern SGLyricsAsk SGKuGouAsk;++// LrcLib.m. Timed LRC as a synced result, nil when no line in it is timed.+SGLyricsResult *SGLyricsLRCResult(NSString *lrc);+// NetEase.m. Word timed lines from NetEase's yrc, or with krc from KuGou's KRC text; nil for none.+NSArray<SGKaraokeLine *> *SGLyricsPieceLines(NSString *body, BOOL krc);+// QQMusic.m, for it and KuGou. A title as the two are matched by: lowercased, without spaces,+// punctuation or symbols, ideographic and full width ones included. The artist without whoever is+// featured ("A feat. B" is A). Whether a credited name and the track's artist name the same one: the+// key of either holds the other's, the featured artists left out.+NSString *SGLyricsMatchKey(NSString *text);+NSString *SGLyricsLeadArtist(NSString *artist);+BOOL SGLyricsSameArtist(NSString *credited, NSString *artist);+// What the two catalogues are asked as: a browser.+#define SGLyricsBrowserAgent @"Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"+// KuGou.m. A download's base64 content as lines, nil when it is not KRC or holds no timed line.+NSArray<SGKaraokeLine *> *SGKuGouLines(NSString *content);// SpicyLyrics.m. Its key in the order; whether the user's own API key is in the Keychain; the// Lyrics page's row that sets or removes it; and an API response as lines, nil when it has none.diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.mindex af261003..cb3e8a3a 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m@@ -160,6 +160,8 @@ NSArray<SGLyricsProvider *> *SGLyricsAllProviders(void) {make(@"netease", @"NetEase", @"Word timing, censored", SGNetEaseAsk),make(@"lrclib", @"LRCLIB", @"Line timing, open fallback", SGLrcLibAsk),make(SGSpicyLyricsKey, @"Spicy Lyrics", @"Community word syncs, your own key", SGSpicyLyricsAsk),+ make(@"qqmusic", @"QQ Music", @"Line timing, Chinese catalogue", SGQQMusicAsk),+ make(@"kugou", @"KuGou", @"Word timing, Chinese catalogue", SGKuGouAsk),];});return all;diff --git a/tweak/Sources/Shared/LyricsSources/NetEase.m b/tweak/Sources/Shared/LyricsSources/NetEase.mindex 107da924..bd883576 100644--- a/tweak/Sources/Shared/LyricsSources/NetEase.m+++ b/tweak/Sources/Shared/LyricsSources/NetEase.m@@ -30,19 +30,23 @@ static void get(NSString *path, NSDictionary<NSString *, NSString *> *query, voi// [43060,3600](43060,120,0)To (43180,330,0)seize (43510,420,0)everything …// A line's start and length in ms, then each piece's start and length before its text. A piece not-// ending in a space runs on into the next one ("wanted" "…"), as in richsync.-static NSArray<SGKaraokeLine *> *linesFromYrc(NSString *yrc) {- static NSRegularExpression *header, *piece;+// ending in a space runs on into the next one ("wanted" "…"), as in richsync. KuGou's KRC is written+// the same way, its pieces in <> and timed from the start of their line instead of the track's.+NSArray<SGKaraokeLine *> *SGLyricsPieceLines(NSString *body, BOOL krc) {+ static NSRegularExpression *header, *yrcPiece, *krcPiece;static dispatch_once_t once;dispatch_once(&once, ^{header = [NSRegularExpression regularExpressionWithPattern:@"^\\[(\\d+),(\\d+)\\]" options:0 error:nil];- piece = [NSRegularExpression regularExpressionWithPattern:@"\\((\\d+),(\\d+),-?\\d+\\)" options:0 error:nil];+ yrcPiece = [NSRegularExpression regularExpressionWithPattern:@"\\((\\d+),(\\d+),-?\\d+\\)" options:0 error:nil];+ krcPiece = [NSRegularExpression regularExpressionWithPattern:@"<(\\d+),(\\d+),-?\\d+>" options:0 error:nil];});+ NSRegularExpression *piece = krc ? krcPiece : yrcPiece;NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];- for (NSString *row in [yrc componentsSeparatedByString:@"\n"]) {+ for (NSString *row in [body componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {NSTextCheckingResult *head = [header firstMatchInString:row options:0 range:NSMakeRange(0, row.length)];if (!head) continue;NSArray<NSTextCheckingResult *> *pieces = [piece matchesInString:row options:0 range:NSMakeRange(NSMaxRange(head.range), row.length - NSMaxRange(head.range))];+ NSInteger lineStart = [row substringWithRange:[head rangeAtIndex:1]].integerValue;NSMutableArray<SGKaraokeWord *> *words = [NSMutableArray array];SGKaraokeWord *open = nil;BOOL spaced = YES; // a space has gone by, so the next word is not joined to the last@@ -50,8 +54,9 @@ static NSArray<SGKaraokeLine *> *linesFromYrc(NSString *yrc) {NSUInteger from = NSMaxRange(pieces[i].range), to = i + 1 < pieces.count ? pieces[i + 1].range.location : row.length;NSString *raw = [row substringWithRange:NSMakeRange(from, to - from)];NSString *text = [raw stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];- NSInteger start = [row substringWithRange:[pieces[i] rangeAtIndex:1]].integerValue;- NSInteger end = start + [row substringWithRange:[pieces[i] rangeAtIndex:2]].integerValue;+ NSInteger start = [row substringWithRange:[pieces[i] rangeAtIndex:1]].integerValue + (krc ? lineStart : 0);+ // A piece of no length is still shown, for a moment.+ NSInteger end = start + MAX([row substringWithRange:[pieces[i] rangeAtIndex:2]].integerValue, 1);// yrc times a Chinese or Japanese syllable a piece at a time and never spaces them, so// each one is a word of its own; only a spaced script runs its pieces together.BOOL unspaced = SGKaraokeUnspacedScript(text);@@ -76,8 +81,8 @@ static NSArray<SGKaraokeLine *> *linesFromYrc(NSString *yrc) {if (!words.count) continue;SGKaraokeLine *line = [SGKaraokeLine new];line.words = words;- line.start = [row substringWithRange:[head rangeAtIndex:1]].integerValue;- line.end = line.start + [row substringWithRange:[head rangeAtIndex:2]].integerValue;+ line.start = lineStart;+ line.end = MAX(lineStart + [row substringWithRange:[head rangeAtIndex:2]].integerValue, words.lastObject.end);[lines addObject:line];}return lines.count ? lines : nil;@@ -90,7 +95,7 @@ static void tryLyrics(NSArray<NSNumber *> *songs, NSUInteger index, void (^done)}get(@"song/lyric/v1", @{@"id": songs[index].stringValue, @"lv": @"1", @"yv": @"1", @"tv": @"-1"}, ^(NSDictionary *root) {id yrc = [root[@"yrc"] isKindOfClass:NSDictionary.class] ? root[@"yrc"][@"lyric"] : nil;- NSArray<SGKaraokeLine *> *lines = [yrc isKindOfClass:NSString.class] ? linesFromYrc(yrc) : nil;+ NSArray<SGKaraokeLine *> *lines = [yrc isKindOfClass:NSString.class] ? SGLyricsPieceLines(yrc, NO) : nil;if (lines) {SGLog(@"netease: song %@ has %lu word timed lines", songs[index], (unsigned long)lines.count);done(lines);diff --git a/tweak/Sources/Shared/LyricsSources/QQMusic.m b/tweak/Sources/Shared/LyricsSources/QQMusic.mnew file mode 100644index 00000000..11d45530--- /dev/null+++ b/tweak/Sources/Shared/LyricsSources/QQMusic.m@@ -0,0 +1,98 @@+// QQ Music, for the Mandarin, Cantonese and other Asian songs the rest have no timed lyrics for.+// Timed by the line: its own word timing (QRC) comes enciphered, so plain LRC is asked for and read+// the way LRCLIB's is. Searched by title and artist, and a recording is only taken when its title+// is the track's, one of its singers is the track's artist and its length is about the track's, so a+// cover or a namesake by someone else is passed over.+#import "Core/SGCore.h"+#import "LyricsSources.h"++static NSString *const kAPI = @"https://u.y.qq.com/cgi-bin/musicu.fcg";+static const NSInteger kLengthSlack = 6;+static NSString *const kLyricRequest = @"music.musichallSong.PlayLyricInfo.GetPlayLyricInfo";++NSString *SGLyricsMatchKey(NSString *text) {+ static NSCharacterSet *dropped;+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ NSMutableCharacterSet *building = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];+ [building formUnionWithCharacterSet:NSCharacterSet.punctuationCharacterSet];+ [building formUnionWithCharacterSet:NSCharacterSet.symbolCharacterSet];+ dropped = [building copy];+ });+ if (![text isKindOfClass:NSString.class]) return @"";+ return [[text.lowercaseString componentsSeparatedByCharactersInSet:dropped] componentsJoinedByString:@""];+}++NSString *SGLyricsLeadArtist(NSString *artist) {+ NSRange featuring = [artist rangeOfString:@" feat" options:NSCaseInsensitiveSearch];+ return featuring.location == NSNotFound ? artist : [artist substringToIndex:featuring.location];+}++BOOL SGLyricsSameArtist(NSString *credited, NSString *artist) {+ NSString *name = SGLyricsMatchKey(credited), *lead = SGLyricsMatchKey(SGLyricsLeadArtist(artist));+ return name.length && lead.length && ([name containsString:lead] || [lead containsString:name]);+}++static id dig(id root, NSArray<NSString *> *path) {+ for (NSString *key in path) root = [root isKindOfClass:NSDictionary.class] ? root[key] : nil;+ return root;+}++static void ask(NSString *name, NSDictionary *request, void (^done)(id reply)) {+ NSDictionary *body = @{@"comm": @{@"ct": @"19", @"cv": @"1859", @"uin": @"0"}, name: request};+ NSDictionary *headers = @{@"Referer": @"https://y.qq.com/", @"User-Agent": SGLyricsBrowserAgent, @"Accept": @"application/json"};+ SGLyricsPostJSON([NSURL URLWithString:kAPI], headers, body, ^(id root) {+ done(dig(root, @[name, @"data"]));+ });+}++// The first recording on the page that is the track: QQ ranks its own catalogue well, so the first+// that fits is taken rather than the closest in length.+static NSNumber *songFor(id data, SGLyricsQuery *query) {+ NSString *title = SGLyricsMatchKey(query.title);+ id list = dig(data, @[@"body", @"song", @"list"]);+ for (NSDictionary *song in [list isKindOfClass:NSArray.class] ? list : @[]) {+ if (![song isKindOfClass:NSDictionary.class]) continue;+ id name = [song[@"title"] isKindOfClass:NSString.class] ? song[@"title"] : song[@"songname"];+ if (![SGLyricsMatchKey(name) isEqualToString:title]) continue;+ id interval = song[@"interval"];+ NSInteger length = [interval isKindOfClass:NSNumber.class] ? [interval integerValue] : 0;+ if (query.seconds > 0 && length > 0 && labs(length - query.seconds) > kLengthSlack) continue;+ BOOL sung = NO;+ id singers = song[@"singer"];+ for (NSDictionary *singer in [singers isKindOfClass:NSArray.class] ? singers : @[]) {+ sung = sung || ([singer isKindOfClass:NSDictionary.class] && SGLyricsSameArtist(singer[@"name"], query.artist));+ }+ if (sung && [song[@"id"] isKindOfClass:NSNumber.class]) return song[@"id"];+ }+ return nil;+}++SGLyricsAsk SGQQMusicAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {+ NSString *lead = SGLyricsLeadArtist(query.artist);+ if (!SGLyricsMatchKey(query.title).length || !SGLyricsMatchKey(lead).length) {+ done(nil);+ return;+ }+ ask(@"req", @{@"module": @"music.search.SearchCgiService", @"method": @"DoSearchForQQMusicDesktop", @"param": @{+ @"query": [NSString stringWithFormat:@"%@ %@", query.title, lead],+ @"search_type": @0, @"grp": @1, @"num_per_page": @8, @"page_num": @1,+ }}, ^(id found) {+ NSNumber *song = songFor(found, query);+ if (!song) {+ SGLog(@"qqmusic: no recording of %@ by %@ near %lds", query.title, lead, (long)query.seconds);+ done(nil);+ return;+ }+ ask(kLyricRequest, @{@"module": @"music.musichallSong.PlayLyricInfo", @"method": @"GetPlayLyricInfo",+ @"param": @{@"crypt": @0, @"qrc": @0, @"songID": song}}, ^(id data) {+ id lyric = dig(data, @[@"lyric"]);+ NSData *decoded = [lyric isKindOfClass:NSString.class]+ ? [[NSData alloc] initWithBase64EncodedString:lyric options:NSDataBase64DecodingIgnoreUnknownCharacters] : nil;+ NSString *lrc = decoded ? [[NSString alloc] initWithData:decoded encoding:NSUTF8StringEncoding] : nil;+ SGLyricsResult *result = SGLyricsLRCResult(lrc);+ SGLog(@"qqmusic: song %@ gave %lu timed lines", song, (unsigned long)result.karaokeLines.count);+ done(result);+ });+ });+};