-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Apple Music): Automated Authentication #623
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import xurl from 'url'; | ||
import path from 'path'; | ||
|
||
import got from 'got'; | ||
import Promise from 'bluebird'; | ||
import NodeCache from 'node-cache'; | ||
import {Client} from '@yujinakayama/apple-music'; | ||
|
@@ -22,58 +23,87 @@ export default class AppleMusic { | |
// https://www.debuggex.com/r/Pv_Prjinkz1m2FOB | ||
VALID_URL: | ||
/(?:(?:(?:(?:https?:\/\/)?(?:www\.)?)(?:(?:music|(?:geo\.itunes))\.apple.com)\/([a-z]{2})\/(song|album|artist|playlist)\/(?:([^/]+)\/)?\w+)|(?:apple_music:(track|album|artist|playlist):([\w.]+)))/, | ||
PROP_SCHEMA: {}, | ||
PROP_SCHEMA: { | ||
developerToken: {type: 'string'}, | ||
}, | ||
}; | ||
|
||
[symbols.meta] = AppleMusic[symbols.meta]; | ||
|
||
#store = { | ||
cache: new NodeCache(), | ||
core: null, | ||
axiosInstance: null, | ||
expiry: null, | ||
defaultStorefront: null, | ||
isAuthenticated: false, | ||
}; | ||
|
||
constructor(config) { | ||
if (!config) throw new Error(`[AppleMusic] Please define a configuration object`); | ||
if (typeof config !== 'object') throw new Error(`[AppleMusic] Please define a configuration as an object`); | ||
if (!config.developerToken) | ||
throw new Error(`[AppleMusic] Please define [developerToken] as a property within the configuration`); | ||
this.#store.core = new Client({developerToken: config.developerToken}); | ||
for (let instance of [this.#store.core.albums, this.#store.core.artists, this.#store.core.playlists, this.#store.core.songs]) | ||
instance.axiosInstance.interceptors.request.use(conf => ((conf.headers.origin = 'https://music.apple.com'), conf)); | ||
this.#store.core = new Client({}); | ||
this.#store.axiosInstance = this.#store.core.songs.axiosInstance; | ||
for (let instance of [this.#store.core.albums, this.#store.core.artists, this.#store.core.playlists]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏽 |
||
instance.axiosInstance = this.#store.axiosInstance; | ||
this.#store.axiosInstance.defaults.headers['Origin'] = 'https://music.apple.com'; | ||
this.#store.defaultStorefront = config.storefront; | ||
this.#store.isAuthenticated = !!config.developerToken; | ||
} | ||
|
||
loadConfig(_config) {} | ||
expiresAt(developerToken) { | ||
let segments = developerToken.split('.'); | ||
let payload = Buffer.from(segments[1] || '', 'base64'); | ||
let parsed = JSON.parse(payload.toString()); | ||
return parsed.exp * 1000; | ||
} | ||
|
||
loadConfig(config) { | ||
if (config.developerToken) { | ||
this.#store.expiry = this.expiresAt(config.developerToken); | ||
this.#store.core.configuration.developerToken = config.developerToken; | ||
this.#store.axiosInstance.defaults.headers['Authorization'] = `Bearer ${config.developerToken}`; | ||
} | ||
} | ||
|
||
hasOnceAuthed() { | ||
throw Error('Unimplemented: [AppleMusic:hasOnceAuthed()]'); | ||
return this.#store.isAuthenticated; | ||
} | ||
|
||
isAuthed() { | ||
return this.#store.isAuthenticated; | ||
return Date.now() < this.#store.expiry; | ||
} | ||
|
||
newAuth() { | ||
throw Error('Unimplemented: [AppleMusic:newAuth()]'); | ||
} | ||
|
||
canTryLogin() { | ||
return !!this.#store.core.configuration.developerToken; | ||
return true; | ||
} | ||
|
||
hasProps() { | ||
return false; | ||
return this.#store.isAuthenticated; | ||
} | ||
|
||
getProps() { | ||
throw Error('Unimplemented: [AppleMusic:getProps()]'); | ||
return { | ||
developerToken: this.#store.core.configuration.developerToken, | ||
}; | ||
} | ||
|
||
async login() { | ||
throw Error('Unimplemented: [AppleMusic:login()]'); | ||
let browsePage = await got('https://music.apple.com/us/browse').text(); | ||
let scriptUri; | ||
if (!(scriptUri = browsePage.match(/assets\/index-[a-z0-9]{8}\.js/)?.[0])) | ||
throw new Error('Unable to extract core script from Apple Music'); | ||
let script = await got(`https://music.apple.com/${scriptUri}`).text(); | ||
let developerToken; | ||
if (!(developerToken = script.match(/eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IldlYlBsYXlLaWQifQ[^"]+/)?.[0])) | ||
throw new Error('Unable to extract developerToken from Apple Music core script'); | ||
this.#store.expiry = this.expiresAt(developerToken); | ||
this.#store.core.configuration.developerToken = developerToken; | ||
this.#store.axiosInstance.defaults.headers['Authorization'] = `Bearer ${developerToken}`; | ||
return (this.#store.isAuthenticated = true); | ||
} | ||
|
||
validateType(uri) { | ||
|
@@ -94,7 +124,7 @@ export default class AppleMusic { | |
const id = isURI ? match[5] : parsedURL.query.i || path.basename(parsedURL.pathname); | ||
const type = isURI ? match[4] : collection_type == 'album' && parsedURL.query.i ? 'track' : collection_type; | ||
const scope = collection_type == 'track' || (collection_type == 'album' && parsedURL.query.i) ? 'song' : collection_type; | ||
storefront = match[1] || storefront || (#store in this ? this.#store.defaultStorefront : 'us'); | ||
storefront = match[1] || storefront || (#store in this ? this.#store.defaultStorefront : null) || 'us'; | ||
return { | ||
id, | ||
type, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Especially love this~!