source: client/bin/gbr @ addd785

debianmacno-cups
Last change on this file since addd785 was addd785, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago
Change '-rrandom' to '-sshuffle'
  • Property mode set to 100755
File size: 4.2 KB
Line 
1#!/usr/bin/perl
2
3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
5
6use strict;
7use warnings;
8
9use Net::CUPS;
10use Net::CUPS::Destination;
11use Getopt::Long;
12use Image::ExifTool qw(ImageInfo);
13use List::Util 'shuffle';
14
15# the usage for this script
16my $usage = <<USAGE;
17Usage: gbr [options] [-q QUEUE] FILES
18
19        -q, --queue             Specify a queue other than the default
20        -d, --dryrun            Just list what would be done
21        -r, --shuffle           Randomize the order that the songs are queued in
22        -h, --help              Print this message
23USAGE
24
25# initialize the variables for the options
26my $q = "";
27my $dryrun = 0;
28my $help = 0;
29my $shuffle = 0;
30
31# parse the options
32GetOptions ('q|queue=s' => \$q,
33            'd|dryrun' => \$dryrun,
34            'h|help' => \$help,
35            's|shuffle' => \$shuffle);
36
37# if the -h flag was passed, then print the usage and exit
38if ($help) {
39    print $usage;
40    exit 0;
41}
42
43# get the files to print from the arguments
44my @files = @ARGV[0 .. $#ARGV];
45
46# if the shuffle flag was passed, then shuffle the order of the files
47if ($shuffle) {
48    @files = shuffle(@files);
49}
50
51# if the -q option is not specified, then assume we're using the
52# default queue
53if (!$q){
54    $q = "DEFAULT";
55}
56
57# if there are no files specified to print, then show the usage,
58# because the user is Doing It Wrong
59if (!@files) {
60    print $usage;
61    exit 1
62}
63
64# set configuration path, and complain if it doesn't exist
65my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
66if (! -e $configpath) {
67    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
68    exit 1;
69}
70
71# initialize the host and queue variables: host holds the address for
72# the machine on which the remote queue runs, and queue holds the name
73# of the printer
74my ($host, $queue);
75
76# load the configuration file (this will set $host and $queue)
77if (-r $configpath) {
78    local $/;
79    my $fh;
80    open $fh, $configpath;
81    eval <$fh>;
82}
83
84# initialize a new CUPS session
85my $cups = Net::CUPS->new();
86# set the server to the one specified in the config file
87$cups->setServer("$host");
88# set the printer name to the one specified in the config file
89my $printer = $cups->getDestination("$queue");
90
91# if $printer is not defined, then throw an error
92unless( $printer){
93    print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n";
94    exit 1;
95}
96
97# initialize the job id and title variables for use below
98my ($jobid, $title);
99
100# for each file that the user wants to print
101foreach my $file(@files) {
102
103    # check to see if the file is a youtube video.  If it is, then
104    # write the URL to a temporary file, and set the number of copies
105    # on the print job to 42 (this is the dirty hack we have in place
106    # to indicate that the job is a youtube file instead of a normal
107    # file)
108    if ($file =~ m|http://www\.youtube\.com/watch\?v=|) {
109        open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file";
110        print FILE $file;
111        $title = $file;
112        $file = "/tmp/gutenbach-youtube";
113        $printer->addOption("copies", 42);
114    }
115
116    # otherwise, we assume it's a normal file.  Try to use exiftool's
117    # ImageInfo to find out the tag information about the file (i.e.,
118    # title, artist, and album).  If you can, then rename the job to
119    # reflect those tags.  Otherwise, keep the normal title.
120    else {
121        my $fileinfo = ImageInfo($file);
122        my $magic = $fileinfo->{FileType};
123
124        if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) {
125            $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'};
126        }
127        else {
128            $title = $file;
129        }
130    }
131
132    # unless it's a dry run, send the print job, given the file and
133    # the job title
134    unless ($dryrun) {
135        $jobid = $printer->printFile($file, $title);
136
137        # if the printFile command returned a job id, then print that out
138        # for the user to see
139        if ($jobid) {
140            print "Sent job '$title' (id $jobid)\n";
141        }
142
143        # otherwise, let them know that an error occurred
144        else {
145            print "Error sending job '$title'\n";
146        }
147
148    # otherwise, just print what we would do
149    } else {
150        print "Would send file '$file' with title '$title'\n";
151    }   
152}
Note: See TracBrowser for help on using the repository browser.