daily pastebin goal
68%
SHARE
TWEET

stewed frog

a guest Jan 9th, 2013 166 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use LWP;
  2. use JSON;
  3. #use URI::Escape;
  4. use Modern::Perl;
  5. use Data::Dumper;
  6. my $UA = LWP::UserAgent->new;
  7. $UA->env_proxy;
  8. my $LONGEST = { path => [] };
  9. my %ECHONEST_UNKNOWN;
  10. my %PATHS_EXPLORED;
  11.  
  12. main: {
  13.     explore(@ARGV);
  14.     say "The longest path seen was $LONGEST->{path}[0]{name} to $LONGEST->{path}[-1]{name} with length " . @{ $LONGEST->{path}};
  15. }
  16.  
  17. sub explore {
  18.     my ( $start, $end ) = @_;
  19.     my @artists = get_similar($end,30);
  20.     say "Got " . ( scalar @artists ) . " artists like $end from last.fm";
  21.     my %frogs;
  22.     for $a (@artists) {
  23.         $ECHONEST_UNKNOWN{$a} and next;
  24.         my $frog= get_frog( $start, $a ) or next;
  25.         $frogs{"$start->$a"} = $frog;
  26.     }
  27.     if( keys %frogs == 0 ) {
  28.         warn "$start leads nowhere (no paths lead to the top 15 similar artists)\n";
  29.     }
  30.     else {
  31.         for (reverse sort { scalar @{ $a->{path} } <=> scalar @{ $b->{path} }} values %frogs ) {
  32.             if( not $LONGEST or @{ $_->{path} } >= ( @{ $LONGEST->{path} } - 1) ) {
  33.                 # interestingly long enough.  Investigate the reverse
  34.                 say "Path from $_->{path}[0]{name} to $_->{path}[-1]{name} has length " . @{ $_->{path} };
  35.                 @{ $_->{path} } >  @{ $LONGEST->{path} } and $LONGEST = $_;
  36.                 explore( $_->{path}[-1]{name}, $_->{path}[0]{name} );
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42. sub get_similar {
  43.     my ($name,$how_many) = @_;
  44.     my @like;
  45.     my @artists;
  46.     my $page = 0;
  47.     while( @artists < $how_many ) {
  48.         $page++;
  49.         push @artists,
  50.           ( $UA->get("http://www.last.fm/music/$name/+similar?page=$page")->content =~
  51.               m{class="remove-bottom-margin">([^<]+)}g );
  52.     }
  53.     s/&amp;/&/g for @artists; # not seeing any other html entities
  54.     return @artists[0..($how_many-1)];
  55. }
  56.  
  57. sub get_frog {
  58.     my ( $start, $end ) = @_;
  59.     # $referer = "http://static.echonest.com/frog/?src=$start&dest=$end"
  60.     $end =  URI::Escape::uri_escape($end);
  61.     $start = URI::Escape::uri_escape($start);
  62.     $PATHS_EXPLORED{"$start->$end"}++ > 2 and return;
  63.     $PATHS_EXPLORED{"$end->$start"}++ > 2 and return;
  64.     my $url = "http://labs.echonest.com/ArtistGraphServer/find_path?start=$start&end=$end";
  65.     my $content = $UA->get($url)->content;
  66.     my $frog = eval { from_json( $content ) };
  67.     $@ and die "$!\n$url\n$content\n";
  68.     if( $frog->{status} =~ /cant find/ ) {
  69.         warn qq{echonest doesn't know about "$a" yet.\n};
  70.        $ECHONEST_UNKNOWN{$a}++;
  71.        return
  72.    }
  73.    return $frog;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top