Skip to content

Commit 0c80ce0

Browse files
committed
add abstraction layer for lyrics provider
Closes: gh-18
1 parent 845298c commit 0c80ce0

4 files changed

Lines changed: 55 additions & 8 deletions

File tree

src/extension.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { SpotifyPreAuthState } from './SpotifyPreAuthState';
88
import { SpotifyAuthState } from './SpotifyAuthState';
99
import { clearTimeout } from 'node:timers';
1010
import { SpotifyCurrentPlayingState } from './SpotifyCurrentPlayingState';
11-
import { LRCLibSearchResponse } from './api/response/LRCLibGetResponse';
12-
import { LRCLibApi } from './api/LRCLibApi';
1311
import TreeMap from 'ts-treemap';
1412
import { LyricsEntry } from './LyricsEntry';
1513
import { generateTextColor, getAccentColorFromUrl } from './ColorUtil';
1614
import path from 'node:path';
1715
import LRUCache from 'lru-cache';
16+
import { LRCLibLyricsProvider } from './provider/LRCLibLyricsProvider';
17+
import { LyricsProvider } from './provider/LyricsProvider';
1818

1919
let panel: WebviewPanel | undefined;
2020

@@ -26,6 +26,8 @@ let tracksCache: LRUCache<string, SpotifyCurrentPlayingState>;
2626
let server: http.Server | null;
2727
let pollingTimeout: NodeJS.Timeout | null;
2828

29+
const provider: LyricsProvider = new LRCLibLyricsProvider();
30+
2931
export async function activate(context: vscode.ExtensionContext) {
3032
context.subscriptions.push(
3133
vscode.commands.registerCommand('spotilyrics.lyrics', async () => {
@@ -352,16 +354,16 @@ async function updateLyrics() {
352354
currentPlayingState.authors !== artists ||
353355
currentPlayingState.name !== trackName
354356
) {
355-
const getLyricsResponse: LRCLibSearchResponse = await LRCLibApi.get(
357+
const lyricsResult = await provider.getLyrics(
356358
trackName,
357359
artists,
358360
albumName,
359361
durationInS
360362
);
361-
if (!getLyricsResponse.statusCode && !getLyricsResponse.instrumental) {
363+
if (lyricsResult && !lyricsResult.instrumental) {
362364
const currentlyPlayingPoll = new SpotifyCurrentPlayingState(trackName, artists);
363-
if (getLyricsResponse.plainLyrics) {
364-
const plainLyricsStrs: string[] = getLyricsResponse.plainLyrics
365+
if (lyricsResult.plainLyrics) {
366+
const plainLyricsStrs: string[] = lyricsResult.plainLyrics
365367
.split(/\n/)
366368
.map((s) => s.trim())
367369
.filter((s) => s !== '')
@@ -370,9 +372,9 @@ async function updateLyrics() {
370372
currentlyPlayingPoll.plainLyricsStrs = plainLyricsStrs;
371373
}
372374
// load synchronized lyrics in treemap
373-
if (getLyricsResponse.syncedLyrics) {
375+
if (lyricsResult.syncedLyrics) {
374376
const synchronizedLyricsMap = new TreeMap<number, LyricsEntry>();
375-
const synchronizedLyricsStrs: string[] = getLyricsResponse.syncedLyrics
377+
const synchronizedLyricsStrs: string[] = lyricsResult.syncedLyrics
376378
.split(/(?=\[\d{2}:\d{2}\.\d{2}\])/)
377379
.filter((s) => s.trim() !== '');
378380
let id: number = 0;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { LyricsProvider } from './LyricsProvider';
2+
import { LyricsResult } from './LyricsResult';
3+
import { LRCLibApi } from '../api/LRCLibApi';
4+
5+
export class LRCLibLyricsProvider implements LyricsProvider {
6+
async getLyrics(
7+
track_name?: string,
8+
artist_name?: string,
9+
album_name?: string,
10+
duration?: number
11+
): Promise<LyricsResult | null> {
12+
const result = await LRCLibApi.get(track_name, artist_name, album_name, duration);
13+
if (result.statusCode) {
14+
return null;
15+
}
16+
return {
17+
name: result.name,
18+
trackName: result.trackName,
19+
artistName: result.artistName,
20+
albumName: result.albumName,
21+
plainLyrics: result.plainLyrics,
22+
syncedLyrics: result.syncedLyrics,
23+
instrumental: result.instrumental,
24+
};
25+
}
26+
}

src/provider/LyricsProvider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LyricsResult } from './LyricsResult';
2+
3+
export interface LyricsProvider {
4+
getLyrics(
5+
track_name?: string,
6+
artist_name?: string,
7+
album_name?: string,
8+
duration?: number
9+
): Promise<LyricsResult | null>;
10+
}

src/provider/LyricsResult.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface LyricsResult {
2+
name: string;
3+
trackName: string;
4+
artistName: string;
5+
albumName: string;
6+
plainLyrics?: string;
7+
syncedLyrics?: string;
8+
instrumental?: boolean;
9+
}

0 commit comments

Comments
 (0)