Add Apple Music integration for now playing

This commit is contained in:
2023-11-02 08:58:44 +11:00
parent c1d756ec6f
commit 4b6107188e
15 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
module Admin
module Actions
module AppleMusic
class Index < Action
include Deps["views.apple_music.index"]
def handle(req, res)
res.render index
end
end
end
end
end

View File

@@ -49,5 +49,7 @@ module Admin
post "/trips/remove_post", to: Auth.call(action: "trips.remove_post")
get "/trips/new", to: Auth.call(action: "trips.new")
post "/trips/:id", to: Auth.call(action: "trips.update")
get "/apple_music", to: Auth.call(action: "apple_music.index")
end
end

View File

@@ -0,0 +1,32 @@
script src="https://js-cdn.music.apple.com/musickit/v3/musickit.js" data-web-components async
javascript:
document.addEventListener('musickitloaded', async function () {
// Call configure() to configure an instance of MusicKit on the Web.
try {
await MusicKit.configure({
developerToken: '#{developer_token}',
app: {
name: 'Blog',
build: '2023.1',
},
});
} catch (err) {
// Handle configuration error
}
// MusicKit instance is available
const music = MusicKit.getInstance();
await music.authorize();
document.querySelectorAll(".mut")[0].innerHTML = music.musicUserToken;
});
div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200"
h1 Admin // Apple Music auth
div
p Your music user token is:
code
pre class="mut"
p keep it secret, keep it safe

View File

@@ -17,6 +17,8 @@ div class="max-w-prose mx-auto prose dark:prose-invert"
a href="/admin/bookmarks" Bookmarks
li
a href="/admin/trips" Trips
li
a href="/admin/apple_music" Apple Music
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"

View File

@@ -0,0 +1,29 @@
require "jwt"
module Admin
module Views
module AppleMusic
class Index < Admin::View
include Deps["settings"]
expose :developer_token do
authentication_payload = {
iss: settings.apple_music_team,
iat: Time.now.to_i, # Issue date
exp: Time.now.to_i + 3600 # Expiry of this token.
}
# The file we got from Apple
apple_music_secret = File.read(File.join(Hanami.app.root, "config", "AuthKey_#{settings.apple_music_key}.p8"))
private_key = OpenSSL::PKey::EC.new(apple_music_secret)
JWT.encode(
authentication_payload,
private_key,
'ES256',
kid: settings.apple_music_key
)
end
end
end
end
end