There be a website

This commit is contained in:
Roscoe 2025-09-21 03:09:52 +01:00
commit af0907c7c3
Signed by: RoscoeDaWah
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
65 changed files with 708 additions and 14 deletions

View file

@ -0,0 +1,9 @@
class HomeController < ApplicationController
def years_between_dates(date_from, date_to)
return (date_to - date_from).to_i / 365
end
def index
@age = years_between_dates(DateTime.civil_from_format(:local, 2005, 6, 7), DateTime.now)
end
end

View file

@ -0,0 +1,78 @@
require 'uri'
require 'net/http'
class MusicController < ApplicationController
def get_current_track
if Rails.cache.exist?("current_track")
return Rails.cache.read("current_track")
end
uri = URI.parse('https://ws.audioscrobbler.com/2.0/')
params = {
:method => 'user.getrecenttracks',
:user => Rails.application.credentials.lastfm.username,
:format => 'json',
:nowplaying => 'true',
:api_key => Rails.application.credentials.lastfm.api_key
}
uri.query = URI.encode_www_form(params)
req = Net::HTTP::Get.new(uri.to_s)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) {|http|
http.request(req)
}
data = JSON.parse(res.body)
track_data = data["recenttracks"]["track"][0]
image = defined?(track_data["image"][-1]) ? track_data["image"][-1] : false
now_playing = false
if track_data["@attr"] != nil
now_playing = track_data["@attr"]["nowplaying"] == "true"
end
current_track = {
:title => track_data["name"],
:artist => track_data["artist"]["#text"],
:url => track_data["url"],
:image => image["#text"],
:header => if now_playing then "Now Playing" else "Last Track" end
}
return current_track
end
def get_top_tracks
if Rails.cache.exist?("top_tracks")
return Rails.cache.read("top_tracks")
end
uri = URI.parse('https://ws.audioscrobbler.com/2.0/')
params = {
:method => 'user.gettoptracks',
:user => Rails.application.credentials.lastfm.username,
:format => 'json',
:period => '1month',
:limit => 10,
:api_key => Rails.application.credentials.lastfm.api_key
}
uri.query = URI.encode_www_form(params)
req = Net::HTTP::Get.new(uri.to_s)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) {|http|
http.request(req)
}
data = JSON.parse(res.body)
top_tracks = []
data["toptracks"]["track"].each do |track|
top_tracks.push({
:title => track["name"],
:artist => track["artist"]["name"],
:url => track["url"],
:plays => track["playcount"]
})
end
return top_tracks
end
def index
@current_track = get_current_track
@top_tracks = get_top_tracks
end
end