From 4a9bf38ef61b39e482458ba56cd1e6fea379b4d7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 21 Nov 2025 21:01:50 -0500 Subject: Added "listening to" box in sidebar --- app/assets/stylesheets/main/layout.scss | 11 +++++++++++ app/controllers/application_controller.rb | 10 ++++++++++ app/models/scrobble.rb | 2 ++ app/views/layouts/application.html.haml | 10 ++++++++++ config/credentials.yml.enc | 2 +- config/schedule.rb | 4 ++++ db/migrate/20251122012500_create_scrobbles.rb | 12 ++++++++++++ db/schema.rb | 11 ++++++++++- lib/tasks/tasks.rake | 21 +++++++++++++++++++++ test/fixtures/scrobbles.yml | 13 +++++++++++++ test/models/scrobble_test.rb | 7 +++++++ 11 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 app/models/scrobble.rb create mode 100644 db/migrate/20251122012500_create_scrobbles.rb create mode 100644 test/fixtures/scrobbles.yml create mode 100644 test/models/scrobble_test.rb diff --git a/app/assets/stylesheets/main/layout.scss b/app/assets/stylesheets/main/layout.scss index ef31ada..d5fe7a7 100644 --- a/app/assets/stylesheets/main/layout.scss +++ b/app/assets/stylesheets/main/layout.scss @@ -228,3 +228,14 @@ h2.centered { text-decoration: none; } } + +#scrobble-box { + img { + margin: 0 auto; + display: block; + } + + p { + text-align: center; + } +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0174cae..ad46fb9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,19 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :choose_random_song private def after_sign_out_path_for(resource) new_session_path(resource) end + + def choose_random_song + ids = Scrobble.ids + if ids.empty? + @random_song = nil + else + @random_song = Scrobble.find(ids.sample) + end + end end diff --git a/app/models/scrobble.rb b/app/models/scrobble.rb new file mode 100644 index 0000000..f527612 --- /dev/null +++ b/app/models/scrobble.rb @@ -0,0 +1,2 @@ +class Scrobble < ApplicationRecord +end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 1d0bea3..6bc7041 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -73,6 +73,16 @@ %li = image_tag "feed.png" = link_to "Atom feed", blogs_url(format: :atom) + - unless @random_song.nil? + .sidebar-module#scrobble-box + .bubble.rounded + %h2 Listening to + = image_tag @random_song.image, width: "174px" + %p + %strong= @random_song.title + by + = @random_song.artist + %p= @random_song.album .sidebar-module .bubble.rounded %h2 Meta diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index cd9cac5..e08912f 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -TMbFARKxiuopoIWE/n0a8KMCh28dFjTGFpfXoXHUQmDlVhA40doz/aLWCYDE00djC70eM4BwXZmaDFC7tly1miU0OymvYijYKNb5Kv9bu2rihSprleyjnBrCF4qykWpDh8XfzXGfiDegSuZzw5RUGKkEU+EGJNIPTnPKAm2MSSnPStVFt1gGxGuYRd0pXCgFUW/USVYnbJEPv6DOxqh3HmKmneP29fcx1V+N/RccyzHsjL2S5lej5s8Q4oAy+SZ4+xLK9Pdh6nA3dZ82fNTLffqmnFxEovuW5n6Cjt0Ka2MZRvYg6sBNDvUVvQSw4u1BRXx7JOHcIWiKdykF/uHdrSwmIPepwd+zs/HCxrthdMSry+yrLwJpZ67JiMA/VKIMYzgLy8yk2JEblBXq3FGo44TWwumIkfTn4H/6UZNb4jGJ4dH+LAFiE7il2X3BbvBb4EkYB3xbu+zqvnohOzuVKkJtjILV6RBrEFzbCijwi3PBOoX8O9fwW8B0BduSp4AhFa+dyenOkD2HjtyEMttJAPuuw3neQKhkv/4SaPaugcD4vKONSwU3F/7nB7yMVQqofVLIc2a1tJaTjWIlNIwVssZS2HS5OZFTpv1XJpCyMcLO/EAe9poqiPieE8WwpdXntRs9O/QQqPWyt2ZdHD8RMKerZjbU1wV58h4rJmmAKUkTCuo=--Jo9AwrM/hr9nWWoS--iHpkNsZwBWB0lySmzkcLdA== \ No newline at end of file +MTHDW1ZOa9otC2lb9iG3bKlhzM8WgEoJddmcsv22b7TEv+ZxlnjBvql1MGJjfo+tNl9dT4gqUOH41Q2WfRhwIokibD6cLMKl8z3VPLHdARR3HW4OzEJoLYXYB5ycLtSFmL3jq/TkMBkctRvPo7jeGGBwtEHvh1tBx6cGr/fEvzWMKYMUPiugaMN3OPExZ5VaD1sCHTMm7erwP06+8KTABwGfF6quthHctj/39RusmAWaObVsUwm70nqij+/qekJEZS3xYEuCDZlKqQvwC7Z+qJlmO8vj8CYNFq80bD9MiuEL0kiTVp22ZyMj2TY6YXy2jabkVx5c7FVytx/+kbaNdGtLYqvvuqDXodcai18OWUhR3r+tzPqcinggWmzZeJ0O54dE7Srpe3wGFH3xmgXHRa1jczmCDwlGRTqmjX7E6CBiP7u5NAKLeALvADWGocJwUi0r8r6E3/a8rpsaL97JCjL1GNCDlRSAapqHafzszDWN31yG5mLc9OUoE2RJjU7bgohrhoAPyHpetRxO7fBpZ0quNAh1awiTXVTcNUlDT+UPkQi1mvQo47G3CM9OG8hlgKtfkMMKsl9D92k/g7zIyJzLsPKqYnxHj+qcnU0939xJ0x4MnUPam1oq7OF2FtBgU/OCDxMuM//qSI2Q4+RRKRHs/NLdADxRgO3WJZ+oe2r9YBfTYTlbdOEKmCxSrQMzpB/1jq0jSTRYynn/GO0Z1Pjx0wVzH8vjVosXfh6/PaKslMdHbOM=--A5y7uJSVaL0539+f--FP9Oigql2/EdcYszZ6hj/A== \ No newline at end of file diff --git a/config/schedule.rb b/config/schedule.rb index e53ebf4..268dfcb 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -21,3 +21,7 @@ every 1.day, at: "6:00am" do rake "thoughts:email_upvote_report" end + +every 6.hours do + rake "thoughts:refresh_scrobbles" +end diff --git a/db/migrate/20251122012500_create_scrobbles.rb b/db/migrate/20251122012500_create_scrobbles.rb new file mode 100644 index 0000000..9f00435 --- /dev/null +++ b/db/migrate/20251122012500_create_scrobbles.rb @@ -0,0 +1,12 @@ +class CreateScrobbles < ActiveRecord::Migration[7.1] + def change + create_table :scrobbles do |t| + t.string :title + t.string :artist + t.string :album + t.string :image + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 76e9ad8..74e1976 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_05_12_181245) do +ActiveRecord::Schema[7.1].define(version: 2025_11_22_012500) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -352,6 +352,15 @@ ActiveRecord::Schema[7.1].define(version: 2025_05_12_181245) do t.index ["recordable_type", "recordable_id"], name: "index_records_on_recordable_type_and_recordable_id" end + create_table "scrobbles", force: :cascade do |t| + t.string "title" + t.string "artist" + t.string "album" + t.string "image" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "streams", force: :cascade do |t| t.string "title" t.text "body" diff --git a/lib/tasks/tasks.rake b/lib/tasks/tasks.rake index fee22f4..91e196f 100644 --- a/lib/tasks/tasks.rake +++ b/lib/tasks/tasks.rake @@ -1,3 +1,5 @@ +require 'net/http' + namespace :thoughts do desc "Email a review of the last day's upvotes" task :email_upvote_report => :environment do @@ -7,4 +9,23 @@ namespace :thoughts do VoteMailer.with(votes: votes).daily_report_email.deliver end end + + desc "Refresh the recently listened tracks" + task :refresh_scrobbles => :environment do + Scrobble.destroy_all + + api_key = Rails.application.credentials.lastfm_api_key + url = URI.parse("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=fefferburbia&api_key=#{api_key}&format=json") + req = Net::HTTP::Get.new(url.to_s) + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + output = JSON.parse(res.body) + + items = output["recenttracks"]["track"].map {|p| [p["name"], p["artist"]["#text"], p["album"]["#text"], p["image"][2]["#text"]]}.sort.uniq + + items.each do |item| + Scrobble.create!(title: item[0], artist: item[1], album: item[2], image: item[3]) + end + end end diff --git a/test/fixtures/scrobbles.yml b/test/fixtures/scrobbles.yml new file mode 100644 index 0000000..9ca535d --- /dev/null +++ b/test/fixtures/scrobbles.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + artist: MyString + album: MyString + image: MyString + +two: + title: MyString + artist: MyString + album: MyString + image: MyString diff --git a/test/models/scrobble_test.rb b/test/models/scrobble_test.rb new file mode 100644 index 0000000..48541da --- /dev/null +++ b/test/models/scrobble_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ScrobbleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end -- cgit 1.4.1