#!/usr/bin/perl use strict; use lib "/home/jason/lib"; # use MIME::Lite; my $NOMAIL = ($ARGV[0] eq "-t" || $ARGV[0] eq "-nomail") ? 1 : 0; my $MAIL = $NOMAIL ? 0 : 1; die <<"EOB" if grep($_ eq "-h", @ARGV); upload.prl [-h] [-nomail] -h Help message. -nomail Print out emails but don't send them. EOB # --------------------------------- # Get list of photos we've done. # --------------------------------- my %DONE; open(my $fh, ") { next if /^\s*(#|$)/; s/\s*$//; $DONE{$_}++; } close($fh); # ---------------------------------------- # Go through upload list one at a time. # ---------------------------------------- open(my $fh, ") { $line++; next if /^#/; s/\s*$//; # Start of picture section. if (/^(\d+) (\d+) (\S.*\S)/) { my ($date, $pnum, $loc) = ($1, $2, $3); print "Got $date $pnum $loc\n"; # Skip if we've already done. next if $DONE{"$date/$pnum.jpg"}; # Read rest of info on it. my @taxa; my $comment; my $error; while (<$fh>) { $line++; next if /^#/; s/\s*$//; if (!s/^ //) { last; } elsif (/^(\d+) (\S+) (\S+) \((\w+)\)/) { my ($nnum, $tax, $com, $sure) = ($1, $2, $3, $4); push @taxa, [$nnum, $tax, $com, $sure]; } elsif (/^"(.*)"/) { $comment = $1; } else { $error = 1; print STDERR "Bad syntax on line $line of upload.txt.\n"; } } next if $error; # Upload it and record fact that we did so. if ($MAIL) { print STDERR "Uploading $date/$pnum.jpg...\n"; } if (upload($date, $pnum, $loc, $comment, \@taxa)) { system("echo $date/$pnum.jpg >> upload.done"); } else { print STDERR "Failed!!!\n"; } if ($MAIL) { print STDERR "Sleeping...\n"; sleep(60*2.5); } } } close($fh); exit 0; # ---------------------------- # Email in one photo. # ---------------------------- sub upload { my ($date, $pnum, $loc, $comment, $taxa) = @_; my %tags; my $tags; my @body; my $title; my $i = 1; foreach (@$taxa) { my ($nnum, $tax, $com, $sure) = @$_; $tax =~ s/_/ /g; $com =~ s/_/ /g; $sure =~ s/_/ /g; my $tax2 = $tax; $tax2 =~ s/<.i>.*?/ /g; $tax2 =~ s/^//; $tax2 =~ s/<.i>.*$//; $tags .= " $tax2"; $tags .= " $com" if $com ne "-"; $tax =~ s/<.i> / /g; if ($tax =~ /^[a-z]/) { $com = $tax; $com =~ s/\b([a-z])/uc($1)/eg; $com =~ s/\b(and|in|o|of|or|s|the)\b/lc($1)/egi; $tax = "unknown"; } $com = "unknown" if $com !~ /\w/; if (@$taxa == 1) { $title = $com eq "unknown" ? $tax : $com; } elsif (@$taxa == 2) { $title .= " and " if $i > 1; $title .= $com eq "unknown" ? $tax : $com; } elsif ($i == 1) { $title = $com eq "unknown" ? $tax : $com; $title .= ", etc."; } push @body, "Scientific Name: $tax\n"; push @body, "Common Name: $com\n"; push @body, "Certainty: $sure ". "(notes)\n"; push @body, "\n" if @$taxa > 1; $i++; } push @body, "Location: $loc\n"; push @body, "Date: $date\n"; push @body, "\n$comment\n" if $comment ne ""; unshift @body, "Tags: ", join(" ", grep(/\w/ && !$tags{$_}++, split(" ", $tags))), "\n"; if ($NOMAIL) { print "PHOTO: $date/$pnum.jpg\n"; print "SUBJ: $title\n"; print "TO: differ65fear\@photos.flickr.com\n"; print @body; print "\n"; } else { my $email = MIME::Lite->new( From => "jasonphollinger\@yahoo.com", To => "differ65fear\@photos.flickr.com", Subject => $title, Type => "multipart/mixed", ); $email->attach( Type => "TEXT", Data => join("", @body), ); $email->attach( Type => "image/jpeg", Path => "$date/$pnum.jpg", Filename => "${date}_${pnum}.jpg", Disposition => "attachment", ); $email->send; } return 1; }