Add top track to weekly post

This commit is contained in:
2023-03-05 22:54:18 +11:00
parent ea9d092505
commit 03bf0eca9f
9 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
module Adamantium
module Actions
module Posts
class TopTracks < Action
include Deps["views.posts.top_tracks", query: "queries.posts.top_tracks"]
def handle(req, res)
res.content_type = "Application/JSON"
res.status = 200
tracks = query.call(slug: req.params[:slug])
track = if tracks.is_a? Array
tracks.first
else
tracks
end
if track
res.render top_tracks, track: track
end
end
end
end
end
end

View File

@@ -0,0 +1,20 @@
require "lastfm"
require "time_math"
module Adamantium
module Queries
module Posts
class TopTracks
include Deps["settings", "repos.post_repo"]
def call(slug:)
post = post_repo.fetch!(slug)
lastfm = Lastfm.new(settings.lastfm_api_key, settings.lastfm_secret)
lastfm.user.get_weekly_track_chart(user: "dNitza", from: TimeMath.week.floor(post.published_at).to_i, to: TimeMath.week.ceil(post.published_at).to_i)
end
end
end
end
end

View File

@@ -24,6 +24,10 @@ article class="h-entry"
- if post.location
img class="shadow-solid shadow-pink-100 dark:shadow-pink-200 rounded mb-4" src=post.large_map
- if post.tags.map(&:label).include? "weekly"
div class="max-w-prose mx-auto text-gray-600 dark:text-gray-200 flex"
div class="mx-auto" hx-get="/post/top_tracks/#{post.slug}" hx-trigger="load"
div class="mb-4 max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"
div class="max-w-prose mx-auto text-gray-600 dark:text-gray-200 flex"

View File

@@ -0,0 +1,7 @@
a href=url class="block flex bg-pink-100 dark:bg-pink-600 rounded px-4 py-2 mb-12"
div class="mr-4 my-auto"
img class="rounded h-34 w-34 my-auto" src=image
div
p class="text-lg" Top track this week
p class="text-xl"= "#{name} by #{artist}"

View File

@@ -0,0 +1,25 @@
module Adamantium
module Views
module Posts
class TopTracks < Adamantium::View
config.layout = false
expose :name do |track:|
track["name"]
end
expose :artist do |track:|
track.dig("artist", "content")
end
expose :image do |track:|
track["image"].detect { |i| i["size"] == "small" }["content"]
end
expose :url do |track:|
track["url"]
end
end
end
end
end