Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: Apple Music lookups back off and match albums without edition suffixes
My-Name-Is-Jeff6c9ac32b13a0
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 1ec2ef48..b2268706 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -128,7 +128,9 @@ Shared:Still (one frame) or Animated. Rendered on the Mac against harness/lyrics-clip/. What reaches thesystem's now playing through this hook and Player/NowPlayingExtras.x is checked by harness/now-playing/AnimatedArtwork/ moving artwork (AnimatedArtwork.h): the track's Canvas, else Apple Music's animated album cover- (SGMotionCatalog.m), kept as local files (SGMotionStore.m), on iOS 26 the lock screen's full-screen+ (SGMotionCatalog.m: searched without the album's edition, the edition of the same name first, and+ left alone for 10 minutes after a 403 or 429), kept as local files (SGMotionStore.m, a download that+ fails on the way tried once more), on iOS 26 the lock screen's full-screenartwork (LockScreenMotion.x). With Every song, a song with neither gets its cover over copies of itblurred and swaying, a seamless 8 s 3:4 loop drawn on the CPU only when the lock screen asks for itand kept per picture (SGFluidClip.m). Rendered on the Mac against harness/fluid-clip/. A track whosediff --git a/harness/player/README.md b/harness/player/README.mdindex 4d71edc5..286a11ce 100644--- a/harness/player/README.md+++ b/harness/player/README.md@@ -75,6 +75,10 @@ come late, out of order, or not at all.away at once, with no fade. The log ends with `motion checks: n of 25 right -- PASS` or `FAIL`.`HARNESS_CANVAS_FAILS=n` fails the first n requests for the clip on the way: with 1 the store tries again andevery check passes, with 2 the clip never comes.+- `catalog` is Apple Music's catalog (`SGMotionCatalog.m`) behind a stub of its search: the names searched+ without their edition, an album search for "Midnights (3am Edition)" asking "Taylor Swift Midnights" and taking+ the edition of that same name before the standard album, then a 429 and the next album finding nothing at once+ with nothing asked. The log ends with `catalog checks: n of 5 right -- PASS` or `FAIL`.- `settings` opens the redesign's Player page (`PlayerSettings.m`, with the real `Settings/` framework) over theplayer at 3 s, with the clip of `motion` in. It checks the card leads the page with the five backgrounds'diff --git a/harness/player/main.m b/harness/player/main.mindex 816153a4..aaca5447 100644--- a/harness/player/main.m+++ b/harness/player/main.m@@ -19,6 +19,8 @@// motion Animated artwork: a clip (HARNESS_CANVAS_FILE, an mp4) that comes in before the player has// laid out, then the field, the lyrics, a field built again, the menu's switch and the player// closed, each step checked; the log ends with motion checks n of m right -- PASS or FAIL+// catalog Apple Music's catalog behind a stub: the search term without the edition, the album of the same+// name first, and the pause after a 429; the log says PASS or FAIL// settings the redesign's Player page (PlayerSettings.m) over the player with the clip of `motion`: the// card over Animated, then Fluid, Colours, Still and Visualiser as the segmented control picks// them, and Animated again, the note under it changing and the rows under the header holding@@ -70,6 +72,11 @@ static NSMutableDictionary<NSString *, SGRHarnessPicture *> *sg_pictures;static NSUInteger sg_served;// How many times the clip itself was served.static NSUInteger sg_canvasServed;+// The catalog scenario's Apple Music: the addresses asked, in order, what it answers, and the clips' playlists+// asked for after it.+static NSMutableArray<NSURL *> *sg_catalogAsked, *sg_canvasAsked;+static NSInteger sg_catalogStatus = 200;+static NSData *sg_catalogBody;static void serve(NSString *imageURI, UIImage *image, NSTimeInterval delay, BOOL fails) {if (!sg_pictures) sg_pictures = [NSMutableDictionary dictionary];@@ -88,6 +95,7 @@ static void serve(NSString *imageURI, UIImage *image, NSTimeInterval delay, BOOL}+ (BOOL)canInitWithRequest:(NSURLRequest *)request {+ if ([request.URL.host isEqualToString:@"amp-api.music.apple.com"]) return sg_catalogAsked != nil;return [request.URL.host isEqualToString:@"i.scdn.co"] || [request.URL.host isEqualToString:@"canvas.harness"];}@@ -96,6 +104,20 @@ static void serve(NSString *imageURI, UIImage *image, NSTimeInterval delay, BOOL}- (void)startLoading {+ if ([self.request.URL.host isEqualToString:@"amp-api.music.apple.com"]) {+ @synchronized (sg_catalogAsked) { [sg_catalogAsked addObject:self.request.URL]; }+ NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL statusCode:sg_catalogStatus HTTPVersion:@"HTTP/1.1"+ headerFields:@{@"Content-Type": @"application/json"}];+ [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];+ [self.client URLProtocol:self didLoadData:sg_catalogBody ?: [NSData data]];+ [self.client URLProtocolDidFinishLoading:self];+ return;+ }+ if (sg_canvasAsked && [self.request.URL.path hasSuffix:@".m3u8"]) {+ @synchronized (sg_canvasAsked) { [sg_canvasAsked addObject:self.request.URL]; }+ [self answer:nil];+ return;+ }// The motion scenario's clip, at once, or HARNESS_CANVAS_DELAY seconds late.if ([self.request.URL.host isEqualToString:@"canvas.harness"]) {const char *file = getenv("HARNESS_CANVAS_FILE"), *delay = getenv("HARNESS_CANVAS_DELAY");@@ -591,6 +613,7 @@ static void loadLyrics(void) {else if ([scenario() isEqualToString:@"scroll"]) [self runScrollChecks];else if ([scenario() isEqualToString:@"landscape"]) [self runLandscape];else if ([scenario() isEqualToString:@"motion"]) [self runMotionChecks];+ else if ([scenario() isEqualToString:@"catalog"]) [self runCatalogChecks];else if ([scenario() isEqualToString:@"badge"]) [self runBadgeChecks];else if ([scenario() isEqualToString:@"cover"]) [self runCoverChecks];else if ([scenario() isEqualToString:@"immersive"]) [self runImmersiveChecks];@@ -1081,6 +1104,76 @@ static BOOL fieldCovered(void) {});}+#pragma mark - Apple Music's catalog++static NSData *catalogAlbums(NSArray<NSArray<NSString *> *> *albums) {+ NSMutableArray *data = [NSMutableArray array];+ for (NSArray<NSString *> *album in albums) {+ [data addObject:@{@"attributes": @{@"name": album[0], @"artistName": album[1],+ @"editorialVideo": @{@"motionTallVideo3x4": @{@"video": album[2]}}}}];+ }+ return [NSJSONSerialization dataWithJSONObject:@{@"results": @{@"albums": @{@"data": data}}} options:0 error:nil];+}++static NSString *termOf(NSURL *url) {+ for (NSURLQueryItem *item in [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO].queryItems) {+ if ([item.name isEqualToString:@"term"]) return item.value;+ }+ return nil;+}++// The album search's term without the edition, the album of the same name before another edition, and the pause+// after a 429: nothing asked of the catalog for the next album, which finds nothing at once.+- (void)runCatalogChecks {+ sg_catalogAsked = [NSMutableArray array];+ sg_canvasAsked = [NSMutableArray array];+ // A web player token that has not run out, so no browse page is read.+ NSString *(^part)(NSString *) = ^NSString *(NSString *json) {+ NSString *base = [[json dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];+ return [[[base stringByReplacingOccurrencesOfString:@"+" withString:@"-"] stringByReplacingOccurrencesOfString:@"/" withString:@"_"]+ stringByReplacingOccurrencesOfString:@"=" withString:@""];+ };+ NSString *token = [NSString stringWithFormat:@"%@.%@.c2ln", part(@"{\"alg\":\"none\"}"), part(@"{\"iss\":\"AMPWebPlay\",\"exp\":4102444800}")];+ [NSUserDefaults.standardUserDefaults setObject:token forKey:@"spotifyglass.motion.token"];++ NSDictionary<NSString *, NSString *> *names = @{+ @"Abbey Road (Remastered)": @"Abbey Road", @"Rumours - 2004 Remaster": @"Rumours", @"[Deluxe]": @"[Deluxe]",+ @"Thriller 25 [Super Deluxe Edition] (Remastered)": @"Thriller 25", @"Midnights": @"Midnights",+ };+ __block BOOL namesRight = YES;+ [names enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *searched, BOOL *stop) {+ if (![SGMotionSearchName(name) isEqualToString:searched]) {+ namesRight = NO;+ NSLog(@"[harness] catalog: %@ searched as \"%@\", not \"%@\"", name, SGMotionSearchName(name), searched);+ }+ }];+ [self expect:namesRight step:@"search names" detail:namesRight ? @"editions taken off" : @"see above"];++ sg_catalogBody = catalogAlbums(@[@[@"Midnights", @"Taylor Swift", @"https://canvas.harness/standard.m3u8"],+ @[@"Midnights (3am Edition)", @"Taylor Swift", @"https://canvas.harness/3am.m3u8"]]);+ after(1, ^{ SGMotionAlbumCover(@"Taylor Swift", @"Midnights (3am Edition)", SGMotionTall, 600, ^(NSURL *file) {}); });+ after(2.5, ^{+ NSString *term = termOf(sg_catalogAsked.firstObject), *master = sg_canvasAsked.firstObject.lastPathComponent;+ [self expect:sg_catalogAsked.count == 1 && [term isEqualToString:@"Taylor Swift Midnights"] step:@"the term"+ detail:[NSString stringWithFormat:@"%lu asked, \"%@\"", (unsigned long)sg_catalogAsked.count, term]];+ [self expect:[master isEqualToString:@"3am.m3u8"] step:@"the album of the same name first" detail:master ?: @"none asked"];+ sg_catalogStatus = 429;+ SGMotionAlbumCover(@"Artist X", @"Album One", SGMotionTall, 600, ^(NSURL *file) {});+ });+ __block BOOL answered = NO;+ after(4, ^{+ [self expect:sg_catalogAsked.count == 2 step:@"a 429" detail:[NSString stringWithFormat:@"%lu asked", (unsigned long)sg_catalogAsked.count]];+ sg_catalogStatus = 200;+ SGMotionAlbumCover(@"Artist X", @"Album Two", SGMotionTall, 600, ^(NSURL *file) { answered = !file; });+ });+ after(5, ^{+ [self expect:sg_catalogAsked.count == 2 && answered step:@"paused after it"+ detail:[NSString stringWithFormat:@"%lu asked, the next album %@", (unsigned long)sg_catalogAsked.count, answered ? @"found nothing at once" : @"waits"]];+ NSLog(@"[harness] catalog checks: %lu of %lu right -- %@", (unsigned long)(self->_checks - self->_failures), (unsigned long)self->_checks,+ self->_failures ? @"FAIL" : @"PASS");+ });+}+static UIView *firstOfClass(UIView *root, NSString *name) {if ([NSStringFromClass(root.class) isEqualToString:name]) return root;for (UIView *sub in root.subviews) {@@ -1735,6 +1828,8 @@ __attribute__((constructor(101))) static void sgr_harnessDefaults(void) {}[NSURLProtocol registerClass:SGRHarnessPictureServer.class];}+ // The shared session the catalog asks with.+ if ([scenario() isEqualToString:@"catalog"]) [NSURLProtocol registerClass:SGRHarnessPictureServer.class];if ([scenario() isEqualToString:@"motion"] || [scenario() isEqualToString:@"settings"]) {[NSUserDefaults.standardUserDefaults setInteger:3 forKey:@"spotifyglass.redesign.player.background"];setenv("HARNESS_CANVAS", "https://canvas.harness/clip.mp4", 1);diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.hindex a77a9290..bb3657d7 100644--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h@@ -86,5 +86,6 @@ NSArray *SGLockScreenMotionRows(void);// Pure steps, for the harness.NSString *SGMotionNameKey(NSString *name);+NSString *SGMotionSearchName(NSString *name);NSString *SGMotionStreamIn(NSString *master, CGFloat pixels);NSString *SGMotionWholeFileIn(NSString *media);diff --git a/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m b/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.mindex 29298a59..fb9634de 100644--- a/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m+++ b/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m@@ -61,6 +61,21 @@ NSString *SGMotionNameKey(NSString *name) {return bare.length ? bare : plain(name);}+// The name as it is searched for: without what Spotify adds for an edition ("(Deluxe)", "[Remastered]",+// " - 2011 Remaster"), so a search for an edition finds the standard album too, whose cover may be the one+// that moves. A name that is all edition is searched as it is.+NSString *SGMotionSearchName(NSString *name) {+ if (![name isKindOfClass:NSString.class]) return @"";+ NSString *bare = name;+ NSRange dash = [bare rangeOfString:@" - "];+ if (dash.location != NSNotFound) bare = [bare substringToIndex:dash.location];+ bare = [bare stringByReplacingOccurrencesOfString:@"\\([^)]*\\)|\\[[^\\]]*\\]" withString:@" " options:NSRegularExpressionSearch+ range:NSMakeRange(0, bare.length)];+ bare = [bare stringByReplacingOccurrencesOfString:@"\\s+" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, bare.length)];+ bare = [bare stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];+ return bare.length ? bare : name;+}+// Apple Music may credit several artists where Spotify names the lead, so either name may hold the// other as a run of whole words.static BOOL sameArtist(NSString *a, NSString *b) {@@ -219,6 +234,12 @@ static void forgetToken(NSString *token) {#pragma mark - catalog+// After a 403 or a 429 the catalog is left alone this long: each lookup meanwhile finds nothing at once and is+// not kept, so it is asked again once the pause is over. Without it every skip sent a search to a catalog that+// had said to stop.+static const NSTimeInterval kCatalogPause = 600;+static NSDate *sg_catalogPausedUntil;+static NSString *escaped(NSString *value) {NSCharacterSet *unreserved = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"];@@ -228,6 +249,10 @@ static NSString *escaped(NSString *value) {// The array at `path` in the catalog's answer, empty when it has none; `answered` is NO when the catalog// could not be asked, so the caller does not remember the absence. A refused token is read again once.static void askCatalog(NSString *address, NSString *path, BOOL renew, void (^done)(NSArray *found, BOOL answered)) {+ if (sg_catalogPausedUntil.timeIntervalSinceNow > 0) {+ dispatch_async(dispatch_get_main_queue(), ^{ done(nil, NO); });+ return;+ }withToken(^(NSString *token) {if (!token) {done(nil, NO);@@ -248,6 +273,10 @@ static void askCatalog(NSString *address, NSString *path, BOOL renew, void (^donaskCatalog(address, path, NO, done);return;}+ if (status == 403 || status == 429) {+ sg_catalogPausedUntil = [NSDate dateWithTimeIntervalSinceNow:kCatalogPause];+ SGLog(@"motion: the catalog answered %ld, not asked for %.0f minutes", (long)status, kCatalogPause / 60);+ }done(found, status == 200);});});@@ -327,20 +356,25 @@ void SGMotionAlbumCover(NSString *artist, NSString *album, SGMotionShape shape,NSString *key = [NSString stringWithFormat:@"album\n%ld\n%@\n%@", (long)shape, artistKey, albumKey];// The remote file is what is kept: the store may drop its local copy later in the launch.lookUp(key, ^(NSURL *remote) { SGMotionFile(remote, done); }, ^(void (^answer)(id, BOOL)) {- search(@"albums", [NSString stringWithFormat:@"%@ %@", artist, album], @"editorialVideo", YES, ^(NSArray *found, BOOL answered) {+ NSString *term = [NSString stringWithFormat:@"%@ %@", artist, SGMotionSearchName(album)];+ search(@"albums", term, @"editorialVideo", YES, ^(NSArray *found, BOOL answered) {if (!answered) {answer(nil, NO);return;}// Editions share a key (Midnights and Midnights (3am Edition), clean and explicit), and only one of- // them may have the motion: the first match that has it.- NSString *video = nil;- for (id result in found) {- id attributes = dig(result, @"attributes");- if (!sameArtist(artistKey, SGMotionNameKey(dig(attributes, @"artistName")))) continue;- if (![SGMotionNameKey(dig(attributes, @"name")) isEqualToString:albumKey]) continue;- video = videoOf(dig(attributes, @"editorialVideo"), shape);- if (video) break;+ // them may have the motion: the album of the same name first, then the first edition that has it.+ NSString *video = nil, *exact = plain(album);+ for (int pass = 0; pass < 2 && !video; pass++) {+ for (id result in found) {+ id attributes = dig(result, @"attributes");+ if (!sameArtist(artistKey, SGMotionNameKey(dig(attributes, @"artistName")))) continue;+ id name = dig(attributes, @"name");+ if (pass == 0 ? ![name isKindOfClass:NSString.class] || ![plain(name) isEqualToString:exact]+ : ![SGMotionNameKey(name) isEqualToString:albumKey]) continue;+ video = videoOf(dig(attributes, @"editorialVideo"), shape);+ if (video) break;+ }}NSURL *master = video ? [NSURL URLWithString:video] : nil;SGLog(@"motion: %@ by %@ has %@", album, artist, master ? @"an animated cover" : @"no animated cover");