-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.pl
executable file
·95 lines (76 loc) · 2.49 KB
/
bot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
# perl bot.pl [options] datafile username
# See the README for more information
use FindBin;
use lib "$FindBin::Bin";
use CoverArtBot;
use LWP::Simple;
use Getopt::Long;
use File::Which;
my $note = "";
my $max = 100;
my $tmpdir = "/tmp/";
my $password = '';
my $remove_note = "";
my $verbose = 0;
my $use_front = 0;
GetOptions('note|n=s' => \$note, 'max|m=i' => \$max, 'tmpdir|t=s' => \$tmpdir, 'password|p=s' => \$password, 'remove-note|r=s' => \$remove_note, 'verbose|v' => \$verbose, 'use-front' => \$use_front);
my $file = shift @ARGV or die "Must provide a filename";
my $username = shift @ARGV or die "Must provide a username";
my @mbids = ();
open FILE, $file or die "Couldn't open the data file ($file)";
while (<FILE>) {
chomp;
my ($mbid, $url, $types, $comment, $rel) = split /\t/;
push @mbids, { 'mbid' => $mbid, 'url' => $url, 'rel' => $rel, 'types' => $types, 'comment' => $comment };
}
close FILE;
if (!$password) {
system "stty -echo";
print "Password for $username: ";
$password = <>;
system "stty echo";
print "\n";
}
my $bot = CoverArtBot->new({username => $username, password => $password, note => $note, remove_note => $remove_note, verbose => $verbose, use_front => $use_front});
my $identify_exe = which('identify');
warn "identify can't be found, install imagemagick for type checking and dimensions in notes" unless $identify_exe;
for my $l (@mbids) {
unless ($max > 0) {
print "Reached maximum number of files.\n";
last;
}
$l->{'note_args'} = {url => $l->{'url'}, mbid => $l->{'mbid'}, local => -e $l->{'url'} ? "local" : "remote"};
my $precheck_ok = $bot->precheck($l);
if ($precheck_ok) {
my $filename = -e $l->{'url'} ? $l->{'url'} : fetch_image($l->{'url'}, $l->{'mbid'});
if (!$filename) {
my $urlname = $l->{'url'};
print STDERR "Failed to fetch $urlname.\n";
next;
}
if ($identify_exe) {
my $info = `$identify_exe "$filename"`;
my ($xdim, $ydim) = $info =~ / JPEG ([0-9]+)x([0-9]+)/;
if (!$xdim || !$ydim) {
print STDERR "Image is not a JPEG, or dimensions can't be found.\n";
next;
}
$l->{'note_args'}->{'x_dim'} = $xdim;
$l->{'note_args'}->{'y_dim'} = $ydim;
$l->{'note_args'}->{'identify_output'} = $info;
}
my $rv = $bot->run($l, $filename);
$max -= $rv;
}
print "$max more image(s)...\n\n";
}
sub fetch_image {
my $url = shift;
return 0 unless $url =~ /\/([^\/]+)$/;
my $filename = $tmpdir.$1;
my $r = getstore($url, "$filename");
# print "$r\n";
return 0 unless $r == "200";
return $filename;
}