| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # Written by Jessica Hamrick (C) 2010 |
|---|
| 4 | |
|---|
| 5 | use strict; |
|---|
| 6 | use warnings; |
|---|
| 7 | |
|---|
| 8 | use Net::CUPS; |
|---|
| 9 | use Net::CUPS::Destination; |
|---|
| 10 | use Getopt::Long; |
|---|
| 11 | use Image::ExifTool qw(ImageInfo); |
|---|
| 12 | |
|---|
| 13 | my $usage = "Usage: gbr QUEUE FILES\n"; |
|---|
| 14 | |
|---|
| 15 | my $q = ""; |
|---|
| 16 | GetOptions ('q|queue=s' => \$q); |
|---|
| 17 | |
|---|
| 18 | my @files = @ARGV[0 .. $#ARGV]; |
|---|
| 19 | |
|---|
| 20 | if (!$q){ |
|---|
| 21 | $q = "DEFAULT"; |
|---|
| 22 | } |
|---|
| 23 | if (!@files) { |
|---|
| 24 | print $usage; |
|---|
| 25 | exit 1 |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
|---|
| 29 | if (! -e $configpath) { |
|---|
| 30 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
|---|
| 31 | exit 1; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | my ($host, $queue); |
|---|
| 35 | |
|---|
| 36 | if (-r $configpath) { |
|---|
| 37 | local $/; |
|---|
| 38 | my $fh; |
|---|
| 39 | open $fh, $configpath; |
|---|
| 40 | eval <$fh>; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | my $cups = Net::CUPS->new(); |
|---|
| 44 | $cups->setServer("$host"); |
|---|
| 45 | my $printer = $cups->getDestination("$queue"); |
|---|
| 46 | unless( $printer){ |
|---|
| 47 | print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n"; |
|---|
| 48 | exit 1; |
|---|
| 49 | } |
|---|
| 50 | my ($jobid, $title); |
|---|
| 51 | |
|---|
| 52 | foreach my $file(@files) { |
|---|
| 53 | if ($file =~ m|http://www\.youtube\.com/watch\?v=|) { |
|---|
| 54 | open FILE, ">", "/tmp/gutenbach-youtube\n" or die "Couldn't create temporary file"; |
|---|
| 55 | print FILE $file; |
|---|
| 56 | $title = $file; |
|---|
| 57 | $file = "/tmp/gutenbach-youtube"; |
|---|
| 58 | $printer->addOption("copies", 42); |
|---|
| 59 | } |
|---|
| 60 | else { |
|---|
| 61 | my $fileinfo = ImageInfo($file); |
|---|
| 62 | my $magic = $fileinfo->{FileType}; |
|---|
| 63 | |
|---|
| 64 | if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) { |
|---|
| 65 | $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'}; |
|---|
| 66 | } |
|---|
| 67 | else { |
|---|
| 68 | $title = $file; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | $jobid = $printer->printFile($file, $title); |
|---|
| 73 | |
|---|
| 74 | if ($jobid) { |
|---|
| 75 | print "Sent job '$title' (id $jobid)\n"; |
|---|
| 76 | } |
|---|
| 77 | else { |
|---|
| 78 | print "Error sending job '$title'\n"; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|