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,12 @@
module Adamantium
module Actions
module RecentlyPlayed
class Index < Action
include Deps["views.recently_played.index"]
def handle(req, res)
res.render index
end
end
end
end
end

View File

@@ -27,6 +27,10 @@ I currently live in Canberra with my partner and [our dogs](https://instagram.co
In my spare time I like to tinker on various Ruby projects (including the software that powers this blog), make things with [Processing](https://processing.org), explore the various [hiking trails](/hikes) around Canberra and potter around [in the garden](/tagged/garden).
### Currently listening to
<div hx-get="/recently_played" hx-trigger="load"></div>
### 2023 Goals
- 25 Hikes

View File

@@ -0,0 +1,43 @@
require "httparty"
require "jwt"
module Adamantium
module Queries
module Posts
class RecentlyPlayed
include Deps["settings"]
def call
resp = HTTParty.get("https://api.music.apple.com/v1/me/recent/played", {
headers: {
"Authorization" => "Bearer #{jwt}",
"Music-User-Token" => settings.apple_music_user_token
}
})
resp.body
end
private
def jwt
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

View File

@@ -0,0 +1,9 @@
div class="grid grid-cols-4 gap-4"
- recently_played_music.each do |album|
a href="#{album[:href]}"
div
img class="rounded transition-transform ease-out hover:scale-105" src="#{album[:image]}"
span class="inline-block text-sm"
= album[:name]
span class="inline-block text-sm font-bold"
= album[:artist]

View File

@@ -0,0 +1,27 @@
module Adamantium
module Views
module RecentlyPlayed
class Index < Adamantium::View
config.layout = false
include Deps["queries.posts.recently_played"]
expose :recently_played_music do |recently_played_result|
# raise recently_played_result["data"].inspect
JSON.parse(recently_played_result)["data"].reject{ |a| a["type"] != "albums" }.map do |album|
{
artist: album["attributes"]["artistName"],
name: album["attributes"]["name"],
image: album["attributes"]["artwork"]["url"].gsub("{w}", "256").gsub("{h}", "256"),
href: album["attributes"]["url"]
}
end
end
private_expose :recently_played_result do
recently_played.call
end
end
end
end
end