forked from fxb/javascript-last.fm-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.js
executable file
·139 lines (125 loc) · 3.38 KB
/
lastfm.js
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
var api_key = '';
var api_secret = '';
lastfmSession = undefined;
userName = undefined;
scrobbleOn = false;
$.cookie.json = true;
function logError(code, message) {
console.log('Error ('+code+'): '+message);
}
function initLastfm() {
/* Create a LastFM object */
lastfm = new LastFM({
apiKey : api_key,
apiSecret : api_secret
});
}
function initToken() {
lastfm.auth.getToken({
success: function(data) {
var win = window.open('https://www.last.fm/api/auth/?api_key='+api_key+'&token='+data.token, "Connect LastFM", "width=1000");
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
initSession(data.token);
}
}, 1000);
},
error: function(code, message) {
logError(code, message);
}
});
}
function initSession(authToken) {
if (typeof $.cookie('lastfm-session') != 'undefined') {
setSession($.cookie('lastfm-session'));
}
else if (typeof authToken != 'undefined') {
lastfm.auth.getSession({token: authToken}, {
success: function(data) {
setSession(data.session);
},
error: function(code, message) {
logError(code, message);
}
});
}
}
function setSession(session) {
lastfmSession = session;
$.cookie('lastfm-session', session, {
expires: 365
});
}
function toggleScrobble(set) {
$.cookie('lastfm-scrobble', set);
scrobbleOn = set;
$('#toggleScrobble').prop('checked', set);
$('#toggleScrobble').button('refresh');
if (set) {
// Initialise session
if (typeof lastfmSession == 'undefined') {
initToken();
}
}
}
function timestamp() {
return Math.floor(Date.now() / 1000);
}
function nowPlaying(artist, track, duration) {
if (scrobbleOn) {
lastfm.track.updateNowPlaying({
artist: artist,
track: track,
duration: duration
},
lastfmSession,
{
success: function(data) {
console.log(data);
},
error: function(code, message) {
logError(code, message);
}
}
);
} else{
logError(0, 'Scrobbling disabled');
}
}
function scrobble(artist, track, timestamp) {
if (scrobbleOn) {
lastfm.track.scrobble({
artist: artist,
track: track,
timestamp: timestamp,
chosenByUser: 0,
},
lastfmSession,
{
success: function(data) {
console.log(data);
},
error: function(code, message) {
logError(code, message);
}
}
);
} else{
logError(0, 'Scrobbling disabled');
}
}
$(function() {
$('#toggleScrobble').prop('checked', $.cookie('lastfm-scrobble'));
scrobbleOn = $.cookie('lastfm-scrobble');
$('label[for="toggleScrobble"]').text('');
$('#toggleScrobble').button().click(function() {
if ($(this).is(':checked')) {
toggleScrobble(true);
} else{
toggleScrobble(false);
}
});
initLastfm();
initSession();
});