Skip to content

Commit d70a5ef

Browse files
committed
chore: Merge branch 'snapshot-fallbacks'
2 parents 05ad3fe + 11e62bf commit d70a5ef

18 files changed

+568
-131
lines changed

providers/Deezer/mod.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { availableRegions } from './regions.ts';
2-
import { type CacheEntry, MetadataApiProvider, type ProviderOptions, ReleaseApiLookup } from '@/providers/base.ts';
2+
import {
3+
type ApiQueryOptions,
4+
type CacheEntry,
5+
MetadataApiProvider,
6+
type ProviderOptions,
7+
ReleaseApiLookup,
8+
} from '@/providers/base.ts';
39
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
410
import { capitalizeReleaseType } from '@/harmonizer/release_types.ts';
511
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
@@ -70,9 +76,9 @@ export default class DeezerProvider extends MetadataApiProvider {
7076
return ['free streaming'];
7177
}
7278

73-
async query<Data>(apiUrl: URL, maxTimestamp?: number): Promise<CacheEntry<Data>> {
79+
async query<Data>(apiUrl: URL, options: ApiQueryOptions): Promise<CacheEntry<Data>> {
7480
const cacheEntry = await this.fetchJSON<Data>(apiUrl, {
75-
policy: { maxTimestamp },
81+
policy: { maxTimestamp: options.snapshotMaxTimestamp },
7682
});
7783
const { error } = cacheEntry.content as { error?: ApiError };
7884

@@ -103,10 +109,9 @@ export class DeezerReleaseLookup extends ReleaseApiLookup<DeezerProvider, Releas
103109

104110
protected async getRawRelease(): Promise<Release> {
105111
const apiUrl = this.constructReleaseApiUrl();
106-
const { content: release, timestamp } = await this.provider.query<Release>(
107-
apiUrl,
108-
this.options.snapshotMaxTimestamp,
109-
);
112+
const { content: release, timestamp } = await this.provider.query<Release>(apiUrl, {
113+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
114+
});
110115
this.updateCacheTime(timestamp);
111116

112117
return release;
@@ -119,7 +124,9 @@ export class DeezerReleaseLookup extends ReleaseApiLookup<DeezerProvider, Releas
119124
while (nextPageQuery) {
120125
const { content, timestamp }: CacheEntry<Result<TracklistItem>> = await this.provider.query(
121126
new URL(nextPageQuery, this.provider.apiBaseUrl),
122-
this.options.snapshotMaxTimestamp,
127+
{
128+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
129+
},
123130
);
124131
tracklist.push(...content.data);
125132
nextPageQuery = content.next;
@@ -132,7 +139,9 @@ export class DeezerReleaseLookup extends ReleaseApiLookup<DeezerProvider, Releas
132139
protected async getRawTrackById(trackId: string): Promise<Track> {
133140
const { content: track, timestamp } = await this.provider.query<Track>(
134141
new URL(`track/${trackId}`, this.provider.apiBaseUrl),
135-
this.options.snapshotMaxTimestamp,
142+
{
143+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
144+
},
136145
);
137146
this.updateCacheTime(timestamp);
138147
return track;

providers/MusicBrainz/mod.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import type {
77
MediumFormat,
88
ReleaseGroupType,
99
} from '@/harmonizer/types.ts';
10-
import { CacheEntry, MetadataApiProvider, ProviderOptions, ReleaseApiLookup } from '@/providers/base.ts';
10+
import {
11+
type ApiQueryOptions,
12+
type CacheEntry,
13+
MetadataApiProvider,
14+
type ProviderOptions,
15+
ReleaseApiLookup,
16+
} from '@/providers/base.ts';
1117
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
1218
import { parseHyphenatedDate } from '@/utils/date.ts';
1319
import { ResponseError } from '@/utils/errors.ts';
@@ -62,9 +68,9 @@ export default class MusicBrainzProvider extends MetadataApiProvider {
6268
return join('https://musicbrainz.org', entity.type, entity.id);
6369
}
6470

65-
async query<Data>(apiUrl: URL, maxTimestamp?: number): Promise<CacheEntry<Data>> {
71+
async query<Data>(apiUrl: URL, options: ApiQueryOptions): Promise<CacheEntry<Data>> {
6672
const cacheEntry = await this.fetchJSON<Data>(apiUrl, {
67-
policy: { maxTimestamp },
73+
policy: { maxTimestamp: options.snapshotMaxTimestamp },
6874
requestInit: {
6975
headers: {
7076
'Accept': 'application/json',
@@ -100,10 +106,9 @@ export class MusicBrainzReleaseLookup extends ReleaseApiLookup<MusicBrainzProvid
100106
async getRawRelease(): Promise<RawRelease> {
101107
if (this.lookup.method === 'gtin') {
102108
const apiUrl = this.constructReleaseApiUrl();
103-
const { content, timestamp } = await this.provider.query<ReleaseSearchResults>(
104-
apiUrl,
105-
this.options.snapshotMaxTimestamp,
106-
);
109+
const { content, timestamp } = await this.provider.query<ReleaseSearchResults>(apiUrl, {
110+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
111+
});
107112
this.updateCacheTime(timestamp);
108113

109114
const { releases } = content;
@@ -121,10 +126,9 @@ export class MusicBrainzReleaseLookup extends ReleaseApiLookup<MusicBrainzProvid
121126
}
122127
}
123128

124-
const { content: release, timestamp } = await this.provider.query<RawRelease>(
125-
this.constructReleaseApiUrl(),
126-
this.options.snapshotMaxTimestamp,
127-
);
129+
const { content: release, timestamp } = await this.provider.query<RawRelease>(this.constructReleaseApiUrl(), {
130+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
131+
});
128132
this.updateCacheTime(timestamp);
129133

130134
return release;

providers/Spotify/mod.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { capitalizeReleaseType } from '@/harmonizer/release_types.ts';
2-
import { ApiAccessToken, type CacheEntry, MetadataApiProvider, ReleaseApiLookup } from '@/providers/base.ts';
2+
import {
3+
type ApiAccessToken,
4+
type ApiQueryOptions,
5+
type CacheEntry,
6+
MetadataApiProvider,
7+
ReleaseApiLookup,
8+
} from '@/providers/base.ts';
39
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
410
import { getFromEnv } from '@/utils/config.ts';
511
import { formatCopyrightSymbols } from '@/utils/copyright.ts';
@@ -76,12 +82,12 @@ export default class SpotifyProvider extends MetadataApiProvider {
7682
return ['free streaming'];
7783
}
7884

79-
async query<Data>(apiUrl: URL, maxTimestamp?: number): Promise<CacheEntry<Data>> {
85+
async query<Data>(apiUrl: URL, options: ApiQueryOptions): Promise<CacheEntry<Data>> {
8086
try {
8187
await this.requestDelay;
8288
const accessToken = await this.cachedAccessToken(this.requestAccessToken);
8389
const cacheEntry = await this.fetchJSON<Data>(apiUrl, {
84-
policy: { maxTimestamp },
90+
policy: { maxTimestamp: options.snapshotMaxTimestamp },
8591
requestInit: {
8692
headers: {
8793
'Authorization': `Bearer ${accessToken}`,
@@ -100,7 +106,7 @@ export default class SpotifyProvider extends MetadataApiProvider {
100106
this.handleRateLimit(response);
101107
// Retry API query when we encounter a 429 rate limit error.
102108
if (response.status === 429) {
103-
return this.query(apiUrl, maxTimestamp);
109+
return this.query(apiUrl, options);
104110
}
105111
try {
106112
// Clone the response so the body of the original response can be
@@ -149,7 +155,7 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
149155
const query = new URLSearchParams();
150156

151157
if (method === 'gtin') {
152-
lookupUrl = new URL(`search`, this.provider.apiBaseUrl);
158+
lookupUrl = new URL('search', this.provider.apiBaseUrl);
153159
query.set('type', 'album');
154160
query.set('q', `upc:${value}`);
155161
if (region) {
@@ -178,10 +184,9 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
178184
this.lookup.value = albumId;
179185
}
180186

181-
const cacheEntry = await this.provider.query<Album>(
182-
this.constructReleaseApiUrl(),
183-
this.options.snapshotMaxTimestamp,
184-
);
187+
const cacheEntry = await this.provider.query<Album>(this.constructReleaseApiUrl(), {
188+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
189+
});
185190
const release = cacheEntry.content;
186191

187192
this.updateCacheTime(cacheEntry.timestamp);
@@ -198,10 +203,9 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
198203
this.lookup.region = region;
199204
for (const gtin of gtins) {
200205
this.lookup.value = gtin;
201-
const cacheEntry = await this.provider.query<SearchResult>(
202-
this.constructReleaseApiUrl(),
203-
this.options.snapshotMaxTimestamp,
204-
);
206+
const cacheEntry = await this.provider.query<SearchResult>(this.constructReleaseApiUrl(), {
207+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
208+
});
205209
this.updateCacheTime(cacheEntry.timestamp);
206210
const releases = cacheEntry.content?.albums?.items;
207211
if (releases?.length) {
@@ -223,10 +227,9 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
223227
// tracks with separate requests if needed.
224228
let nextUrl = rawRelease.tracks.next;
225229
while (nextUrl && allTracks.length < rawRelease.tracks.total) {
226-
const cacheEntry = await this.provider.query<ResultList<SimplifiedTrack>>(
227-
new URL(nextUrl),
228-
this.options.snapshotMaxTimestamp,
229-
);
230+
const cacheEntry = await this.provider.query<ResultList<SimplifiedTrack>>(new URL(nextUrl), {
231+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
232+
});
230233
this.updateCacheTime(cacheEntry.timestamp);
231234
allTracks.push(...cacheEntry.content.items);
232235
nextUrl = cacheEntry.content.next;
@@ -256,11 +259,9 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
256259
const apiUrl = new URL('tracks', this.provider.apiBaseUrl);
257260
for (let index = 0; index < trackIds.length; index += maxResults) {
258261
apiUrl.searchParams.set('ids', trackIds.slice(index, index + maxResults).join(','));
259-
apiUrl.search = apiUrl.searchParams.toString();
260-
const cacheEntry = await this.provider.query<TrackList>(
261-
apiUrl,
262-
this.options.snapshotMaxTimestamp,
263-
);
262+
const cacheEntry = await this.provider.query<TrackList>(apiUrl, {
263+
snapshotMaxTimestamp: this.options.snapshotMaxTimestamp,
264+
});
264265
this.updateCacheTime(cacheEntry.timestamp);
265266
allTracks.push(...cacheEntry.content.tracks);
266267
}

0 commit comments

Comments
 (0)