1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
---|
4 | # help from Kyle Brogle (broglek) |
---|
5 | |
---|
6 | use strict; |
---|
7 | use warnings; |
---|
8 | |
---|
9 | use Net::CUPS; |
---|
10 | use Net::CUPS::Destination; |
---|
11 | use Getopt::Long; |
---|
12 | use Image::ExifTool qw(ImageInfo); |
---|
13 | |
---|
14 | my $usage = "Usage: gbr QUEUE FILES\n"; |
---|
15 | |
---|
16 | my $q = ""; |
---|
17 | GetOptions ('q|queue=s' => \$q); |
---|
18 | |
---|
19 | my @files = @ARGV[0 .. $#ARGV]; |
---|
20 | |
---|
21 | # if the -q option is not specified, then assume we're using the |
---|
22 | # default queue |
---|
23 | if (!$q){ |
---|
24 | $q = "DEFAULT"; |
---|
25 | } |
---|
26 | |
---|
27 | # if there are no files specified to print, then show the usage, |
---|
28 | # because the user is Doing It Wrong |
---|
29 | if (!@files) { |
---|
30 | print $usage; |
---|
31 | exit 1 |
---|
32 | } |
---|
33 | |
---|
34 | # set configuration path, and complain if it doesn't exist |
---|
35 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
---|
36 | if (! -e $configpath) { |
---|
37 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
---|
38 | exit 1; |
---|
39 | } |
---|
40 | |
---|
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 |
---|
44 | my ($host, $queue); |
---|
45 | |
---|
46 | # load the configuration file (this will set $host and $queue) |
---|
47 | if (-r $configpath) { |
---|
48 | local $/; |
---|
49 | my $fh; |
---|
50 | open $fh, $configpath; |
---|
51 | eval <$fh>; |
---|
52 | } |
---|
53 | |
---|
54 | # initialize a new CUPS session |
---|
55 | my $cups = Net::CUPS->new(); |
---|
56 | # set the server to the one specified in the config file |
---|
57 | $cups->setServer("$host"); |
---|
58 | # set the printer name to the one specified in the config file |
---|
59 | my $printer = $cups->getDestination("$queue"); |
---|
60 | |
---|
61 | # if $printer is not defined, then throw an error |
---|
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 | } |
---|
66 | |
---|
67 | # initialize the job id and title variables for use below |
---|
68 | my ($jobid, $title); |
---|
69 | |
---|
70 | # for each file that the user wants to print |
---|
71 | foreach my $file(@files) { |
---|
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) |
---|
78 | if ($file =~ m|http://www\.youtube\.com/watch\?v=|) { |
---|
79 | open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file"; |
---|
80 | print FILE $file; |
---|
81 | $title = $file; |
---|
82 | $file = "/tmp/gutenbach-youtube"; |
---|
83 | $printer->addOption("copies", 42); |
---|
84 | } |
---|
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. |
---|
90 | else { |
---|
91 | my $fileinfo = ImageInfo($file); |
---|
92 | my $magic = $fileinfo->{FileType}; |
---|
93 | |
---|
94 | if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) { |
---|
95 | $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'}; |
---|
96 | } |
---|
97 | else { |
---|
98 | $title = $file; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | # send the print job, given the file and the job title |
---|
103 | $jobid = $printer->printFile($file, $title); |
---|
104 | |
---|
105 | # if the printFile command returned a job id, then print that out |
---|
106 | # for the user to see |
---|
107 | if ($jobid) { |
---|
108 | print "Sent job '$title' (id $jobid)\n"; |
---|
109 | } |
---|
110 | |
---|
111 | # otherwise, let them know that an error occurred |
---|
112 | else { |
---|
113 | print "Error sending job '$title'\n"; |
---|
114 | } |
---|
115 | } |
---|