Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add Deezer.com Connector #41

Merged
merged 7 commits into from

2 participants

@damienalexandre

Here is my contribution, adding a deezer.com connector. It's a very popular streaming service in Europe.

The main difficulty here is that the song duration can appear a long time after the song starts to play. I'm using a timer and I send the song only when the duration is ok.

I also use the title change event to know when a new song is played.

@david-sabata david-sabata merged commit 679867c into david-sabata:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2012
  1. @damienalexandre
  2. @damienalexandre

    Some cleaning

    damienalexandre authored
  3. @damienalexandre
  4. @damienalexandre
  5. @damienalexandre

    Missing debug line

    damienalexandre authored
Commits on Mar 6, 2012
  1. @damienalexandre

    Add a fix on the song duration and late scrobble.

    damienalexandre authored
    You can have a case when the song has changed, but as we are waiting for the new duration to warn the extension,
    the old song is scrobbled. So we call reset() and that should do the trick :)
  2. @damienalexandre

    Add a todo

    damienalexandre authored
This page is out of date. Refresh to see the latest.
Showing with 143 additions and 8 deletions.
  1. +129 −0 deezer.js
  2. +10 −6 manifest.json
  3. +4 −2 options.html
View
129 deezer.js
@@ -0,0 +1,129 @@
+/**
+ * Chrome-Last.fm-Scrobbler Deezer.com Connector by @damienalexandre
+ *
+ * v1.0, 5 march 2012
+ *
+ * The difficulty here is that the song duration can appear a long time after the
+ * song starts playing.
+ * We use the title change to know when a song is played.
+ *
+ * @todo Handle the song "pause"? (do we have to cancel the scrobble?)
+ * @todo Improve the way we deal with the first played song. I have made it lazy for perf purpose.
+ */
+
+var currentDeezerTimeout = null;
+
+$(document).ready(function() {
+
+ $("title").bind('DOMSubtreeModified', function()
+ {
+ cancel(); // In case we switch song before the scrobble (as the duration is async, we may warn the extension too late)
+
+ if (currentDeezerTimeout) // Handle song fast zapping
+ {
+ window.clearTimeout(currentDeezerTimeout);
+ }
+
+ currentDeezerTimeout = window.setTimeout(sendTrack, 1000); // As the duration may be not available.
+ });
+
+ sendTrack(); // We maybe have a song playing right away. There is no retry if this call fails.
+
+ $(window).unload(function()
+ {
+ cancel();
+ return true;
+ });
+});
+
+/**
+ * Handle the chrome.extension
+ * and can retry itself too.
+ */
+function sendTrack()
+{
+ if (currentDeezerTimeout)
+ {
+ window.clearTimeout(currentDeezerTimeout);
+ }
+
+ var deezerSong = getCurrentTrack();
+
+ if (deezerSong && deezerSong.duration > 0)
+ {
+ chrome.extension.sendRequest({type: 'validate', artist: deezerSong.artist, track: deezerSong.track}, function(response)
+ {
+ if (response != false)
+ {
+ var song = response; // contains valid artist/track now
+
+ chrome.extension.sendRequest({type: 'nowPlaying', artist: song.artist, track: song.track, duration: deezerSong.duration});
+ }
+ else
+ {
+ chrome.extension.sendRequest({type: 'nowPlaying', duration: deezerSong.duration});
+ displayMsg('Not recognized');
+ }
+ });
+ }
+ else if (currentDeezerTimeout)
+ {
+ // Retry to fetch the song infos later
+ currentDeezerTimeout = window.setTimeout(sendTrack, 1000);
+ }
+}
+
+/**
+ * Try to get the song infos (track, artist, duration)
+ * @return object|boolean
+ */
+function getCurrentTrack()
+{
+ if ($('#h_play').is(":hidden")) { // Play button hidden, the song is playing
+ return {
+ track: $('#current-track').html(),
+ artist: $('#current-artist').html(),
+ duration: parseDuration($('#end-track').html())
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Binded on the unload
+ */
+function cancel()
+{
+ // reset the background scrobbler song data
+ chrome.extension.sendRequest({type: 'reset'});
+}
+
+/**
+ * Maybe this kind of common method should be in the Core
+ *
+ * @param durationString Like "13;37".
+ * @return int The duration string translated to seconds
+ */
+function parseDuration(durationString)
+{
+ try
+ {
+ var match = durationString.match(/\d+:\d+/g);
+
+ if (match)
+ {
+ var mins = match[0].substring(0, match[0].indexOf(':'));
+ var seconds = match[0].substring(match[0].indexOf(':')+1);
+ return parseInt(mins*60, 10) + parseInt(seconds, 10);
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ catch(err)
+ {
+ return 0;
+ }
+}
View
16 manifest.json
@@ -1,27 +1,27 @@
{
"name": "Last.fm Scrobbler",
"version": "1.7",
-
- "description": "Scrobble music all around the web!",
+
+ "description": "Scrobble music all around the web!",
"icons": {
"128": "icon128.png"
},
-
+
"background_page": "background.html",
"options_page": "options.html",
-
+
"permissions": [
"tabs",
"notifications",
"http://ws.audioscrobbler.com/2.0/",
"http://gdata.youtube.com/feeds/api/videos/"
],
-
+
"page_action": {
"chromeBroken": "remove this line after issue #86449 is resolved"
},
- "content_scripts": [
+ "content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["dummy.js"]
@@ -75,6 +75,10 @@
"matches": ["*://www.pandora.com/*"],
"js": ["jquery-1.6.1.min.js", "jquery.dump.js", "pandora.js"],
"run_at" : "document_start"
+ },
+ {
+ "matches": ["*://www.deezer.com/*"],
+ "js": ["jquery-1.6.1.min.js", "jquery.dump.js", "deezer.js"]
}
]
}
View
6 options.html
@@ -47,7 +47,8 @@
<li>FFtunes.com</li>
<li>Fizy.com</li>
<li>BandCamp.com</li>
- <li>Pandora.com (new interface)</li>
+ <li>Pandora.com (new interface)</li>
+ <li>Deezer.com</li>
</ul>
</p>
<p>
@@ -80,7 +81,8 @@
<li><a href="https://github.com/moski">Moski Doski</a> - Thesixtyone.com support</li>
<li><a href="http://last.fm/user/Macint/">Peter McEvoy</a> - <a href="http://ghostly.com/discovery/play">Ghostly Discovery</a> support</li>
<li><a href="https://github.com/porges">George Pollard</a> - BandCamp support</li>
- <li><a href="http://jperr.com">Jordan Perr</a> - Pandora support</li>
+ <li><a href="http://jperr.com">Jordan Perr</a> - Pandora support</li>
+ <li><a href="http://damienalexandre.fr/">Damien Alexandre</a> - Deezer support</li>
</ul>
</p>
</div>
Something went wrong with that request. Please try again.