* 歌曲下载策略
| 50 | * 歌曲下载策略 |
| 51 | */ |
| 52 | class SongDownloadStrategy implements DownloadStrategy { |
| 53 | private settingStore = useSettingStore(); |
| 54 | private dataStore = useDataStore(); |
| 55 | |
| 56 | // prepare 阶段准备的状态 |
| 57 | private _downloadUrl = ""; |
| 58 | private fileType = "mp3"; |
| 59 | private lyricResult: LyricResult | null = null; |
| 60 | private basicLyric = ""; |
| 61 | private ttmlLyric = ""; |
| 62 | private yrcLyric = ""; |
| 63 | private albumArtists: string[] = []; |
| 64 | |
| 65 | constructor( |
| 66 | public readonly song: SongType, |
| 67 | private quality: SongLevelType, |
| 68 | ) {} |
| 69 | |
| 70 | get id() { |
| 71 | return this.song.id; |
| 72 | } |
| 73 | get name() { |
| 74 | return this.song.name; |
| 75 | } |
| 76 | get downloadUrl() { |
| 77 | return this._downloadUrl; |
| 78 | } |
| 79 | |
| 80 | async prepare(): Promise<void> { |
| 81 | // 解析下载链接 |
| 82 | const { url, type } = await this.resolveUrl(); |
| 83 | this._downloadUrl = url; |
| 84 | this.fileType = type; |
| 85 | |
| 86 | // 获取歌词 |
| 87 | if (this.shouldDownloadLyrics()) { |
| 88 | this.lyricResult = (await songLyric(this.song.id)) as LyricResult; |
| 89 | |
| 90 | const options: LyricProcessorOptions = { |
| 91 | downloadLyricToTraditional: this.settingStore.downloadLyricToTraditional, |
| 92 | downloadLyricTranslation: this.settingStore.downloadLyricTranslation, |
| 93 | downloadLyricRomaji: this.settingStore.downloadLyricRomaji, |
| 94 | downloadLyricEncoding: this.settingStore.downloadLyricEncoding, |
| 95 | }; |
| 96 | |
| 97 | // 处理基础歌词 |
| 98 | this.basicLyric = await LyricProcessor.processBasic(this.lyricResult, options); |
| 99 | |
| 100 | // 处理逐字歌词 (后续使用) |
| 101 | const { downloadMakeYrc, downloadSaveAsAss } = this.settingStore; |
| 102 | if (downloadMakeYrc || downloadSaveAsAss) { |
| 103 | let ttmlLyric = ""; |
| 104 | const yrcLyric = this.lyricResult?.yrc?.lyric || ""; |
| 105 | let qmResultData; |
| 106 | |
| 107 | try { |
| 108 | const ttmlRes = await songLyricTTML(this.song.id); |
| 109 | if (typeof ttmlRes === "string") ttmlLyric = ttmlRes; |
nothing calls this directly
no outgoing calls
no test coverage detected