@@ -9,6 +9,7 @@ import fs from 'fs'
99import youtube from '@/desktop/main/youtube'
1010import { CacheAPIs } from '@/shared/CacheAPIs'
1111import { FetchTracksResponse } from '@/shared/api/Track'
12+ import store from '@/desktop/main/store'
1213
1314const getAudioFromCache = async ( id : number ) => {
1415 // get from cache
@@ -76,47 +77,51 @@ const getAudioFromYouTube = async (id: number) => {
7677 const track = fetchTrackResult ?. songs ?. [ 0 ]
7778 if ( ! track ) return
7879
79- const data = await youtube . matchTrack ( track . ar [ 0 ] . name , track . name )
80- if ( ! data ) return
81- return {
82- data : [
83- {
84- source : 'youtube' ,
85- id,
86- url : data . url ,
87- br : data . bitRate ,
88- size : 0 ,
89- md5 : '' ,
90- code : 200 ,
91- expi : 0 ,
92- type : 'opus' ,
93- gain : 0 ,
94- fee : 8 ,
95- uf : null ,
96- payed : 0 ,
97- flag : 4 ,
98- canExtend : false ,
99- freeTrialInfo : null ,
100- level : 'standard' ,
101- encodeType : 'opus' ,
102- freeTrialPrivilege : {
103- resConsumable : false ,
104- userConsumable : false ,
105- listenType : null ,
80+ try {
81+ const data = await youtube . matchTrack ( track . ar [ 0 ] . name , track . name )
82+ if ( ! data ) return
83+ return {
84+ data : [
85+ {
86+ source : 'youtube' ,
87+ id,
88+ url : data . url ,
89+ br : data . bitRate ,
90+ size : 0 ,
91+ md5 : '' ,
92+ code : 200 ,
93+ expi : 0 ,
94+ type : 'opus' ,
95+ gain : 0 ,
96+ fee : 8 ,
97+ uf : null ,
98+ payed : 0 ,
99+ flag : 4 ,
100+ canExtend : false ,
101+ freeTrialInfo : null ,
102+ level : 'standard' ,
103+ encodeType : 'opus' ,
104+ freeTrialPrivilege : {
105+ resConsumable : false ,
106+ userConsumable : false ,
107+ listenType : null ,
108+ } ,
109+ freeTimeTrialPrivilege : {
110+ resConsumable : false ,
111+ userConsumable : false ,
112+ type : 0 ,
113+ remainTime : 0 ,
114+ } ,
115+ urlSource : 0 ,
116+ r3play : {
117+ youtube : data ,
118+ } ,
106119 } ,
107- freeTimeTrialPrivilege : {
108- resConsumable : false ,
109- userConsumable : false ,
110- type : 0 ,
111- remainTime : 0 ,
112- } ,
113- urlSource : 0 ,
114- r3play : {
115- youtube : data ,
116- } ,
117- } ,
118- ] ,
119- code : 200 ,
120+ ] ,
121+ code : 200 ,
122+ }
123+ } catch ( e ) {
124+ log . error ( 'getAudioFromYouTube error' , id , e )
120125 }
121126}
122127
@@ -154,9 +159,11 @@ async function audio(fastify: FastifyInstance) {
154159 return
155160 }
156161
157- const fromYoutube = getAudioFromYouTube ( id )
158- if ( fromYoutube ) {
159- return fromYoutube
162+ if ( store . get ( 'settings.enableFindTrackOnYouTube' ) ) {
163+ const fromYoutube = getAudioFromYouTube ( id )
164+ if ( fromYoutube ) {
165+ return fromYoutube
166+ }
160167 }
161168
162169 // 是试听歌曲就把url删掉
@@ -181,11 +188,14 @@ async function audio(fastify: FastifyInstance) {
181188 fastify . post (
182189 `/${ appName . toLowerCase ( ) } /audio/:id` ,
183190 async (
184- req : FastifyRequest < { Params : { id : string } ; Querystring : { url : string } } > ,
191+ req : FastifyRequest < {
192+ Params : { id : string }
193+ Querystring : { url : string ; bitrate : number }
194+ } > ,
185195 reply
186196 ) => {
187197 const id = Number ( req . params . id )
188- const { url } = req . query
198+ const { url, bitrate } = req . query
189199 if ( isNaN ( id ) ) {
190200 return reply . status ( 400 ) . send ( { error : 'Invalid param id' } )
191201 }
@@ -200,7 +210,7 @@ async function audio(fastify: FastifyInstance) {
200210 }
201211
202212 try {
203- await cache . setAudio ( await data . toBuffer ( ) , { id, url } )
213+ await cache . setAudio ( await data . toBuffer ( ) , { id, url, bitrate } )
204214 reply . status ( 200 ) . send ( 'Audio cached!' )
205215 } catch ( error ) {
206216 reply . status ( 500 ) . send ( { error } )
0 commit comments