about summary refs log tree commit diff stats
path: root/lib/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tasks')
-rw-r--r--lib/tasks/tasks.rake21
1 files changed, 21 insertions, 0 deletions
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 @@
1require 'net/http'
2
1namespace :thoughts do 3namespace :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
10end 31end