[fe74c7c] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
[6b7441a] | 3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
---|
| 4 | # help from Kyle Brogle (broglek) |
---|
[fe74c7c] | 5 | |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
[b58aada] | 9 | use Net::CUPS; |
---|
| 10 | use Net::CUPS::Destination; |
---|
[7cdd65d] | 11 | use Getopt::Long; |
---|
[b58aada] | 12 | use Image::ExifTool qw(ImageInfo); |
---|
| 13 | |
---|
[38388ac] | 14 | my $usage = "Usage: gbr QUEUE FILES\n"; |
---|
[fe74c7c] | 15 | |
---|
[7cdd65d] | 16 | my $q = ""; |
---|
| 17 | GetOptions ('q|queue=s' => \$q); |
---|
[fe74c7c] | 18 | |
---|
[7cdd65d] | 19 | my @files = @ARGV[0 .. $#ARGV]; |
---|
| 20 | |
---|
[6b7441a] | 21 | # if the -q option is not specified, then assume we're using the |
---|
| 22 | # default queue |
---|
[7cdd65d] | 23 | if (!$q){ |
---|
| 24 | $q = "DEFAULT"; |
---|
| 25 | } |
---|
[6b7441a] | 26 | |
---|
| 27 | # if there are no files specified to print, then show the usage, |
---|
| 28 | # because the user is Doing It Wrong |
---|
[7cdd65d] | 29 | if (!@files) { |
---|
[fe74c7c] | 30 | print $usage; |
---|
| 31 | exit 1 |
---|
| 32 | } |
---|
| 33 | |
---|
[6b7441a] | 34 | # set configuration path, and complain if it doesn't exist |
---|
[fe74c7c] | 35 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
---|
[85a1ac1] | 36 | if (! -e $configpath) { |
---|
[0d02eca] | 37 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
---|
[85a1ac1] | 38 | exit 1; |
---|
| 39 | } |
---|
| 40 | |
---|
[6b7441a] | 41 | # initialize the host and queue variables: host holds the address for |
---|
| 42 | # the machine on which the remote queue runs, and queue holds the name |
---|
| 43 | # of the printer |
---|
[fe74c7c] | 44 | my ($host, $queue); |
---|
| 45 | |
---|
[6b7441a] | 46 | # load the configuration file (this will set $host and $queue) |
---|
[fe74c7c] | 47 | if (-r $configpath) { |
---|
| 48 | local $/; |
---|
| 49 | my $fh; |
---|
| 50 | open $fh, $configpath; |
---|
| 51 | eval <$fh>; |
---|
| 52 | } |
---|
| 53 | |
---|
[6b7441a] | 54 | # initialize a new CUPS session |
---|
[b58aada] | 55 | my $cups = Net::CUPS->new(); |
---|
[6b7441a] | 56 | # set the server to the one specified in the config file |
---|
[b58aada] | 57 | $cups->setServer("$host"); |
---|
[6b7441a] | 58 | # set the printer name to the one specified in the config file |
---|
[b58aada] | 59 | my $printer = $cups->getDestination("$queue"); |
---|
[6b7441a] | 60 | |
---|
| 61 | # if $printer is not defined, then throw an error |
---|
[9fdf4a1] | 62 | unless( $printer){ |
---|
| 63 | print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n"; |
---|
| 64 | exit 1; |
---|
| 65 | } |
---|
[6b7441a] | 66 | |
---|
| 67 | # initialize the job id and title variables for use below |
---|
[b58aada] | 68 | my ($jobid, $title); |
---|
[c5a98db] | 69 | |
---|
[6b7441a] | 70 | # for each file that the user wants to print |
---|
[38388ac] | 71 | foreach my $file(@files) { |
---|
[6b7441a] | 72 | |
---|
| 73 | # check to see if the file is a youtube video. If it is, then |
---|
| 74 | # write the URL to a temporary file, and set the number of copies |
---|
| 75 | # on the print job to 42 (this is the dirty hack we have in place |
---|
| 76 | # to indicate that the job is a youtube file instead of a normal |
---|
| 77 | # file) |
---|
[38388ac] | 78 | if ($file =~ m|http://www\.youtube\.com/watch\?v=|) { |
---|
[600e713] | 79 | open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file"; |
---|
[b58aada] | 80 | print FILE $file; |
---|
| 81 | $title = $file; |
---|
| 82 | $file = "/tmp/gutenbach-youtube"; |
---|
| 83 | $printer->addOption("copies", 42); |
---|
[38388ac] | 84 | } |
---|
[6b7441a] | 85 | |
---|
| 86 | # otherwise, we assume it's a normal file. Try to use exiftool's |
---|
| 87 | # ImageInfo to find out the tag information about the file (i.e., |
---|
| 88 | # title, artist, and album). If you can, then rename the job to |
---|
| 89 | # reflect those tags. Otherwise, keep the normal title. |
---|
[38388ac] | 90 | else { |
---|
[b58aada] | 91 | my $fileinfo = ImageInfo($file); |
---|
| 92 | my $magic = $fileinfo->{FileType}; |
---|
| 93 | |
---|
[9fdf4a1] | 94 | if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) { |
---|
[b58aada] | 95 | $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'}; |
---|
| 96 | } |
---|
| 97 | else { |
---|
| 98 | $title = $file; |
---|
[38388ac] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[6b7441a] | 102 | # send the print job, given the file and the job title |
---|
[b58aada] | 103 | $jobid = $printer->printFile($file, $title); |
---|
| 104 | |
---|
[6b7441a] | 105 | # if the printFile command returned a job id, then print that out |
---|
| 106 | # for the user to see |
---|
[b58aada] | 107 | if ($jobid) { |
---|
| 108 | print "Sent job '$title' (id $jobid)\n"; |
---|
| 109 | } |
---|
[6b7441a] | 110 | |
---|
| 111 | # otherwise, let them know that an error occurred |
---|
[b58aada] | 112 | else { |
---|
| 113 | print "Error sending job '$title'\n"; |
---|
| 114 | } |
---|
[c5a98db] | 115 | } |
---|