Refactor app in to its own slice
This commit is contained in:
30
slices/main/queries/blogroll/index.rb
Normal file
30
slices/main/queries/blogroll/index.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
require "httparty"
|
||||
|
||||
module Main
|
||||
module Queries
|
||||
module Blogroll
|
||||
class Index
|
||||
include Deps["settings"]
|
||||
|
||||
def call
|
||||
resp = HTTParty.get("https://#{settings.rss_url}/api/greader.php/reader/api/0/subscription/list?output=json", {
|
||||
headers: {
|
||||
"Authorization" => "GoogleLogin auth=#{auth_token}"
|
||||
}
|
||||
})
|
||||
|
||||
resp.body
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def auth_token
|
||||
auth_url = "https://#{settings.rss_url}/api/greader.php/accounts/ClientLogin?Email=#{settings.rss_username}&Passwd=#{settings.rss_password}"
|
||||
resp = HTTParty.get(auth_url)
|
||||
auth = resp.match(/SID=(.*)/)
|
||||
auth[1].strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
43
slices/main/queries/posts/recently_played.rb
Normal file
43
slices/main/queries/posts/recently_played.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
require "httparty"
|
||||
require "jwt"
|
||||
|
||||
module Main
|
||||
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
|
41
slices/main/queries/posts/top_tracks.rb
Normal file
41
slices/main/queries/posts/top_tracks.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require "lastfm"
|
||||
require "time_math"
|
||||
|
||||
module Main
|
||||
module Queries
|
||||
module Posts
|
||||
class TopTracks
|
||||
include Deps["settings", "repos.post_repo", "repos.top_track_repo"]
|
||||
|
||||
def call(slug:)
|
||||
ENV["TZ"] = "Australia/Melbourne"
|
||||
post = post_repo.fetch!(slug)
|
||||
start_date = TimeMath.week.floor(post.published_at).to_i
|
||||
end_date = TimeMath.week.ceil(post.published_at).to_i
|
||||
|
||||
top_tracks = top_track_repo.for_post(id: post.id)
|
||||
|
||||
return top_tracks unless top_tracks.empty?
|
||||
|
||||
lastfm = Lastfm.new(settings.lastfm_api_key, settings.lastfm_secret)
|
||||
|
||||
tracks = lastfm.user.get_weekly_track_chart(user: "dNitza", from: start_date, to: end_date)
|
||||
|
||||
track = if tracks.is_a? Array
|
||||
tracks.first
|
||||
else
|
||||
tracks
|
||||
end
|
||||
|
||||
if track
|
||||
mb_id = (track["mbid"] == {}) ? "unknown" : track["mbid"]
|
||||
top_track_repo.upsert(post_id: post.id, name: track["name"], artist: track.dig("artist", "content"), url: track["url"], mb_id: mb_id)
|
||||
end
|
||||
ENV["TZ"] = nil
|
||||
|
||||
top_track_repo.for_post(id: post.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user