From 84007b07fb57913b64f95867cd005805e73be71b Mon Sep 17 00:00:00 2001 From: zonemeen Date: Wed, 3 Jan 2024 16:38:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20web=E6=96=B9=E5=BC=8F=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=AD=8C=E8=AF=8D=E8=87=B3=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/download.ts | 4 ++-- src/index.ts | 4 ++-- src/qrcode/index.ts | 22 ++++++++++++++++------ template/index.html | 14 +++++++++----- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/download.ts b/src/download.ts index 096f4e3ca..f58317dc4 100644 --- a/src/download.ts +++ b/src/download.ts @@ -5,7 +5,7 @@ import { red, green } from 'colorette' import { pipeline } from 'node:stream/promises' import { join, basename } from 'node:path' import { existsSync, mkdirSync, createWriteStream, unlinkSync } from 'node:fs' -import lyric from './services/lyric' +import lyricDownload from './services/lyric' import type { SongInfo } from './types' const barList: cliProgress.SingleBar[] = [] @@ -53,7 +53,7 @@ const downloadSong = (song: SongInfo, index: number) => { if (!existsSync(targetDir)) mkdirSync(targetDir) if (withLyric) { - await lyric[service](lrcPath, lyricDownloadUrl).catch(() => { + await lyricDownload[service](lrcPath, lyricDownloadUrl).catch(() => { createWriteStream(lrcPath).write('[00:00.00]无歌词') }) } diff --git a/src/index.ts b/src/index.ts index be3384fb4..7cbb6ea1b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,10 +8,10 @@ import type { SongInfo } from './types' !(async () => { const { - options: { qrcode, port, open, path }, + options: { qrcode, port, open, path, lyric }, } = command if (qrcode) { - return await qrcodeGenerator({ port, open, path }) + return await qrcodeGenerator({ port, open, path, lyric }) } const result = await searchMusic(command) const { songs = [] } = await choose(result) diff --git a/src/qrcode/index.ts b/src/qrcode/index.ts index b3f6fc4ed..e9cfab0d5 100644 --- a/src/qrcode/index.ts +++ b/src/qrcode/index.ts @@ -8,13 +8,15 @@ import qrcode from 'qrcode-terminal' import open from 'open' import express, { NextFunction, Request, Response } from 'express' import search from '../services/search' -import lyric from '../services/lyric' +import lyricDownload from '../services/lyric' import { getNetworkAddress } from '../utils' import type { ServiceType } from '../types' interface DownloadRequestType { + service: ServiceType url: string songName: string + lyricUrl: string } interface SearchRequestType { @@ -40,10 +42,12 @@ export default async ({ port, open: isOpen, path, + lyric: withLyric, }: { - port: string - open: boolean - path: string + port?: string + open?: boolean + path?: string + lyric?: boolean }) => { const app = express() @@ -82,7 +86,7 @@ export default async ({ pageSize, }) const lyricList = (await Promise.allSettled( - searchSongs.map(({ lyricUrl }) => lyric[service](null, lyricUrl!)) + searchSongs.map(({ lyricUrl }) => lyricDownload[service](null, lyricUrl!)) )) as { value: string | undefined }[] searchSongs.forEach((song, index) => { song.lrc = lyricList[index].value ?? '[00:00.00]无歌词' @@ -102,11 +106,17 @@ export default async ({ >, res: Response ) => { - const { url, songName } = req.query + const { service, url, songName, lyricUrl } = req.query if (path) { if (!existsSync(path)) mkdirSync(path) const songPath = join(path, songName) await pipeline(got.stream(url), createWriteStream(songPath)) + if (withLyric) { + const lrcPath = join(path, `${songName.split('.')[0]}.lrc`) + await lyricDownload[service](lrcPath, lyricUrl).catch(() => { + createWriteStream(lrcPath).write('[00:00.00]无歌词') + }) + } res.send({ download: 'success' }) } else { got.stream(url).pipe(res) diff --git a/template/index.html b/template/index.html index 6198cf7d5..c4589a760 100644 --- a/template/index.html +++ b/template/index.html @@ -171,13 +171,17 @@ this.player.list.switch(index) this.player.play() }, - async onDownload({ id, url, songName }) { + async onDownload({ id, url, songName, lyricUrl }) { try { + const { service } = this.params this.downloadLoadingList.push(id) - const response = await fetch(`/download?url=${url}&songName=${songName}`, { - method: 'GET', - signal: this.controller.signal, - }) + const response = await fetch( + `/download?service=${service}&url=${url}&songName=${songName}&lyricUrl=${lyricUrl}`, + { + method: 'GET', + signal: this.controller.signal, + } + ) const blob = await response.blob() if (blob.type !== 'application/json') { const link = document.createElement('a')