spacevillain (spacevillain) wrote in changelog,
spacevillain
spacevillain
changelog

[livejournal] r23041: LJSUP-13713: Last.fm works incorrect

Committer: atyurin
LJSUP-13713: Last.fm works incorrect
U   trunk/htdocs/js/lastfm.js
Modified: trunk/htdocs/js/lastfm.js
===================================================================
--- trunk/htdocs/js/lastfm.js	2012-10-03 10:08:18 UTC (rev 23040)
+++ trunk/htdocs/js/lastfm.js	2012-10-03 12:00:54 UTC (rev 23041)
@@ -1,65 +1,70 @@
-function lastfm_current ( username, show_error ) {
-    document.getElementById('prop_current_music').value = show_error ? "Running, please wait..." : "";
+LJ.LastFM = {
+    /*
+     * Get current playing track in last.fm
+     * http://www.last.fm/api/show/user.getRecentTracks
+     * @param {String} user LastFM username
+     * @param {Function(Object)} callback Argument is the current track, see last.fm API
+     */
+    getNowPlaying: function(user, callback) {
+        'use strict';
 
-    var req = { method : "POST", 
-        data : HTTPReq.formEncoded({ "username" : username }),
-        url : "/tools/endpoints/lastfm_current_track.bml",
-        onData : function (info) { import_handle(info, show_error) },
-        onError : import_error
-    };
-    HTTPReq.getJSON(req);
-};
+        jQuery.ajax({
+            url: 'http://ws.audioscrobbler.com/2.0/',
+            dataType: 'json',
+            cache: false,
+            data: {
+                method: 'user.getrecenttracks',
+                user: user,
+                api_key: 'b25b959554ed76058ac220b7b2e0a026',
+                format: 'json'
+            }
+        }).done(function(res) {
+            var tracks = res.recenttracks,
+                last = tracks && tracks.track[0],
+                nowPlaying = null;
 
-var jobstatus;
-var timer;
+            if (last.name && last.artist && last.artist.name) {
+                var date = +new Date(Number(last.date.uts) * 1000),
+                    justListened = +new Date() - date < 300000;
 
-function import_handle(info, show_error) {
-    if (info.error) {
-        document.getElementById('prop_current_music').value = info.error;
-        return import_error(info.error);
-    }
+                if ((last['@attr'] && last['@attr'].nowplaying) || justListened) {
+                    nowPlaying = {
+                        artist: last.artist.name,
+                        name: last.name,
+                        _: last
+                    };
+                }
+            }
 
-    if (info.handle) {
-        jobstatus = new JobStatus(info.handle, function (info) { got_track(info, show_error) } );
-        timer = window.setInterval(jobstatus.updateStatus.bind(jobstatus), 1500);
-    } else if (show_error) {
-        document.getElementById('prop_current_music').value = "TODO: Gearman no job. Please run";
-        import_error('TODO: Gearman no job. Please run.');
+            if (callback) {
+                callback(nowPlaying);
+            }
+        });
     }
-
-    done = 0; // If data already received or not
 };
 
-function got_track (info, show_error) {
-    if (info.running) {
-    } else {
-        window.clearInterval(timer);
+function lastfm_current ( username, show_error ) {
+    'use strict';
 
-        if (info.status == "success") {
-            if (done)
-                return;
+    var user = Site.page.last_fm_user;
+    if (!user) {
+        return;
+    }
 
-            done = 1;
+    var input = document.getElementById('prop_current_music');
 
-            eval('var result = ' + info.result);
-            if (result.error) {
-                document.getElementById('prop_current_music').value = '';
-                if (show_error) {
-                    LiveJournal.ajaxError(result.error);
-                }
-            } else {
-                document.getElementById('prop_current_music').value = result.data;
-            }
+    input.value = 'Loading...';
+    LJ.LastFM.getNowPlaying(user, function(track) {
+        if (track) {
+            input.value = '{artist} - {name} | Powered by Last.fm'.supplant(track);
         } else {
-            document.getElementById('prop_current_music').value = '';
-            if (show_error) {
-                LiveJournal.ajaxError('Failed to receive track from Last.fm.');
-            }
+            input.value = '';
         }
-    }
+    });
 }
 
-function import_error(msg) {
-    LiveJournal.ajaxError(msg);
-}
-
+if (Site.page.ljpost) {
+    jQuery(function() {
+        lastfm_current();
+    });
+}
\ No newline at end of file

Tags: atyurin, js, livejournal, spacevillain
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments