Skip to content

Commit 34f8598

Browse files
committed
Idk anymore
1 parent d035df2 commit 34f8598

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/lib/api/api.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import { shareValues } from "$lib/util/array";
12
import { COVERARCH, type CAARelease, type CAAReleaseImage } from "./coveArtArchive";
2-
import { type MBArtistSearchResult , type MBArtistSearch, MUSICBRAINZ, type MBArtist, type MBRecordingSearchResult, type MBRecordingSearch, type MBRelease } from "./musicBrainz";
3+
import { type MBArtistSearchResult , type MBArtistSearch, MUSICBRAINZ, type MBArtist, type MBRecordingSearchResult, type MBRecordingSearch, type MBRelease, type MBArtistCredit } from "./musicBrainz";
34

45
export type APIArtist = {
56
name: string,
67
id: string,
7-
logo?: string
8+
logo?: string,
89
}
910

1011
export type APISong = {
1112
title: string,
1213
id: string,
14+
artistIds: string[],
1315
coverArt?: string
1416
}
1517

@@ -173,12 +175,12 @@ export class SongAPI {
173175
}
174176

175177
let url = `${MUSICBRAINZ}/recording/?query=${encodeURI(searchString)}&limit=5`;
176-
178+
console.log(url)
177179
const search: MBRecordingSearch | undefined = await this.get(url);
178180

179181
if(!search || !search.recordings)
180182
return;
181-
183+
182184
return search.recordings[0];
183185
}
184186

@@ -190,13 +192,13 @@ export class SongAPI {
190192
}
191193

192194
private async querySongCoverArt(song: MBRecordingSearchResult): Promise<string | undefined> {
193-
let release: MBRelease | null = null;
194-
195-
for(const rel of song.releases) {
196-
if(rel["artist-credit-id"] == song["artist-credit-id"]) {
195+
let release: MBRelease | undefined = song.releases[0];
196+
197+
/*for(const rel of song.releases) {
198+
if(rel.status == "Official") {
197199
release = rel;
198200
}
199-
}
201+
}*/
200202

201203
if(!release)
202204
return;
@@ -217,6 +219,7 @@ export class SongAPI {
217219
return {
218220
title: searchRes.title,
219221
id: searchRes.id,
222+
artistIds: this.getArtistIdsFromCredits(searchRes["artist-credit"]),
220223
coverArt: await this.querySongCoverArt(searchRes)
221224
};
222225
}
@@ -245,6 +248,9 @@ export class SongAPI {
245248
// TODO: Search in other sources. There are barely any artist images in MusicBrainz
246249
// this function is 'async' to prepare it for making requests in the future
247250

251+
if(!artist.relations)
252+
return;
253+
248254
for(const rel of artist.relations) {
249255
if(rel.type == "image") {
250256
return rel.url.resource;
@@ -262,4 +268,25 @@ export class SongAPI {
262268
// the logo logic is highly flawed (URL contains the image page instead of the actual file, barely any artists have an image). Disabled for now
263269
};
264270
}
271+
272+
private getArtistIdsFromCredits(credits: MBArtistCredit[] | undefined): string[] {
273+
const res: string[] = [];
274+
275+
if(!credits) {
276+
return [];
277+
}
278+
279+
for(const artist of credits) {
280+
res.push(artist.artist.id);
281+
}
282+
283+
return res;
284+
}
285+
286+
private shareCredits(a: MBArtistCredit[], b: MBArtistCredit[]) {
287+
const aIds = this.getArtistIdsFromCredits(a);
288+
const bIds = this.getArtistIdsFromCredits(b);
289+
290+
return shareValues(a, b);
291+
}
265292
}

src/lib/api/musicBrainz.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export type MBArtistSearchResult = {
1515
export type MBRelease = {
1616
id: string,
1717
status: "Official" | "Unofficial",
18-
"artist-credit-id": string
18+
"artist-credit"?: MBArtistCredit[],
1919
}
2020

2121
export type MBRecordingSearchResult = {
2222
id: string,
2323
title: string,
2424
releases: MBRelease[],
25-
"artist-credit-id": string
25+
"artist-credit"?: MBArtistCredit[],
2626
}
2727

2828
export type MBSearch = {
@@ -42,7 +42,12 @@ export type MBRecordingSearch = MBSearch & {
4242
export type MBArtist = {
4343
id: string,
4444
name: string,
45-
relations: MBRelation[]
45+
relations?: MBRelation[]
46+
}
47+
48+
export type MBArtistCredit = {
49+
name: string,
50+
artist: MBArtist
4651
}
4752

4853
export type MBRelation = {

src/lib/util/array.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function shareValues(a: any[], b: any[]): boolean {
2+
for(const value of a) {
3+
if(b.includes(value)) {
4+
return true;
5+
}
6+
}
7+
8+
return false;
9+
}

0 commit comments

Comments
 (0)