about summary refs log tree commit diff stats
path: root/lib/tasks/tasks.rake
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tasks/tasks.rake')
-rw-r--r--lib/tasks/tasks.rake31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/tasks/tasks.rake b/lib/tasks/tasks.rake new file mode 100644 index 0000000..91e196f --- /dev/null +++ b/lib/tasks/tasks.rake
@@ -0,0 +1,31 @@
1require 'net/http'
2
3namespace :thoughts do
4 desc "Email a review of the last day's upvotes"
5 task :email_upvote_report => :environment do
6 votes = Vote.where("created_at > ?", 1.day.ago).where(upvote: 1).all
7
8 unless votes.empty?
9 VoteMailer.with(votes: votes).daily_report_email.deliver
10 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
31end