source: client/bin/gbr @ 43ddb7a

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

Added a -n flag to gbr, to specify the number of files to play

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