-
Notifications
You must be signed in to change notification settings - Fork 4
/
lastfmhue.rb
82 lines (71 loc) · 1.77 KB
/
lastfmhue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'color'
require 'hue'
require 'miro'
require 'nokogiri'
require 'open-uri'
lastfm_user = ENV['LASTFM_USER']
lastfm_user = 'laser-kun' if lastfm_user.nil?
url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{lastfm_user}&limit=1&api_key=#{ENV['LASTFM_API_KEY']}"
hue = Hue::Client.new
def colour_to_hue(colour)
r = colour.r
g = colour.g
b = colour.b
max = [r, g, b].max
min = [r, g, b].min
delta = max - min
v = max * 100
if max != 0.0
s = delta / max * 100
else
s = 0.0
end
if s == 0.0
h = 0.0
else
if r == max
h = (g - b) / delta
elsif g == max
h = 2 + (b - r) / delta
elsif b == max
h = 4 + (r - g) / delta
end
h *= 60.0
if h < 0
h += 360.0
end
end
{
hue: (h * 182.04).round,
saturation: (s / 100.0 * 255.0).round,
bri: (v / 100.0 * 255.0).round
}
end
current_album_art = nil
while true do
doc = Nokogiri::XML(open(url))
new_album_art = doc.xpath("//image[@size='large']").first.text
if new_album_art != current_album_art
unless new_album_art.empty?
current_album_art = new_album_art
puts "Detected track change to #{doc.xpath("//track/name").first.text} [#{new_album_art}]"
begin
file = Tempfile.new 'lastfmhue'
file.write open(new_album_art).read
file.close
colours = Miro::DominantColors.new(file.path)
hue.lights.each_with_index do |light, i|
light.on!
rgb = colours.to_rgb[i]
target_colour = Color::RGB.new(rgb[0], rgb[1], rgb[2])
puts "Transitioning #{light.name} to #{rgb}"
light.set_state(colour_to_hue(target_colour), transition: 5)
end
ensure
file.close
file.unlink
end
end
end
sleep 18
end