#!/usr/bin/perl -w # Copyright © 2005-2011 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Does a Google Image Search of the iTunes currently-selected band/album # pairs (for finding album artwork). # # Created: 6-Dec-2005. require 5; use diagnostics; use strict; my $progname = $0; $progname =~ s@.*/@@g; my $version = q{ $Revision: 1.6 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; sub url_quote($) { my ($s) = @_; $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge; return $s; } sub get_itunes() { my @script = ('tell application "iTunes"', ' set output to ""', ' repeat with the_track in selection as list', ' tell the_track', ' set output to output & ' . 'artist & "\t" & album & "\t" & name & "\n"', ' end tell', ' end repeat', 'end tell'); my $cmd = "osascript -e '" . join ("' -e '", @script) . "'"; my $result = `$cmd`; error ("failed to get selected track from itunes") unless ($result =~ m/\n.*\n/s); if ($verbose) { my $x = $result; $x =~ s/^/$progname: /gm; print STDERR "$progname: selected:\n$x\n"; } return split ("\n", $result); } # Do a search for artist+album in Google Images. # sub google_album_art($$) { my ($artist, $album) = @_; $artist =~ s/"/ /gs; $album =~ s/"/ /gs; my $search = "\"$artist\" \"$album\""; # $search .= " site:discogs.com"; $search = url_quote ($search); my $url = ('http://images.google.com/images' . '?svnum=100' . '&safe=off' . '&q=' . $search); print STDERR "$progname: search: $url\n" if ($verbose); system ("open", $url); } # Do a search where the summary of the results is likely to include the # year in which the selected track was released. # sub google_track_year($$) { my ($artist, $track) = @_; $artist =~ s/"/ /gs; $track =~ s/"/ /gs; $artist =~ s/^The //si; my $search = "site:discogs.com released genre \"$artist\" \"$track\""; #$search = "\"$artist\" \"$track\""; $search = url_quote ($search); my $url = 'http://www.google.com/search?q=' . $search; print STDERR "$progname: search: $url\n" if ($verbose); system ("open", $url); } # Search for just the track name. # sub google_track_name($) { my ($track) = @_; $track =~ s@\s+\(.*?\)@@g; $track =~ s/"/ /gs; my $search = url_quote("song \"$track\""); my $url = 'http://www.google.com/search?q=' . $search; print STDERR "$progname: search: $url\n" if ($verbose); system ("open", $url); } sub google_itunes($) { my ($mode) = @_; my @tracks = get_itunes(); my %done; foreach (@tracks) { my ($artist, $album, $track) = split ("\t", $_); my $key = ($mode eq 'year' ? "$artist $track" : $mode eq 'name' ? $track : "$artist $album"); next if (defined ($done{$key})); $done{$key} = 1; if ($mode eq 'year') { google_track_year ($artist, $track); } elsif ($mode eq 'name') { google_track_name ($key); } else { google_album_art ($artist, $album); } } } sub error($) { my ($err) = @_; print STDERR "$progname: $err\n"; exit 1; } sub usage() { print STDERR "usage: $progname [--year | --name] [--verbose]\n"; exit 1; } sub main() { my $mode = 'art'; while ($#ARGV >= 0) { $_ = shift @ARGV; if ($_ eq "--verbose") { $verbose++; } elsif (m/^-v+$/) { $verbose += length($_)-1; } elsif (m/^--?year$/) { $mode = 'year'; } elsif (m/^--?name$/) { $mode = 'name'; } elsif (m/^-./) { usage; } else { usage; } } google_itunes ($mode); } main(); exit 0;