2022-07-11 11:06:41 +08:00
import log from './log'
2022-06-12 15:29:14 +08:00
import axios from 'axios'
2022-07-11 11:06:41 +08:00
const token =
'Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IldlYlBsYXlLaWQifQ.eyJpc3MiOiJBTVBXZWJQbGF5IiwiaWF0IjoxNjQ2NjU1MDgwLCJleHAiOjE2NjIyMDcwODB9.pyOrt2FmP0cHkzYtO8KiEzQL2t1qpRszzxIYbLH7faXSddo6PQei771Ja3aGwGVU4hD99lZAw7bwat60iBcGiQ'
2022-06-12 15:29:14 +08:00
export const getCoverVideo = async ( {
name ,
2022-07-11 11:06:41 +08:00
artist ,
2022-06-12 15:29:14 +08:00
} : {
name : string
2022-07-11 11:06:41 +08:00
artist : string
2022-06-12 15:29:14 +08:00
} ) : Promise < string | undefined > = > {
2022-07-11 11:06:41 +08:00
const keyword = ` ${ artist } ${ name } `
log . debug ( ` [appleMusic] getCoverVideo: ${ keyword } ` )
const searchResult = await axios ( {
method : 'GET' ,
url : 'https://amp-api.music.apple.com/v1/catalog/us/search' ,
params : {
term : keyword ,
types : 'albums' ,
'fields[albums]' : 'artistName,name,editorialVideo' ,
platform : 'web' ,
limit : '1' ,
} ,
headers : {
Authority : 'amp-api.music.apple.com' ,
Accept : '*/*' ,
Authorization : token ,
Referer : 'http://localhost:9000/' ,
'Sec-Fetch-Dest' : 'empty' ,
'Sec-Fetch-Mode' : 'cors' ,
'Sec-Fetch-Site' : 'cross-site' ,
'User-Agent' :
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cider/1.5.1 Chrome/100.0.4896.160 Electron/18.3.3 Safari/537.36' ,
'Accept-Encoding' : 'gzip' ,
} ,
} ) . catch ( e = > {
log . debug ( '[appleMusic] Search album error' , e )
2022-06-12 15:29:14 +08:00
} )
2022-07-11 11:06:41 +08:00
const album = searchResult ? . data ? . results ? . albums ? . data ? . [ 0 ]
if ( ! album ) {
log . debug ( '[appleMusic] No album found on apple music' )
2022-06-12 15:29:14 +08:00
return
}
2022-07-11 11:06:41 +08:00
log . debug (
` [appleMusic] Got ${ album ? . id } : ${ album ? . attributes ? . name } by ${ album ? . attributes ? . artistName } `
)
2022-06-12 15:29:14 +08:00
2022-07-11 11:06:41 +08:00
const url = album ? . attributes ? . editorialVideo ? . motionSquareVideo1x1 ? . video
if ( ! url ) {
log . debug ( '[appleMusic] Album does not have video cover' )
}
return url
2022-06-12 15:29:14 +08:00
}