source: client/bin/gbr @ ca8acb7

debianmacno-cups
Last change on this file since ca8acb7 was ca8acb7, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago

Add recursive file playing to gbr (Trac #29)

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