Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use LWP;
- use JSON;
- #use URI::Escape;
- use Modern::Perl;
- use Data::Dumper;
- my $UA = LWP::UserAgent->new;
- $UA->env_proxy;
- my $LONGEST = { path => [] };
- my %ECHONEST_UNKNOWN;
- my %PATHS_EXPLORED;
- main: {
- explore(@ARGV);
- say "The longest path seen was $LONGEST->{path}[0]{name} to $LONGEST->{path}[-1]{name} with length " . @{ $LONGEST->{path}};
- }
- sub explore {
- my ( $start, $end ) = @_;
- my @artists = get_similar($end,30);
- say "Got " . ( scalar @artists ) . " artists like $end from last.fm";
- my %frogs;
- for $a (@artists) {
- $ECHONEST_UNKNOWN{$a} and next;
- my $frog= get_frog( $start, $a ) or next;
- $frogs{"$start->$a"} = $frog;
- }
- if( keys %frogs == 0 ) {
- warn "$start leads nowhere (no paths lead to the top 15 similar artists)\n";
- }
- else {
- for (reverse sort { scalar @{ $a->{path} } <=> scalar @{ $b->{path} }} values %frogs ) {
- if( not $LONGEST or @{ $_->{path} } >= ( @{ $LONGEST->{path} } - 1) ) {
- # interestingly long enough. Investigate the reverse
- say "Path from $_->{path}[0]{name} to $_->{path}[-1]{name} has length " . @{ $_->{path} };
- @{ $_->{path} } > @{ $LONGEST->{path} } and $LONGEST = $_;
- explore( $_->{path}[-1]{name}, $_->{path}[0]{name} );
- }
- }
- }
- }
- sub get_similar {
- my ($name,$how_many) = @_;
- my @like;
- my @artists;
- my $page = 0;
- while( @artists < $how_many ) {
- $page++;
- push @artists,
- ( $UA->get("http://www.last.fm/music/$name/+similar?page=$page")->content =~
- m{class="remove-bottom-margin">([^<]+)}g );
- }
- s/&/&/g for @artists; # not seeing any other html entities
- return @artists[0..($how_many-1)];
- }
- sub get_frog {
- my ( $start, $end ) = @_;
- # $referer = "http://static.echonest.com/frog/?src=$start&dest=$end"
- $end = URI::Escape::uri_escape($end);
- $start = URI::Escape::uri_escape($start);
- $PATHS_EXPLORED{"$start->$end"}++ > 2 and return;
- $PATHS_EXPLORED{"$end->$start"}++ > 2 and return;
- my $url = "http://labs.echonest.com/ArtistGraphServer/find_path?start=$start&end=$end";
- my $content = $UA->get($url)->content;
- my $frog = eval { from_json( $content ) };
- $@ and die "$!\n$url\n$content\n";
- if( $frog->{status} =~ /cant find/ ) {
- warn qq{echonest doesn't know about "$a" yet.\n};
- $ECHONEST_UNKNOWN{$a}++;
- return
- }
- return $frog;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement