mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-03-31 05:22 am

[dw-free] Remove last.fm music detection

[commit: http://hg.dwscoalition.org/dw-free/rev/b069deabc426]

http://bugs.dwscoalition.org/show_bug.cgi?id=539

Remove last.fm music detection.

Patch by [personal profile] pauamma.

Files modified:
  • bin/upgrading/en.dat
  • bin/worker/lastfm-current-track
  • cgi-bin/LJ/LastFM.pm
  • cgi-bin/weblib.pl
  • htdocs/js/lastfm.js
  • htdocs/manage/profile/index.bml
  • htdocs/manage/profile/index.bml.text
  • htdocs/tools/endpoints/lastfm_current_track.bml
--------------------------------------------------------------------------------
diff -r 29fc0126315a -r b069deabc426 bin/upgrading/en.dat
--- a/bin/upgrading/en.dat	Tue Mar 31 05:15:00 2009 +0000
+++ b/bin/upgrading/en.dat	Tue Mar 31 05:22:00 2009 +0000
@@ -1223,8 +1223,6 @@ entryform.mood.noneother=None, or other:
 entryform.mood.noneother=None, or other:
 
 entryform.music=Music:
-
-entryform.music.detect=Detect
 
 entryform.nojstime.note=<b>Note:</b> The time/date above is from our server.  Correct them for your timezone before posting.
 
diff -r 29fc0126315a -r b069deabc426 bin/worker/lastfm-current-track
--- a/bin/worker/lastfm-current-track	Tue Mar 31 05:15:00 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-
-use LJ::Worker::Gearman;
-use LJ::LastFM;
-use Storable qw(freeze thaw); # safe across updating when use with simple structures only
-use JSON;
-
-gearman_decl('get_current_track' => \&get_current_track);
-gearman_work(save_result => 1);
-
-# get user's current last.fm track
-sub get_current_track {
-    my $job = shift;
-    my $arg = $job->arg; # restricted to be a scalar
-    my ( $username ) = @{thaw($arg)};
-
-    return JSON::objToJson( LJ::LastFM::current($username) );
-}
-
-
-1;
diff -r 29fc0126315a -r b069deabc426 cgi-bin/LJ/LastFM.pm
--- a/cgi-bin/LJ/LastFM.pm	Tue Mar 31 05:15:00 2009 +0000
+++ b/cgi-bin/LJ/LastFM.pm	Tue Mar 31 05:22:00 2009 +0000
@@ -4,131 +4,12 @@ package LJ::LastFM;
 package LJ::LastFM;
 
 use strict;
+use warnings;
 
-use LWP::UserAgent;
-use HTML::Entities;
-use XML::Parser;
-use Encode;
-
-# Get current track
-sub current {
-    my $username = LJ::eurl(shift);
-
-    return { error => "Missing last.fm credentials" }
-        unless $LJ::LAST_FM_API_KEY && $LJ::LAST_FM_API_URL;
-
-    my $ua = LJ::get_useragent( role=>'last_fm', timeout=>$LJ::LAST_FM_TIMEOUT );
-    my $url = "$LJ::LAST_FM_API_URL&api_key=$LJ::LAST_FM_API_KEY&user=$username";
-    my $response = $ua->get($url);
-    unless ($response->is_success) {
-        warn "Can't get data from last.fm: " . $response->status_line;
-        return { error => "Can't retrieve data from last.fm" };
-    }
-    
-    my $content = $response->content();
-
-    # process xml
-    my $in_current_play_tag = 0;    # if we are inside track tag with nowplaying attribute
-    my $current_tag = '';           # a name of a current tag
-
-    # error
-    my $error_code  = 0;
-    my $error_message = '';
-
-    # now plaing song attributes
-    my $artist = '';
-    my $name = '';
-
-    # Handlers.
-    # on tag start:
-    my $handler_start = sub {
-        my $expat = shift;
-        my $element = shift;
-        my %attr = @_;
-
-        # catch tag 'track' with 'nowplaying=true'
-        if ($element eq "track"
-            && exists($attr{'nowplaying'}) && $attr{'nowplaying'} eq "true") {
-            $in_current_play_tag = 1;
-            return;
-        }
-
-        $error_code = $attr{'code'}
-            if ($element eq "error" && exists($attr{'code'}));
-
-        # for all other tags just remember name
-        $current_tag = $element;
-    };
-
-    # on tag end:
-    my $handler_end = sub {
-        my $expat = shift;
-        my $element = shift;
-        my %attr = @_;
-
-        # if we leave 'track' tag
-        if ($element eq "track") {
-            $in_current_play_tag = 0;
-        }
-
-        # forget a name of a current tag
-        $current_tag = '';
-    };
-
-    # inside a tag:
-    my $handler_char = sub {
-        my $expat = shift;
-        my $string = shift;
-
-        # 'error'
-        if ($current_tag eq 'error') {
-            $error_message = $string;
-            return;
-        }
-
-        # pay attention only on current playing tracks
-        return unless($in_current_play_tag);
-
-        # remember song attributes
-        if ($current_tag eq "artist") {
-            $artist = $string;
-            return;
-        }
-
-        if ($current_tag eq "name") {
-            $name = $string;
-            return;
-        }
-    };
-
-    my $parser = new XML::Parser(Handlers => {
-            Start => $handler_start,
-            End   => $handler_end,
-            Char  => $handler_char,
-        });
-    eval { $parser->parse($content); };
-    if ($@) { # invalid xml
-        return { error => "Can't retrieve data from last.fm: wrong response from server" };
-    }
-
-    if ($error_message) {
-        return { error => "Can't retrieve data from last.fm: $error_message" };
-    }
-
-    # This prevents worker from die when it catch unicode characters in last.fm title.
-    # (turn off UTF-8 flags from text strings)
-    ($artist, $name) = map { Encode::is_utf8($_) ? Encode::encode("utf8", $_) : $_ } ($artist, $name);
-
-    if ($artist || $name) {
-        my $track = HTML::Entities::decode(
-            ($artist ? $artist : 'Unknown artist') . ' - ' . ($name ? $name : 'Unknown track' ) . $LJ::LAST_FM_SIGN_TEXT
-        );
-        return { data => $track };
-    } else {
-        return { error => 'No "now listening" track in last.fm data' };
-    }
-}
-
+# FIXME: this can be simplified, if not completely removed, but it's used in
+# LJ::S2, talkread.bml, and talkpost.bml, all of which are (I think) having
+# surgery of their own, so leaving it as is until someone can look at how they
+# use it in more detail.
 sub format_current_music_string {
     my $string = shift;
 
@@ -159,5 +40,4 @@ sub format_current_music_string {
     return $string;
 }
 
-
 1;
diff -r 29fc0126315a -r b069deabc426 cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl	Tue Mar 31 05:15:00 2009 +0000
+++ b/cgi-bin/weblib.pl	Tue Mar 31 05:22:00 2009 +0000
@@ -1453,19 +1453,17 @@ MOODS
             $out .= "</span>\n";
             $out .= "</p>\n";
 
-            my $show_lastfm = 0;
-            $show_lastfm = 1 if $opts->{'prop_last_fm_user'};
-
-            my @width_for_lastfm = ();
-            @width_for_lastfm = ('style' => 'width: 45%') if $show_lastfm;
-
             # Current Location
             $out .= "<p class='pkg'>";
-            unless ($LJ::DISABLED{'web_current_location'}) {
+            if ( LJ::is_enabled( 'web_current_location' ) ) {
                 $out .= "<span class='inputgroup-left'>";
-                $out .= "<label for='prop_current_location' class='left options'>" . BML::ml('entryform.location') . "</label>";
-                $out .= LJ::html_text({ 'name' => 'prop_current_location', 'value' => $opts->{'prop_current_location'}, 'id' => 'prop_current_location',
-                                        'class' => 'text', 'size' => '35', 'maxlength' => '60', 'tabindex' => $tabindex->(), @width_for_lastfm }) . "\n";
+                $out .= "<label for='prop_current_location' class='left options'>" . BML::ml( 'entryform.location' ) . "</label>";
+                $out .= LJ::html_text({ name => 'prop_current_location',
+                                        value => $opts->{prop_current_location},
+                                        id => 'prop_current_location',
+                                        class => 'text', size => '35',
+                                        maxlength => '60',
+                                        tabindex => $tabindex->() }) . "\n";
                 $out .= "</span>";
             }
 
@@ -1485,33 +1483,17 @@ MOODS
             $out .= "</span>\n";
             $out .= "</p>\n";
 
-            ### Insert last.fm functions
-            if ($show_lastfm) {
-                LJ::need_res(qw(
-                    js/lastfm.js
-                    js/jobstatus.js
-                ));
-            }
-
             # Current Music
             $out .= "<p class='pkg'>\n";
             $out .= "<span class='inputgroup-left'>\n";
             $out .= "<label for='prop_current_music' class='left options'>" . BML::ml('entryform.music') . "</label>\n";
             # BML::ml('entryform.music')
-            $out .= LJ::html_text({ 'name' => 'prop_current_music', 'value' => $opts->{'prop_current_music'}, 'id' => 'prop_current_music',
-                                    'class' => 'text', 'size' => '35', 'maxlength' => LJ::std_max_length(), 'tabindex' => $tabindex->(), @width_for_lastfm }) . "\n";
-            if ($show_lastfm) {
-                my $last_fm_user = LJ::ejs($opts->{'prop_last_fm_user'});
-                my $button_label = BML::ml('entryform.music.detect');
-                $out .= qq[<input type="button" value="$button_label" style="float: left" onclick="lastfm_current('$last_fm_user', true);">];
-
-                $out .= LJ::help_icon_html("lastfm", "", " ");
-
-                # automatically detect current music only if creating new entry
-                $out .= qq[<script>
-                    lastfm_current('$last_fm_user', false);
-                </script>] if $opts->{mode} eq 'update';
-            }
+            $out .= LJ::html_text({ name => 'prop_current_music',
+                                    value => $opts->{prop_current_music},
+                                    id => 'prop_current_music',
+                                    class => 'text', size => '35',
+                                    maxlength => LJ::std_max_length(),
+                                    tabindex => $tabindex->() }) . "\n";
             $out .= "</span>\n";
             $out .= "<span class='inputgroup-right'>";
 
diff -r 29fc0126315a -r b069deabc426 htdocs/js/lastfm.js
--- a/htdocs/js/lastfm.js	Tue Mar 31 05:15:00 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,65 +0,0 @@
-function lastfm_current ( username, show_error ) {
-    document.getElementById('prop_current_music').value = show_error ? "Running, please wait..." : "";
-
-    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);
-};
-
-var jobstatus;
-var timer;
-
-function import_handle(info, show_error) {
-    if (info.error) {
-        document.getElementById('prop_current_music').value = info.error;
-        return import_error(info.error);
-    }
-
-    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.');
-    }
-
-    done = 0; // If data already received or not
-};
-
-function got_track (info, show_error) {
-    if (info.running) {
-    } else {
-        window.clearInterval(timer);
-
-        if (info.status == "success") {
-            if (done)
-                return;
-
-            done = 1;
-
-            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;
-            }
-        } else {
-            document.getElementById('prop_current_music').value = '';
-            if (show_error) {
-                LiveJournal.ajaxError('Failed to receive track from Last.fm.');
-            }
-        }
-    }
-}
-
-function import_error(msg) {
-    LiveJournal.ajaxError(msg);
-}
-
diff -r 29fc0126315a -r b069deabc426 htdocs/manage/profile/index.bml
--- a/htdocs/manage/profile/index.bml	Tue Mar 31 05:15:00 2009 +0000
+++ b/htdocs/manage/profile/index.bml	Tue Mar 31 05:22:00 2009 +0000
@@ -408,7 +408,7 @@ body<=
         # last_fm_user
         $ret .= "<tr><td class='field_name'>$ML{'.fn.last_fm'}</td><td>";
         $ret .= LJ::html_text({ 'name' => 'last_fm_user', 'value' => $u->{'last_fm_user'}, 'size' => '20', 'maxlength' => '255' });
