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-12 22:42:50 +08:00
import { AppleMusicAlbum , AppleMusicArtist } from '@/shared/AppleMusic'
2022-06-12 15:29:14 +08:00
2022-07-12 22:42:50 +08:00
const headers = {
Authority : 'amp-api.music.apple.com' ,
Accept : '*/*' ,
Authorization :
2022-10-28 20:29:04 +08:00
'Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IldlYlBsYXlLaWQifQ.eyJpc3MiOiJBTVBXZWJQbGF5IiwiaWF0IjoxNjYxNDQwNDMyLCJleHAiOjE2NzY5OTI0MzIsInJvb3RfaHR0cHNfb3JpZ2luIjpbImFwcGxlLmNvbSJdfQ.z4BMv9_O4MpMK2iFhYkDqPsx53soPSnlXXK3jm99pHqGOrZADvTgEUw2U7_B1W0MAtFiWBYhYcGvWrzaOig6Bw' ,
2022-07-12 22:42:50 +08:00
Referer : 'https://music.apple.com/' ,
'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' ,
2022-10-28 20:29:04 +08:00
Origin : 'https://music.apple.com' ,
2022-07-12 22:42:50 +08:00
}
2022-06-12 15:29:14 +08:00
2022-07-12 22:42:50 +08:00
export const getAlbum = async ( {
2022-06-12 15:29:14 +08:00
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-07-12 22:42:50 +08:00
} ) : Promise < AppleMusicAlbum | undefined > = > {
2022-07-11 11:06:41 +08:00
const keyword = ` ${ artist } ${ name } `
2022-07-12 22:42:50 +08:00
log . debug ( ` [appleMusic] getAlbum: ${ keyword } ` )
2022-07-11 11:06:41 +08:00
const searchResult = await axios ( {
method : 'GET' ,
2022-07-12 22:42:50 +08:00
headers ,
2022-07-11 11:06:41 +08:00
url : 'https://amp-api.music.apple.com/v1/catalog/us/search' ,
params : {
term : keyword ,
types : 'albums' ,
2022-07-12 22:42:50 +08:00
'fields[albums]' : 'artistName,name,editorialVideo,editorialNotes' ,
2022-07-11 11:06:41 +08:00
platform : 'web' ,
2022-07-12 22:42:50 +08:00
limit : '5' ,
l : 'en-us' , // TODO: get from settings
2022-07-11 11:06:41 +08:00
} ,
} ) . catch ( e = > {
log . debug ( '[appleMusic] Search album error' , e )
2022-06-12 15:29:14 +08:00
} )
2022-08-03 23:48:39 +08:00
const albums : AppleMusicAlbum [ ] | undefined =
searchResult ? . data ? . results ? . albums ? . data
2022-10-28 20:29:04 +08:00
2022-07-12 22:42:50 +08:00
const album =
2022-08-03 23:48:39 +08:00
albums ? . find (
2022-07-12 22:42:50 +08:00
a = >
a . attributes . name . toLowerCase ( ) === name . toLowerCase ( ) &&
a . attributes . artistName . toLowerCase ( ) === artist . toLowerCase ( )
2022-08-03 23:48:39 +08:00
) || albums ? . [ 0 ]
2022-07-11 11:06:41 +08:00
if ( ! album ) {
log . debug ( '[appleMusic] No album found on apple music' )
2022-06-12 15:29:14 +08:00
return
}
2022-07-12 22:42:50 +08:00
return album
}
export const getArtist = async (
name : string
) : Promise < AppleMusicArtist | undefined > = > {
const searchResult = await axios ( {
method : 'GET' ,
url : 'https://amp-api.music.apple.com/v1/catalog/us/search' ,
headers ,
params : {
term : name ,
types : 'artists' ,
'fields[artists]' : 'url,name,artwork,editorialVideo,artistBio' ,
'omit[resource:artists]' : 'relationships' ,
platform : 'web' ,
limit : '1' ,
l : 'en-us' , // TODO: get from settings
2022-10-28 20:29:04 +08:00
with : 'serverBubbles' ,
2022-07-12 22:42:50 +08:00
} ,
} ) . catch ( e = > {
log . debug ( '[appleMusic] Search artist error' , e )
} )
2022-10-28 20:29:04 +08:00
const artist = searchResult ? . data ? . results ? . artist ? . data ? . [ 0 ]
2022-07-12 22:42:50 +08:00
if (
artist &&
artist ? . attributes ? . name ? . toLowerCase ( ) === name . toLowerCase ( )
) {
return artist
2022-07-11 11:06:41 +08:00
}
2022-06-12 15:29:14 +08:00
}