spacevillain (spacevillain) wrote in changelog,
spacevillain
spacevillain
changelog

[livejournal] r23418: LJSUP-14457: [Update.bml] Last.fm music ...

Committer: atyurin
LJSUP-14457: [Update.bml] Last.fm music detection is not working
U   trunk/htdocs/js/lastfm.js
Modified: trunk/htdocs/js/lastfm.js
===================================================================
--- trunk/htdocs/js/lastfm.js	2012-12-05 10:20:54 UTC (rev 23417)
+++ trunk/htdocs/js/lastfm.js	2012-12-05 11:42:01 UTC (rev 23418)
@@ -1,5 +1,38 @@
 LJ.LastFM = {
     /*
+     * @param {Object} data Last.fm response
+     * @return {Object|null} Parsed object with "artist" and "title"
+     */
+    _parse: function(data) {
+        'use strict';
+
+        var tracks = data.recenttracks,
+            last = tracks && tracks.track[0],
+            date = null,
+            justListened = false;
+
+        if (last.name && last.artist && (last.artist.name || last.artist['#text'])) {
+
+            if (last.date) {
+                date = +new Date(Number(last.date.uts) * 1000),
+                justListened = +new Date() - date < 300000;
+            }
+
+            if ((last['@attr'] && last['@attr'].nowplaying) || justListened) {
+                return {
+                    artist: last.artist.name || last.artist['#text'],
+                    title: last.name,
+                    _: last
+                };
+            } else {
+                return null;
+            }
+        } else {
+            throw new Error('Data error');
+        }
+    },
+
+    /*
      * Get current playing (or just listened) track in last.fm
      * http://www.last.fm/api/show/user.getRecentTracks
      * @param {String} user LastFM username
@@ -8,6 +41,8 @@
     getNowPlaying: function(user, callback) {
         'use strict';
 
+        var self = this;
+
         jQuery.ajax({
             url: 'http://ws.audioscrobbler.com/2.0/',
             dataType: 'json',
@@ -24,30 +59,10 @@
                 return;
             }
 
-            var tracks = res.recenttracks,
-                last = tracks && tracks.track[0],
-                nowPlaying = null,
-                date = null,
-                justListened = false;
-
-            if (last.name && last.artist && last.artist.name) {
-
-                if (last.date) {
-                    date = +new Date(Number(last.date.uts) * 1000),
-                    justListened = +new Date() - date < 300000;
-                }
-
-                if ((last['@attr'] && last['@attr'].nowplaying) || justListened) {
-                    nowPlaying = {
-                        artist: last.artist.name,
-                        name: last.name,
-                        _: last
-                    };
-                }
-            }
-
             if (callback) {
-                callback(nowPlaying);
+                callback(
+                    self._parse(res)
+                );
             }
         });
     }
@@ -56,26 +71,43 @@
 function lastfm_current ( username, show_error ) {
     'use strict';
 
-    var user = Site.page.last_fm_user;
+    var user = Site.page.last_fm_user,
+        label = null,
+        input = document.getElementById('prop_current_music'),
+        spinner = 'b-updatepage-field-music-loading';
+
     if (!user) {
         console.error('No last.fm user');
         return;
     }
 
-    var input = document.getElementById('prop_current_music');
+    if (Site.page.ljpost) {
+        label = jQuery('.b-updatepage-field-music');
+        label.toggleClass(spinner, true);
+        input.value = '';
+    } else {
+        input.value = 'Loading...';
+    }
 
-    input.value = 'Loading...';
     LJ.LastFM.getNowPlaying(user, function(track) {
         if (track) {
-            input.value = '{artist} - {name} | Powered by Last.fm'.supplant(track);
+            input.value = '{artist} - {title} | Powered by Last.fm'.supplant(track);
         } else {
             input.value = '';
         }
+
+        if (label) {
+            label.toggleClass(spinner, false);
+        }
     });
 }
 
 if (Site.page.ljpost) {
     jQuery(function() {
-        lastfm_current();
+        'use strict';
+
+        if (Site.page.last_fm_user) {
+            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