diff options
| -rw-r--r-- | app/assets/stylesheets/main/layout.scss | 11 | ||||
| -rw-r--r-- | app/controllers/application_controller.rb | 10 | ||||
| -rw-r--r-- | app/models/scrobble.rb | 2 | ||||
| -rw-r--r-- | app/views/layouts/application.html.haml | 10 | ||||
| -rw-r--r-- | config/credentials.yml.enc | 2 | ||||
| -rw-r--r-- | config/schedule.rb | 4 | ||||
| -rw-r--r-- | db/migrate/20251122012500_create_scrobbles.rb | 12 | ||||
| -rw-r--r-- | db/schema.rb | 11 | ||||
| -rw-r--r-- | lib/tasks/tasks.rake | 21 | ||||
| -rw-r--r-- | test/fixtures/scrobbles.yml | 13 | ||||
| -rw-r--r-- | test/models/scrobble_test.rb | 7 |
11 files changed, 101 insertions, 2 deletions
| 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 { | |||
| 228 | text-decoration: none; | 228 | text-decoration: none; |
| 229 | } | 229 | } |
| 230 | } | 230 | } |
| 231 | |||
| 232 | #scrobble-box { | ||
| 233 | img { | ||
| 234 | margin: 0 auto; | ||
| 235 | display: block; | ||
| 236 | } | ||
| 237 | |||
| 238 | p { | ||
| 239 | text-align: center; | ||
| 240 | } | ||
| 241 | } | ||
| 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 @@ | |||
| 1 | class ApplicationController < ActionController::Base | 1 | class ApplicationController < ActionController::Base |
| 2 | protect_from_forgery with: :exception | 2 | protect_from_forgery with: :exception |
| 3 | before_action :choose_random_song | ||
| 3 | 4 | ||
| 4 | private | 5 | private |
| 5 | 6 | ||
| 6 | def after_sign_out_path_for(resource) | 7 | def after_sign_out_path_for(resource) |
| 7 | new_session_path(resource) | 8 | new_session_path(resource) |
| 8 | end | 9 | end |
| 10 | |||
| 11 | def choose_random_song | ||
| 12 | ids = Scrobble.ids | ||
| 13 | if ids.empty? | ||
| 14 | @random_song = nil | ||
| 15 | else | ||
| 16 | @random_song = Scrobble.find(ids.sample) | ||
| 17 | end | ||
| 18 | end | ||
| 9 | end | 19 | 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 @@ | |||
| 1 | class Scrobble < ApplicationRecord | ||
| 2 | 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 @@ | |||
| 73 | %li | 73 | %li |
| 74 | = image_tag "feed.png" | 74 | = image_tag "feed.png" |
| 75 | = link_to "Atom feed", blogs_url(format: :atom) | 75 | = link_to "Atom feed", blogs_url(format: :atom) |
| 76 | - unless @random_song.nil? | ||
| 77 | .sidebar-module#scrobble-box | ||
| 78 | .bubble.rounded | ||
| 79 | %h2 Listening to | ||
| 80 | = image_tag @random_song.image, width: "174px" | ||
| 81 | %p | ||
| 82 | %strong= @random_song.title | ||
| 83 | by | ||
| 84 | = @random_song.artist | ||
| 85 | %p= @random_song.album | ||
| 76 | .sidebar-module | 86 | .sidebar-module |
| 77 | .bubble.rounded | 87 | .bubble.rounded |
| 78 | %h2 Meta | 88 | %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+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c9OUoE2RJjU7bgohrhoAPyHpetRxO7fBpZ0quNAh1awiTXVTcNUlDT+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 @@ | |||
| 21 | every 1.day, at: "6:00am" do | 21 | every 1.day, at: "6:00am" do |
| 22 | rake "thoughts:email_upvote_report" | 22 | rake "thoughts:email_upvote_report" |
| 23 | end | 23 | end |
| 24 | |||
| 25 | every 6.hours do | ||
| 26 | rake "thoughts:refresh_scrobbles" | ||
| 27 | 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 @@ | |||
| 1 | class CreateScrobbles < ActiveRecord::Migration[7.1] | ||
| 2 | def change | ||
| 3 | create_table :scrobbles do |t| | ||
| 4 | t.string :title | ||
| 5 | t.string :artist | ||
| 6 | t.string :album | ||
| 7 | t.string :image | ||
| 8 | |||
| 9 | t.timestamps | ||
| 10 | end | ||
| 11 | end | ||
| 12 | 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 @@ | |||
| 10 | # | 10 | # |
| 11 | # It's strongly recommended that you check this file into your version control system. | 11 | # It's strongly recommended that you check this file into your version control system. |
| 12 | 12 | ||
| 13 | ActiveRecord::Schema[7.1].define(version: 2025_05_12_181245) do | 13 | ActiveRecord::Schema[7.1].define(version: 2025_11_22_012500) do |
| 14 | create_table "active_storage_attachments", force: :cascade do |t| | 14 | create_table "active_storage_attachments", force: :cascade do |t| |
| 15 | t.string "name", null: false | 15 | t.string "name", null: false |
| 16 | t.string "record_type", null: false | 16 | t.string "record_type", null: false |
| @@ -352,6 +352,15 @@ ActiveRecord::Schema[7.1].define(version: 2025_05_12_181245) do | |||
| 352 | t.index ["recordable_type", "recordable_id"], name: "index_records_on_recordable_type_and_recordable_id" | 352 | t.index ["recordable_type", "recordable_id"], name: "index_records_on_recordable_type_and_recordable_id" |
| 353 | end | 353 | end |
| 354 | 354 | ||
| 355 | create_table "scrobbles", force: :cascade do |t| | ||
| 356 | t.string "title" | ||
| 357 | t.string "artist" | ||
| 358 | t.string "album" | ||
| 359 | t.string "image" | ||
| 360 | t.datetime "created_at", null: false | ||
| 361 | t.datetime "updated_at", null: false | ||
| 362 | end | ||
| 363 | |||
| 355 | create_table "streams", force: :cascade do |t| | 364 | create_table "streams", force: :cascade do |t| |
| 356 | t.string "title" | 365 | t.string "title" |
| 357 | t.text "body" | 366 | 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 @@ | |||
| 1 | require 'net/http' | ||
| 2 | |||
| 1 | namespace :thoughts do | 3 | namespace :thoughts do |
| 2 | desc "Email a review of the last day's upvotes" | 4 | desc "Email a review of the last day's upvotes" |
| 3 | task :email_upvote_report => :environment do | 5 | task :email_upvote_report => :environment do |
| @@ -7,4 +9,23 @@ namespace :thoughts do | |||
| 7 | VoteMailer.with(votes: votes).daily_report_email.deliver | 9 | VoteMailer.with(votes: votes).daily_report_email.deliver |
| 8 | end | 10 | end |
| 9 | end | 11 | end |
| 12 | |||
| 13 | desc "Refresh the recently listened tracks" | ||
| 14 | task :refresh_scrobbles => :environment do | ||
| 15 | Scrobble.destroy_all | ||
| 16 | |||
| 17 | api_key = Rails.application.credentials.lastfm_api_key | ||
| 18 | url = URI.parse("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=fefferburbia&api_key=#{api_key}&format=json") | ||
| 19 | req = Net::HTTP::Get.new(url.to_s) | ||
| 20 | res = Net::HTTP.start(url.host, url.port) {|http| | ||
| 21 | http.request(req) | ||
| 22 | } | ||
| 23 | output = JSON.parse(res.body) | ||
| 24 | |||
| 25 | items = output["recenttracks"]["track"].map {|p| [p["name"], p["artist"]["#text"], p["album"]["#text"], p["image"][2]["#text"]]}.sort.uniq | ||
| 26 | |||
| 27 | items.each do |item| | ||
| 28 | Scrobble.create!(title: item[0], artist: item[1], album: item[2], image: item[3]) | ||
| 29 | end | ||
| 30 | end | ||
| 10 | end | 31 | 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 @@ | |||
| 1 | # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
| 2 | |||
| 3 | one: | ||
| 4 | title: MyString | ||
| 5 | artist: MyString | ||
| 6 | album: MyString | ||
| 7 | image: MyString | ||
| 8 | |||
| 9 | two: | ||
| 10 | title: MyString | ||
| 11 | artist: MyString | ||
| 12 | album: MyString | ||
| 13 | 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 @@ | |||
| 1 | require "test_helper" | ||
| 2 | |||
| 3 | class ScrobbleTest < ActiveSupport::TestCase | ||
| 4 | # test "the truth" do | ||
| 5 | # assert true | ||
| 6 | # end | ||
| 7 | end | ||
