-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jyntran/dev
v1.0
- Loading branch information
Showing
17 changed files
with
536 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Jensen Tran | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# NhacCuaTui Scrobbler | ||
|
||
![NhacCuaTui Scrobbler screenshot](screenshot.png) | ||
|
||
Chrome extension that scrobbles music from [NhacCuaTui.com](http://www.nhaccuatui.com) to [Last.fm](http://www.last.fm) | ||
- Adds current track to Last.fm profile under Scrobbling Now | ||
- Scrobbles after the halfway mark on a song | ||
- Please keep only one open tab of playing music on NhacCuaTui | ||
|
||
Feel free to contribute or post issues to the [GitHub repository](https://github.com/jyntran/nhaccuatui-scrobbler). | ||
|
||
## User Installation | ||
|
||
1. Download the .crx here: http://jyntran.ca/chrome/nhaccuatui-scrobbler.crx | ||
2. Open Chrome and go to `chrome://extensions` | ||
3. Drag and drop the .crx into Chrome | ||
4. Accept permissions to install the extension | ||
5. Click on the extension button, then click Log In | ||
|
||
## Note for Developers | ||
|
||
- You will need a Last.fm API key and secret to run it: http://www.last.fm/api | ||
- Place your key and secret in `api/config.js`. The file `config.sample.js` is provided as a template. | ||
|
||
## License | ||
|
||
[MIT](license.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* config.js */ | ||
|
||
var CONFIG = { | ||
api_key: '', | ||
api_secret: '', | ||
api_url: 'http://ws.audioscrobbler.com/2.0/', | ||
cb_file: 'cb.html' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* lastfm.js */ | ||
|
||
var lastfm = { | ||
_sendRequest: this._sendRequest, | ||
_getSignature: this._getSignature, | ||
authorize: this.authorize, | ||
getSession: this.getSession, | ||
scrobble: this.scrobble, | ||
updateNowPlaying: this.updateNowPlaying, | ||
login: this.login, | ||
logout: this.logout, | ||
session: {} | ||
} | ||
|
||
function _sendRequest(method, params, callback) { | ||
var uri = CONFIG.api_url; | ||
var _data = ""; | ||
var _params = []; | ||
|
||
for (param in params) { | ||
_params.push(encodeURIComponent(param) + "=" | ||
+ encodeURIComponent(params[param])); | ||
} | ||
|
||
switch (method) { | ||
case "GET": | ||
uri += '?' + _params.join('&').replace(/%20/, '+'); | ||
break; | ||
case "POST": | ||
_data = _params.join('&'); | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
$.ajax({ | ||
type: method, | ||
url: uri, | ||
data: _data, | ||
dataType: 'json', | ||
headers: { | ||
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8", | ||
"If-Modified-Since": "Thu, 01 Jun 1970 00:00:00 GMT", | ||
"Pragma": "no-cache" | ||
}, | ||
success: function(data) { | ||
callback(data); | ||
}, | ||
error: function(err) { | ||
console.log(err); | ||
} | ||
}); | ||
} | ||
|
||
function scrobble(artist, track, timestamp, callback) { | ||
var params = { | ||
api_key: CONFIG.api_key, | ||
sk: lastfm.session.key, | ||
method: 'track.scrobble', | ||
artist: artist, | ||
track: track, | ||
timestamp: timestamp | ||
}; | ||
|
||
params.api_sig = _getSignature(params); | ||
params.format = 'json'; | ||
|
||
_sendRequest('POST', params, function(resp) { | ||
callback(resp); | ||
}); | ||
} | ||
|
||
function updateNowPlaying(artist, track, callback) { | ||
var params = { | ||
api_key: CONFIG.api_key, | ||
sk: lastfm.session.key, | ||
method: 'track.updateNowPlaying', | ||
artist: artist, | ||
track: track | ||
}; | ||
|
||
params.api_sig = _getSignature(params); | ||
params.format = 'json'; | ||
|
||
_sendRequest('POST', params, function(resp) { | ||
callback(resp); | ||
}); | ||
} | ||
|
||
function _getSignature(params) { | ||
var keys = []; | ||
var key, i; | ||
var sig = ''; | ||
|
||
for (key in params) { | ||
keys.push(key); | ||
} | ||
|
||
for (i in keys.sort()) { | ||
key = keys[i]; | ||
sig += key + params[key]; | ||
} | ||
|
||
sig += CONFIG.api_secret; | ||
|
||
return md5(sig); | ||
} | ||
|
||
function authorize(token, callback) { | ||
var params = { | ||
api_key: CONFIG.api_key, | ||
method: 'auth.getSession', | ||
token: token | ||
}; | ||
|
||
params.api_sig = _getSignature(params); | ||
params.format = 'json'; | ||
|
||
_sendRequest('GET', params, function(resp){ | ||
if (resp) { | ||
callback(resp); | ||
} else { | ||
callback(); | ||
} | ||
}); | ||
} | ||
|
||
function getSession(token) { | ||
authorize(token, function(resp) { | ||
if (resp && resp.session) { | ||
lastfm.session = resp.session; | ||
localStorage.setItem('nctscrobble_session_key', resp.session.key); | ||
localStorage.setItem('nctscrobble_session_name', resp.session.name); | ||
} | ||
}); | ||
} | ||
|
||
function login() { | ||
var cb = chrome.runtime.getURL(CONFIG.cb_file); | ||
chrome.tabs.create({ | ||
url: 'http://www.last.fm/api/auth?api_key=' + CONFIG.api_key + '&cb=' + cb | ||
}); | ||
} | ||
|
||
function logout() { | ||
localStorage.removeItem('nctscrobble_session_key'); | ||
localStorage.removeItem('nctscrobble_session_name'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* background.js */ | ||
|
||
chrome.runtime.onMessage.addListener( | ||
function(request, sender, sendResponse) { | ||
if (request.name == 'scrobble') { | ||
var r = request.data; | ||
lastfm.scrobble(r.artist, r.track, r.timestamp, function(resp){ | ||
sendResponse({name:'scrobble response', data: resp}); | ||
}); | ||
} | ||
if (request.name == 'now-playing') { | ||
var r = request.data; | ||
lastfm.updateNowPlaying(r.artist, r.track, function(resp){ | ||
sendResponse({name:'now-playing response', data: resp}); | ||
}); | ||
} | ||
return true; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<script src="cb.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* cb.js */ | ||
|
||
function _url_param(name, url) { | ||
return unescape((RegExp(name + '=' + | ||
'(.+?)(&|$)').exec(url) || [,null])[1]); | ||
} | ||
|
||
chrome.runtime.getBackgroundPage(function(bp) { | ||
bp.lastfm.getSession(_url_param("token", location.search)); | ||
window.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* contentscript.js */ | ||
|
||
(function() { | ||
|
||
$(document).ready(function(){ | ||
var totalTimeStr, totalTime, | ||
currentTimeStr, currentTime, | ||
metadata, | ||
isScrobbled = false; | ||
|
||
function onSongPage() { | ||
return window.location.href.indexOf('nhaccuatui.com/bai-hat/') > -1; | ||
} | ||
|
||
function onPlaylistPage() { | ||
return window.location.href.indexOf('nhaccuatui.com/playlist/') > -1; | ||
} | ||
|
||
function getMetadata() { | ||
var trackName, artistName; | ||
if (onSongPage()) { | ||
var titleElem = document.getElementsByClassName('name_title')[0]; | ||
trackName = titleElem.children[0].innerText; | ||
artistName = titleElem.children[2].children[0].innerText; | ||
} else if (onPlaylistPage()) { | ||
var playerElem = document.getElementById('nameSingerflashPlayer'); | ||
trackName = playerElem.children[0].children[0].innerText; | ||
artistName = playerElem.children[1].children[0].innerText; | ||
} | ||
var metadata = { | ||
track: trackName, | ||
artist: artistName | ||
}; | ||
chrome.runtime.sendMessage({ | ||
name: "metadata", | ||
data: metadata | ||
}, function(resp) { | ||
//console.log(resp); | ||
}); | ||
return metadata; | ||
} | ||
|
||
function scrobbleTrack(data) { | ||
var obj = { | ||
track: data.track, | ||
artist: data.artist, | ||
timestamp: new Date().getTime() / 1000 | ||
}; | ||
chrome.runtime.sendMessage({ | ||
name: 'scrobble', | ||
data: obj | ||
}, function(resp) { | ||
if (!resp.data.error) { | ||
isScrobbled = true; | ||
//console.log('Success: scrobbled the following track: ' + data.artist + ' - ' + data.track); | ||
} else { | ||
//console.log('Error: could not scrobbled the following track: ' + data.artist + ' - ' + data.track); | ||
} | ||
}); | ||
} | ||
|
||
function updateNowPlaying(data) { | ||
var obj = { | ||
track: data.track, | ||
artist: data.artist | ||
}; | ||
chrome.runtime.sendMessage({ | ||
name: 'now-playing', | ||
data, obj | ||
}, function(resp) { | ||
if (!resp.data.error) { | ||
//console.log('Success: now playing following track: ' + data.artist + ' - ' + data.track); | ||
} else { | ||
//console.log('Error: could not update now-playing for the following track: ' + data.artist + ' - ' + data.track); | ||
} | ||
}); | ||
} | ||
|
||
/* https://stackoverflow.com/a/9640417 */ | ||
function hmsToSecondsOnly(str) { | ||
var p = str.split(':'), | ||
s = 0, m = 1; | ||
while (p.length > 0) { | ||
s += m * parseInt(p.pop(), 10); | ||
m *= 60; | ||
} | ||
return s; | ||
} | ||
|
||
function isHalfway(total, current) { | ||
return current >= (total/2); | ||
} | ||
|
||
function getTotalTime() { | ||
if ($('#utTotalTimeflashPlayer').text() != totalTimeStr) { | ||
totalTimeStr = $('#utTotalTimeflashPlayer').text(); | ||
totalTime = hmsToSecondsOnly(totalTimeStr); | ||
isScrobbled = false; | ||
metadata = getMetadata(); | ||
} | ||
} | ||
|
||
function checkCurrentTime() { | ||
getTotalTime(); | ||
metadata = getMetadata(); | ||
updateNowPlaying(metadata); | ||
currentTimeStr = $('#utCurrentTimeflashPlayer').text(); | ||
currentTime = hmsToSecondsOnly(currentTimeStr); | ||
if (!isScrobbled && isHalfway(totalTime, currentTime)) { | ||
scrobbleTrack(metadata); | ||
} else | ||
if (isScrobbled) { | ||
// TODO: prevent message from being sent multiple times afterward | ||
chrome.runtime.sendMessage({ | ||
name: 'scrobbled', | ||
data: metadata | ||
}, function(resp) { | ||
//console.log(resp); | ||
}); | ||
} | ||
} | ||
|
||
$('#playerMp3flashPlayer').ready(function(){ | ||
$('body').on('DOMSubtreeModified', '#utCurrentTimeflashPlayer', checkCurrentTime); | ||
}); | ||
}); | ||
|
||
})(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.