-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.coffee
140 lines (123 loc) · 3.85 KB
/
lastfm.coffee
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# LastFM for Ubersicht
# Patrick Pan, [email protected]
# Replace "your_username_here" with your username (leave the quotes)
# If you're being told to enter your username even though you already have:
# 1) You haven't scrobbled before (probably not)
# 2) Last.FM changed their API. Email me and mention Last.FM in the subject line!
command: 'username=' + 'your_username_here' + '; curl -s "http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=$username&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&limit=1"'
refreshFrequency: 5000
render: (output) ->
# this is a bit of a hack until jQuery UI is included natively
$.getScript("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js", () ->
$('#lastfm').draggable()
return
)
# apologize for the DOM vomit here
'<div id="lastfm"><div id="display"><div id="art"></div><div id="overart"></div><div id="text"><div><span id="timestamp"></span><br><span id="artistalbum">something\'s wrong!</span></div><div id="track"></div></div></div></div>'
afterRender: ->
update: (output) ->
if(!output)
return
data = JSON.parse(output)
if(!data.recenttracks)
$('#track').text 'Please enter your Last.FM username in lastfm.coffee!'
return
track = data.recenttracks.track[0]
if track['@attr']
$('#timestamp').text 'Now Playing'
else
$('#timestamp').text @timeString(new Date / 1000 - (track.date.uts))
if $('#track').text() != track.name
$('#track').text track.name
$('#track').css
'font-size': '4rem'
'height': 'auto'
@autoSizeText($('#track')[0])
$('#artistalbum').text track.artist['#text'] + ": " + track.album['#text']
if(track.image)
$('#art').css
'background-image': 'url(' + track.image[track.image.length-1]['#text'] + ')'
'background-size': 'cover'
'background-repeat': 'no-repeat'
return
style: """
color: #fff
#lastfm
box-shadow: 25px 25px 100px 15px rgba(0,0,0,1)
overflow: hidden
position: relative
padding-bottom: 5rem
background: #000
border-bottom: 1px solid #000
border-right: 1px solid #000
#display
padding: 0 0
#art
width: 600px
height: 600px
z-index:1
#overart
width: 600px
height: 600px
z-index: 2
background-image: -webkit-gradient(
linear, left top, left bottom, from(rgba(0,0,0,0)),
to(rgba(0,0,0,1)), color-stop(.6,rgba(0,0,0,0.3)), color-stop(.75,rgba(0,0,0,0.6)), color-stop(.9,rgba(0,0,0,0.9)));
position: absolute
top:0
left:0
#text
position:absolute
font-family: Helvetica Neue
z-index:3
bottom:0
margin: 1rem
text-shadow: 0 0 15px rgba(255,255,255,0.1)
overflow-x: scroll
#artistalbum, #timestamp
font-size: 1.2rem
font-weight: 300
width:600px
word-wrap: break-word
color: rgba(255,255,255,0.6)
#track
font-size: 4rem
font-weight: 700
padding-right: 20px
"""
timeString: (time) ->
if time < 60
return 'Now Playing'
if (time /= 60) < 1.5
return 'A minute ago'
if time < 59.5
return Math.round(time, 0) + ' minutes ago'
if (time /= 60) < 1.5
return 'An hour ago'
if time < 23.5
return Math.round(time, 0) + ' hours ago'
if (time /= 24) < 1.5
return 'An hour ago'
if time < 29.5
return Math.round(time, 0) + ' days ago'
if (time /= 30) < 1.5
return 'A month ago'
if time < 11.5
return Math.round(time, 0) + ' months ago'
return 'A long, long time ago'
autoSizeText: (el) ->
while true
quitme = no
fontSize = parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue('font-size'))
lineHeight = parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("line-height"));
divHeight = el.offsetHeight
if fontSize < 25
$(el).css
'height': lineHeight
return
else
$(el).css
'font-size': (fontSize - 5) + "px"
break unless divHeight > lineHeight
# ---
# generated by js2coffee 2.1.0