[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); |
---|
[a5cf43b] | 13 | use List::Util 'shuffle'; |
---|
[ca8acb7] | 14 | use File::Find; |
---|
[b58aada] | 15 | |
---|
[fc8707b] | 16 | # the usage for this script |
---|
| 17 | my $usage = <<USAGE; |
---|
| 18 | Usage: gbr [options] [-q QUEUE] FILES |
---|
[fe74c7c] | 19 | |
---|
[fc8707b] | 20 | -q, --queue Specify a queue other than the default |
---|
| 21 | -d, --dryrun Just list what would be done |
---|
[ca8acb7] | 22 | -s, --shuffle Randomize the order that the songs are queued in |
---|
| 23 | -r, --recursive Recursively find files if a directory is passed in |
---|
[43ddb7a] | 24 | -n, --number NUMBER Only print NUMBER files, if more than NUMBER are given |
---|
| 25 | (this will print the first NUMBER files if -s is not given) |
---|
[fc8707b] | 26 | -h, --help Print this message |
---|
| 27 | USAGE |
---|
| 28 | |
---|
| 29 | # initialize the variables for the options |
---|
[7cdd65d] | 30 | my $q = ""; |
---|
[fc8707b] | 31 | my $dryrun = 0; |
---|
| 32 | my $help = 0; |
---|
[addd785] | 33 | my $shuffle = 0; |
---|
[ca8acb7] | 34 | my $recursive = 0; |
---|
[43ddb7a] | 35 | my $number = 0; |
---|
[fc8707b] | 36 | |
---|
| 37 | # parse the options |
---|
| 38 | GetOptions ('q|queue=s' => \$q, |
---|
| 39 | 'd|dryrun' => \$dryrun, |
---|
[a5cf43b] | 40 | 'h|help' => \$help, |
---|
[ca8acb7] | 41 | 's|shuffle' => \$shuffle, |
---|
[43ddb7a] | 42 | 'r|recursive' => \$recursive, |
---|
| 43 | 'n|number=i' => \$number); |
---|
[fc8707b] | 44 | |
---|
| 45 | # if the -h flag was passed, then print the usage and exit |
---|
| 46 | if ($help) { |
---|
| 47 | print $usage; |
---|
| 48 | exit 0; |
---|
| 49 | } |
---|
[fe74c7c] | 50 | |
---|
[a5cf43b] | 51 | # get the files to print from the arguments |
---|
[7cdd65d] | 52 | my @files = @ARGV[0 .. $#ARGV]; |
---|
[ca8acb7] | 53 | my @allfiles; |
---|
| 54 | |
---|
| 55 | # if the recursive flag was passed, then recursively find files |
---|
| 56 | if ($recursive) { |
---|
| 57 | |
---|
| 58 | # helper function to add a file to the end of the list, but only |
---|
| 59 | # if it is a file (and not a directory) |
---|
| 60 | sub append { |
---|
[267d44e] | 61 | if (-f $_) { |
---|
| 62 | push(@allfiles, $_); |
---|
[ca8acb7] | 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
[267d44e] | 66 | # recursively find all the files and add them to @allfiles |
---|
| 67 | find({ wanted => \&append, no_chdir => 1}, @files); |
---|
[ca8acb7] | 68 | |
---|
| 69 | # if we're not shuffling the list, then sort the files. If we are |
---|
| 70 | # shuffling the list, then don't bother sorting (since we're going |
---|
| 71 | # to shuffle it anyway) |
---|
| 72 | unless ($shuffle) { |
---|
| 73 | @files = sort(@allfiles); |
---|
| 74 | } else { |
---|
| 75 | @files = @allfiles; |
---|
| 76 | } |
---|
| 77 | } |
---|
[7cdd65d] | 78 | |
---|
[addd785] | 79 | # if the shuffle flag was passed, then shuffle the order of the files |
---|
| 80 | if ($shuffle) { |
---|
[a5cf43b] | 81 | @files = shuffle(@files); |
---|
| 82 | } |
---|
| 83 | |
---|
[43ddb7a] | 84 | # if the number flag was specified, then only play the specified |
---|
| 85 | # number of files |
---|
| 86 | if ($number > 0 and $number < $#files) { |
---|
| 87 | @files = @files[0 .. $number-1] |
---|
| 88 | } |
---|
| 89 | |
---|
[6b7441a] | 90 | # if the -q option is not specified, then assume we're using the |
---|
| 91 | # default queue |
---|
[7cdd65d] | 92 | if (!$q){ |
---|
| 93 | $q = "DEFAULT"; |
---|
| 94 | } |
---|
[6b7441a] | 95 | |
---|
| 96 | # if there are no files specified to print, then show the usage, |
---|
| 97 | # because the user is Doing It Wrong |
---|
[7cdd65d] | 98 | if (!@files) { |
---|
[fe74c7c] | 99 | print $usage; |
---|
| 100 | exit 1 |
---|
| 101 | } |
---|
| 102 | |
---|
[6b7441a] | 103 | # set configuration path, and complain if it doesn't exist |
---|
[fe74c7c] | 104 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
---|
[85a1ac1] | 105 | if (! -e $configpath) { |
---|
[0d02eca] | 106 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
---|
[85a1ac1] | 107 | exit 1; |
---|
| 108 | } |
---|
| 109 | |
---|
[6b7441a] | 110 | # initialize the host and queue variables: host holds the address for |
---|
| 111 | # the machine on which the remote queue runs, and queue holds the name |
---|
| 112 | # of the printer |
---|
[fe74c7c] | 113 | my ($host, $queue); |
---|
| 114 | |
---|
[6b7441a] | 115 | # load the configuration file (this will set $host and $queue) |
---|
[fe74c7c] | 116 | if (-r $configpath) { |
---|
| 117 | local $/; |
---|
| 118 | my $fh; |
---|
| 119 | open $fh, $configpath; |
---|
| 120 | eval <$fh>; |
---|
| 121 | } |
---|
| 122 | |
---|
[6b7441a] | 123 | # initialize a new CUPS session |
---|
[b58aada] | 124 | my $cups = Net::CUPS->new(); |
---|
[6b7441a] | 125 | # set the server to the one specified in the config file |
---|
[b58aada] | 126 | $cups->setServer("$host"); |
---|
[6b7441a] | 127 | # set the printer name to the one specified in the config file |
---|
[b58aada] | 128 | my $printer = $cups->getDestination("$queue"); |
---|
[6b7441a] | 129 | |
---|
| 130 | # if $printer is not defined, then throw an error |
---|
[9fdf4a1] | 131 | unless( $printer){ |
---|
[fc8707b] | 132 | print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n"; |
---|
[9fdf4a1] | 133 | exit 1; |
---|
| 134 | } |
---|
[6b7441a] | 135 | |
---|
| 136 | # initialize the job id and title variables for use below |
---|
[b58aada] | 137 | my ($jobid, $title); |
---|
[c5a98db] | 138 | |
---|
[6b7441a] | 139 | # for each file that the user wants to print |
---|
[38388ac] | 140 | foreach my $file(@files) { |
---|
[6b7441a] | 141 | |
---|
| 142 | # check to see if the file is a youtube video. If it is, then |
---|
| 143 | # write the URL to a temporary file, and set the number of copies |
---|
| 144 | # on the print job to 42 (this is the dirty hack we have in place |
---|
| 145 | # to indicate that the job is a youtube file instead of a normal |
---|
| 146 | # file) |
---|
[38388ac] | 147 | if ($file =~ m|http://www\.youtube\.com/watch\?v=|) { |
---|
[600e713] | 148 | open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file"; |
---|
[b58aada] | 149 | print FILE $file; |
---|
| 150 | $title = $file; |
---|
| 151 | $file = "/tmp/gutenbach-youtube"; |
---|
| 152 | $printer->addOption("copies", 42); |
---|
[38388ac] | 153 | } |
---|
[6b7441a] | 154 | |
---|
| 155 | # otherwise, we assume it's a normal file. Try to use exiftool's |
---|
| 156 | # ImageInfo to find out the tag information about the file (i.e., |
---|
| 157 | # title, artist, and album). If you can, then rename the job to |
---|
| 158 | # reflect those tags. Otherwise, keep the normal title. |
---|
[38388ac] | 159 | else { |
---|
[b58aada] | 160 | my $fileinfo = ImageInfo($file); |
---|
| 161 | my $magic = $fileinfo->{FileType}; |
---|
| 162 | |
---|
[9fdf4a1] | 163 | if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) { |
---|
[b58aada] | 164 | $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'}; |
---|
| 165 | } |
---|
| 166 | else { |
---|
| 167 | $title = $file; |
---|
[38388ac] | 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
[fc8707b] | 171 | # unless it's a dry run, send the print job, given the file and |
---|
| 172 | # the job title |
---|
| 173 | unless ($dryrun) { |
---|
| 174 | $jobid = $printer->printFile($file, $title); |
---|
[6b7441a] | 175 | |
---|
[fc8707b] | 176 | # if the printFile command returned a job id, then print that out |
---|
| 177 | # for the user to see |
---|
| 178 | if ($jobid) { |
---|
| 179 | print "Sent job '$title' (id $jobid)\n"; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | # otherwise, let them know that an error occurred |
---|
| 183 | else { |
---|
| 184 | print "Error sending job '$title'\n"; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | # otherwise, just print what we would do |
---|
| 188 | } else { |
---|
| 189 | print "Would send file '$file' with title '$title'\n"; |
---|
| 190 | } |
---|
[c5a98db] | 191 | } |
---|