2 # -*- coding: ascii -*-
4 # Copyright (C) 2010 Toni Gundogdu.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 use version
0.77 (); our $VERSION = version
->declare("2.3.0_3");
25 binmode STDOUT
, ":utf8";
26 binmode STDERR
, ":utf8";
28 use Getopt
::ArgvFile
qw(argvFile);
30 use Getopt
::Long
qw(:config bundling);
31 use Encode
qw(decode_utf8);
41 return process_queue
();
46 if (grep {$_ eq "--config-file"} @ARGV)
47 { argvFile
(fileOption
=> '--config-file'); }
52 "@/usr/local/share/clive/cliverc",
53 "@/usr/share/clive/cliverc",
55 "@/etc/xdg/clive/clive.conf",
56 "@/etc/xdg/clive.conf"
61 '@'."$ENV{HOME}/.cliverc",
62 '@'."$ENV{HOME}/.clive/config",
63 '@'."$ENV{HOME}/.config/clive/config"
67 push @ARGV, '@'."$ENV{CLIVE_CONFIG}" if $ENV{CLIVE_CONFIG
};
74 'help' => \
&print_help
,
75 'version' => \
&print_version
,
76 'license' => \
&print_license
,
79 'output_file|output-file|O=s',
80 'no_download|no-download|n',
83 'get_with|get-with=s',
84 'filename_format|filename-format=s',
89 $config{format
} ||= 'default';
90 $config{filename_format
} ||= '%t.%s';
91 $config{regexp
} ||= '/(\\w|\\s)/g';
96 Carp
::croak
"error: specify path to quvi(1) command with --quvi"
101 Carp
::croak
"error: specify path to a download command with --get-with\n"
102 unless $config{get_with
};
107 add_queue
($_) foreach @ARGV;
109 if (scalar @queue == 0)
110 { add_queue
($_) while <STDIN
>; }
112 @queue = uniq2
(@queue); # Remove duplicate URLs.
114 select STDOUT
; $| = 1; # Go unbuffered.
119 my $ln = trim
(shift);
122 return if $ln =~ /^$/;
123 return if $ln =~ /^#/;
125 $ln = "http://$ln" if $ln !~ m{^http://}i;
130 sub uniq2
{ # http://is.gd/g8jQU
146 my $n = scalar @queue;
153 print_checking
(++$i, $n);
155 my $q = $config{quvi
};
157 $q .= " -q" if $q !~ /-q/; # Force --quiet.
158 $q .= " -f $config{format}";
160 my $o = join '', qx/$q/;
166 $video = JSON
::XS
::decode_json
($o);
168 print "done.\n" unless $config{quiet
};
170 ($r, $fpath) = get_video
();
174 { $r = invoke_exec
($fpath); }
183 return if $config{quiet
};
187 print "($i of $n) " if $n > 1;
188 print "Checking ...";
193 require File
::Basename
;
195 my $fpath = get_filename
();
196 my $fname = File
::Basename
::basename
($fpath);
198 if ($config{no_download
})
199 { print_video
($fname); return 0; }
201 write_video
($fpath, $fname);
208 my $e = $config{exec};
209 $e =~ s/%f/"$fpath"/g;
216 sub to_mb
{ (shift) / (1024*1024); }
219 printf "%s %.2fM [%s]\n",
221 to_mb
($video->{link}[0]->{length_bytes
}),
222 $video->{link}[0]->{content_type
};
227 my ($fpath, $fname) = @_;
229 my $g = $config{get_with
};
230 $g =~ s/%u/"$video->{link}[0]->{url}"/g;
231 $g =~ s/%f/"$fpath"/g;
232 $g =~ s/%n/"$fname"/g;
243 if ($config{output_file
})
244 { $fpath = $config{output_file
}; }
246 { $fpath = apply_output_path
(apply_filename_format
()); }
251 sub apply_output_path
{
256 my $cwd = decode_utf8
(Cwd
::getcwd
);
259 require File
::Spec
::Functions
;
261 File
::Spec
::Functions
::catfile
($cwd, $fname);
264 sub apply_filename_format
{
266 return $config{output_filename
}
267 if $config{output_filename
};
269 my $title = trim
(apply_regexp
($video->{page_title
}));
270 my $fname = $config{filename_format
};
272 $fname =~ s/%s/$video->{link}[0]->{file_suffix}/g;
273 $fname =~ s/%h/$video->{host}/g if $video->{host
}; # quvi 0.2.8+
274 $fname =~ s/%i/$video->{id}/g;
275 $fname =~ s/%t/$title/g;
290 my ($title, $rq) = (shift, qr
|^/(.*)/(.*)$|);
292 if ($config{regexp
} =~ /$rq/) {
294 return unless $title; # Must be a syntax check.
296 $title = decode_utf8
($title); # Do not touch.
298 my ($pat, $flags, $g, $i) = ($1, $2);
301 $g = ($flags =~ /g/);
302 $i = ($flags =~ /i/);
305 $rq = $i ? qr
|$pat|i
: qr
|$pat|;
309 ?
join '', $title =~ /$rq/g
310 : join '', $title =~ /$rq/;
314 "invalid syntax (`$config{regexp}'), expected Perl like syntax "
315 . "`/pattern/flags', e.g. `/(\\w)/g'";
322 if ($config{format
} eq "help") {
325 --format arg get format arg
326 --format list list websites and supported formats
327 --format list arg match arg to websites, list formats for matches
329 --format webm_480p get format webm_480p (youtube)
330 --format list youtube list youtube formats
331 --format list dailym list dailym(otion) formats
336 elsif ($config{format
} eq "list") {
338 my $q = (split /\s+/, $config{quvi
})[0]; # Improve this.
341 foreach (qx/$q --support/) {
342 my ($k,$v) = split /\s+/, $_;
347 if (scalar @ARGV > 0) {
349 foreach (sort keys %h)
350 { print "$_:\n $h{$_}\n" if $_ =~ /$ARGV[0]/; }
357 print "$_:\n $h{$_}\n\n" foreach sort keys %h;
365 Pod
::Usage
::pod2usage
(-exitstatus
=>0, -verbose
=>1);
369 printf "clive $VERSION with Perl %s on %s\n",
370 (sprintf "%vd", $^V
), $^O
;
376 "# Copyright (C) 2010 Toni Gundogdu.
378 # This program is free software: you can redistribute it and/or modify
379 # it under the terms of the GNU General Public License as published by
380 # the Free Software Foundation, either version 3 of the License, or
381 # (at your option) any later version.
383 # This program is distributed in the hope that it will be useful,
384 # but WITHOUT ANY WARRANTY; without even the implied warranty of
385 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
386 # GNU General Public License for more details.
388 # You should have received a copy of the GNU General Public License
389 # along with this program. If not, see <http://www.gnu.org/licenses/>.
398 clive [E<lt>optionsE<gt>] [E<lt>urlE<gt> ...]
402 --help print help and exit
403 --version print version and exit
404 --license print license and exit
405 --quiet turn off all output excl. errors
406 -f, --format arg (=default) download video format
407 -O, --output-file arg write downloaded video to file
408 -n, --no-download do not download video, print info only
409 --config-file arg file to read clive arguments from
411 --quvi arg path to quvi(1) with additional args
412 --get-with arg path to download command with args
413 --filename-format arg (=%t.%s) output video filename format
414 --regexp arg (=/(\w|\s)/g) regexp to clean up video title
415 --exec arg invoke arg when download finishes