aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/api/spotify-lrc/route.ts
blob: 793f39368277afd3dceb4f93a97da73861a22d38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { NextRequest, NextResponse } from 'next/server'

const spotifyLyricsAPIURL = 'https://spotify-lyric-api-984e7b4face0.herokuapp.com/'

interface Line{
    timeTag: string;
    words: string;
}

interface SpotifyLyricAPIResponse {
    error: boolean;
    syncType: string;
    usage?: string
    lines?: Line[];
}

function extractSpotifyId(url: string | null) {
    if (!url) {
        return url;
    }
    const spotifyUrlPattern = /https:\/\/open\.spotify\.com\/track\/([a-zA-Z0-9]+)/;
    const match = spotifyUrlPattern.exec(url);
    if (match && match[1]) {
        return match[1];
    } else {
        return url;
}
}

function convertLinesToLrc(lines: Line[] | undefined) {
    if (!lines) {
        return '';
    }
    let lrc = '';
    for (const line of lines) {
        lrc += `[${line['timeTag']}]${line['words']}\n`;
        console.log(lrc);
    }
    return lrc;
}

export const runtime = 'edge';
export async function GET(request: NextRequest) {
 const searchParams = request.nextUrl.searchParams
 const q = searchParams.get('q');
 const url = `${spotifyLyricsAPIURL}?trackid=${extractSpotifyId(q)}&format=lrc`
const response = await fetch(url);
if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
}
const data: SpotifyLyricAPIResponse = await response.json();
 const lrcString = convertLinesToLrc(data.lines);
 return NextResponse.json({ message: lrcString })
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage