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
RevLine 
[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
6use strict;
7use warnings;
8
[b58aada]9use Net::CUPS;
10use Net::CUPS::Destination;
[7cdd65d]11use Getopt::Long;
[b58aada]12use Image::ExifTool qw(ImageInfo);
[a5cf43b]13use List::Util 'shuffle';
[b58aada]14
[fc8707b]15# the usage for this script
16my $usage = <<USAGE;
17Usage: gbr [options] [-q QUEUE] FILES
[fe74c7c]18
[fc8707b]19        -q, --queue             Specify a queue other than the default
20        -d, --dryrun            Just list what would be done
[addd785]21        -r, --shuffle           Randomize the order that the songs are queued in
[fc8707b]22        -h, --help              Print this message
23USAGE
24
25# initialize the variables for the options
[7cdd65d]26my $q = "";
[fc8707b]27my $dryrun = 0;
28my $help = 0;
[addd785]29my $shuffle = 0;
[fc8707b]30
31# parse the options
32GetOptions ('q|queue=s' => \$q,
33            'd|dryrun' => \$dryrun,
[a5cf43b]34            'h|help' => \$help,
[addd785]35            's|shuffle' => \$shuffle);
[fc8707b]36
37# if the -h flag was passed, then print the usage and exit
38if ($help) {
39    print $usage;
40    exit 0;
41}
[fe74c7c]42
[a5cf43b]43# get the files to print from the arguments
[7cdd65d]44my @files = @ARGV[0 .. $#ARGV];
45
[addd785]46# if the shuffle flag was passed, then shuffle the order of the files
47if ($shuffle) {
[a5cf43b]48    @files = shuffle(@files);
49}
50
[6b7441a]51# if the -q option is not specified, then assume we're using the
52# default queue
[7cdd65d]53if (!$q){
54    $q = "DEFAULT";
55}
[6b7441a]56
57# if there are no files specified to print, then show the usage,
58# because the user is Doing It Wrong
[7cdd65d]59if (!@files) {
[fe74c7c]60    print $usage;
61    exit 1
62}
63
[6b7441a]64# set configuration path, and complain if it doesn't exist
[fe74c7c]65my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
[85a1ac1]66if (! -e $configpath) {
[0d02eca]67    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
[85a1ac1]68    exit 1;
69}
70
[6b7441a]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
[fe74c7c]74my ($host, $queue);
75
[6b7441a]76# load the configuration file (this will set $host and $queue)
[fe74c7c]77if (-r $configpath) {
78    local $/;
79    my $fh;
80    open $fh, $configpath;
81    eval <$fh>;
82}
83
[6b7441a]84# initialize a new CUPS session
[b58aada]85my $cups = Net::CUPS->new();
[6b7441a]86# set the server to the one specified in the config file
[b58aada]87$cups->setServer("$host");
[6b7441a]88# set the printer name to the one specified in the config file
[b58aada]89my $printer = $cups->getDestination("$queue");
[6b7441a]90
91# if $printer is not defined, then throw an error
[9fdf4a1]92unless( $printer){
[fc8707b]93    print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n";
[9fdf4a1]94    exit 1;
95}
[6b7441a]96
97# initialize the job id and title variables for use below
[b58aada]98my ($jobid, $title);
[c5a98db]99
[6b7441a]100# for each file that the user wants to print
[38388ac]101foreach my $file(@files) {
[6b7441a]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)
[38388ac]108    if ($file =~ m|http://www\.youtube\.com/watch\?v=|) {
[600e713]109        open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file";
[b58aada]110        print FILE $file;
111        $title = $file;
112        $file = "/tmp/gutenbach-youtube";
113        $printer->addOption("copies", 42);
[38388ac]114    }
[6b7441a]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.
[38388ac]120    else {
[b58aada]121        my $fileinfo = ImageInfo($file);
122        my $magic = $fileinfo->{FileType};
123
[9fdf4a1]124        if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) {
[b58aada]125            $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'};
126        }
127        else {
128            $title = $file;
[38388ac]129        }
130    }
131
[fc8707b]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);
[6b7441a]136
[fc8707b]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    }   
[c5a98db]152}
Note: See TracBrowser for help on using the repository browser.