-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrobbler.js
95 lines (88 loc) · 3.11 KB
/
scrobbler.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
/* Copyright (c) 2011 Srikanth Raju, http://srikanthraju.in/scrobbleware
* Released under MIT license. See LICENSE file for more information */
var song_details = {};
var cache = LastFMCache();
var lastfm = new LastFM({
apiKey: 'e2ab4f19dcc05968add2c22efd69cf53',
apiSecret: 'a67ac7517161be33a08b473c64800ab3',
cache : cache
});
var session;
function auth()
{
console.log( localStorage.username );
console.log( localStorage.password );
if( localStorage.username != null && localStorage.password != null )
{
lastfm.auth.getMobileSession( { username:localStorage.username, password:localStorage.password }, { success: function( data ) {
console.log( data.session );
session = data.session;
localStorage.auth_success = 1;
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {});
});
listenForSongs();
}, error: function( data ) {
localStorage.auth_success = 2;
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {});
});
}
} );
}
else
{
console.log( "Need to enter credentials in options page" );
}
}
function scrobble()
{
var now_time = parseInt(new Date().getTime() / 1000.0);
if( song_details.title && song_details.title != "" &&
song_details.artist && song_details.artist != "" &&
song_details.album && song_details.album != "" && session != null
&& now_time - song_details.start_time > 30 )
{
lastfm.track.scrobble( { track:song_details.title,
artist:song_details.artist, album:song_details.album,
timestamp:song_details.start_time }, session,
{ success: function( data ){ playingstatus(); } }
);
}
}
function playingstatus()
{
if( session != null )
lastfm.track.updateNowPlaying( { track:song_details.title,
artist:song_details.artist,
album:song_details.album }, session );
}
function listenForSongs() {
chrome.extension.onRequest.addListener(
function( request, sender, sendResponse ) {
switch( request.type )
{
case "playing_song":
scrobble();//Scrobble previous song if any
//Verify new song
//Update infos and set last.fm playing status
console.log( request );
song_details.title = request.title;
song_details.album = request.album;
song_details.artist = request.artist;
song_details.start_time = parseInt(new Date().getTime() / 1000.0);
break;
case "stopped_song":
song_details = {};
break;
}
sendResponse({});
}
);
chrome.tabs.onRemoved.addListener( function( ) {
//scrobble();
}
);
}
console.log( "Done loading extension" );
auth();//Auth on startup