-        $ret .= "<div class='helper'>$ML{'.fn.last_fm.desc'}</div></td></tr>";
+        $ret .= "</td></tr>";
         $ret .= "</td></tr>\n";
 
         } # end is_person check
diff -r 29fc0126315a -r b069deabc426 htdocs/manage/profile/index.bml.text
--- a/htdocs/manage/profile/index.bml.text	Tue Mar 31 05:15:00 2009 +0000
+++ b/htdocs/manage/profile/index.bml.text	Tue Mar 31 05:22:00 2009 +0000
@@ -112,8 +112,6 @@
 .fn.interests=List all your interests, separated by commas, to allow other users to find you using the Interest Search.
 
 .fn.last_fm=Last.fm username 
-
-.fn.last_fm.desc=Enter your last.fm username here and you will be able to automatically fill in Music field with scrobbled music when posting new entry.
 
 .fn.link=Link
 
diff -r 29fc0126315a -r b069deabc426 htdocs/tools/endpoints/lastfm_current_track.bml
--- a/htdocs/tools/endpoints/lastfm_current_track.bml	Tue Mar 31 05:15:00 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-<?_code # -*-bml-*-
-{
-    use strict;
-    use vars qw(%POST);
-    use JSON;
-    use Storable qw(freeze thaw); # safe across updating when use with simple structures only
-    use Gearman::Client;
-
-    my $username = $POST{'username'};
-
-    my $client = new Gearman::Client;
-    $client->job_servers( @LJ::GEARMAN_SERVERS );
-
-    my $arg = freeze( [ $username ] );
-    my $task = Gearman::Task->new('get_current_track', \$arg);
-
-    my $handle = $client->dispatch_background($task);
-
-    return JSON::objToJson({ handle => $handle });
-}
-_code?>
--------------------------------------------------------------------------------