diff options
| author | Pinapelz <yukais@pinapelz.com> | 2023-11-15 23:37:59 -0800 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2023-11-15 23:37:59 -0800 |
| commit | 6860c5529bab2e35864e1c5803974bd0ffe43222 (patch) | |
| tree | 005adf864aafa6a255dad92ea3fd90e206ca3988 | |
| parent | 27b28d6507712f00ced4abbe7e026a12d47649f7 (diff) | |
Initial api route for spotify-lyric-api
| -rw-r--r-- | src/app/api/route.ts | 5 | ||||
| -rw-r--r-- | src/app/api/spotify-lrc/route.ts | 53 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/app/api/route.ts b/src/app/api/route.ts new file mode 100644 index 0000000..0e50858 --- /dev/null +++ b/src/app/api/route.ts @@ -0,0 +1,5 @@ +import { NextResponse } from "next/server"; + +export async function GET(){ + return NextResponse.json({ message: 'Invalid Usage, See About page for usage' }) +}
\ No newline at end of file diff --git a/src/app/api/spotify-lrc/route.ts b/src/app/api/spotify-lrc/route.ts new file mode 100644 index 0000000..c332e88 --- /dev/null +++ b/src/app/api/spotify-lrc/route.ts @@ -0,0 +1,53 @@ +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 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 }) +} |
