Skip to content

Commit

Permalink
feat: web方式支持下载歌词至服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
zonemeen committed Jan 3, 2024
1 parent 7791937 commit 84007b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down Expand Up @@ -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]无歌词')
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<SongInfo>command)
const { songs = [] } = await choose(<SongInfo>result)
Expand Down
22 changes: 16 additions & 6 deletions src/qrcode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

Expand Down Expand Up @@ -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!))

Check warning on line 89 in src/qrcode/index.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
)) as { value: string | undefined }[]
searchSongs.forEach((song, index) => {
song.lrc = lyricList[index].value ?? '[00:00.00]无歌词'
Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 84007b0

Please sign in to comment.