Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Perl version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perl version
Authored by: lar3ry on Aug 13, '10 03:23:39PM
The bash script used a lot of utility calls inefficiently. Here's an equivalent Perl script, which should be more efficient, easier to maintain, and a bit more rugged in case Last.fm changes the format of their tag output:
#! /usr/bin/perl -w

sub osascript($);

%INFO = ();

sub osascript($)
{
    my ($script) = @_;
    local $_;

    my @lines = split(/[\r\n]/, $script);
    $script = '';
    foreach (@lines) {
        $script .= "echo " . quotemeta($_) . ';';
    }

    open CMD,"($script) | osascript|"
        or die "Cannot run osascript: $^E";

    my $output = <CMD>;
    close(CMD) or die "Error processing osascript: $^E";
    $output =~ tr /\r\n//d if (defined($output));

    return $output;
}
    
$artist = osascript (<<EOF);
tell application "iTunes"
    get artist of current track
end tell
EOF

print "Artist is $artist\n";

open CMD, "wget -O - -q 'http://last.fm/music/$artist/+tags'|"
    or die "wget failed: $^E";

while (<CMD>) {
    tr /\r\n//;

    $size = undef;
    $tag = undef;

    if (/font-size:\s*(\d+)px;/) {
        $size = $1;
    }
    if (m@rel="tag">([^<]+)</a>@) {
        $tag = $1;
    }
    if (defined($size) && defined($tag)) {
        $INFO{$tag} = $size;
    }
}

@tags = sort { $INFO{$b} <=> $INFO{$a} } keys(%INFO);
@tags = splice @tags,0,3;

$tags = '"' . join('" & return & return & "', @tags) . '"';

foreach (@tags) {
    print "$_\n";
}

osascript(<<EOF);
set tags to $tags
tell application "iTunes"
    set genre of current track to tags
end tell
EOF

Edited on Aug 13, '10 03:27:01PM by lar3ry


[ Reply to This | # ]
Perl version
Authored by: dfbills on Sep 08, '10 07:52:41AM

lar3ry: that was a quick re-write. Unfortunately, I'm getting an error that I can't seem to resolve:

Semicolon seems to be missing at ./set_tags.sh line 65.
syntax error at ./set_tags.sh line 66, near "EOF

osascript"
Can't find string terminator "EOF" anywhere before EOF at ./set_tags.sh line 66.

---
-d



[ Reply to This | # ]
Perl version
Authored by: lar3ry on Sep 08, '10 11:03:03AM

Since the script works (I just copied it, saved it, and ran it), here are a couple of suggestions:

1. Make sure you have the "#! /usr/bin/perl" line in line 1 of your file. Otherwise, it will be interpreted as a shell script, not a Perl program.

2. Try typing "perl -cw set_tag.sh" and see if you get the same message. Otherwise, the script isn't being run as a Perl program (and refer to step 1). If you do get the same message, then make sure that the final line in your file contains "EOF" and nothing else (no leading blanks, etc.).

lar3ry



[ Reply to This | # ]
Perl version
Authored by: dfbills on Sep 09, '10 06:21:38AM
lar3ry: Thanks, buy still no dice, even when running from Perl. I'm running Perl v5.10.0 on 10.6.4.

$ perl -cw set_tags.sh
Can't find string terminator "EOF" anywhere before EOF at set_tags.sh line 65.
I've posted a link to my actual shell app below: http://bit.ly/9AKQPs
---
-d
Edited on Sep 09, '10 06:27:46AM by dfbills


[ Reply to This | # ]