Update NEWS for 2.3.3
[clive.git] / bin / clive
blob5ea871c14af2e2bae6a8b14205a1ab738defc692
1 #!/usr/bin/perl
2 # -*- coding: ascii -*-
4 # clive
5 # Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 use warnings;
22 use strict;
24 use version 0.77 (); our $VERSION = version->declare("2.3.3");
26 binmode STDOUT, ":utf8";
27 binmode STDERR, ":utf8";
29 use Getopt::ArgvFile qw(argvFile);
31 use Getopt::Long qw(:config bundling);
32 use Encode qw(decode_utf8);
33 use Carp qw(croak);
35 my $depr_msg = "Warning:
36 '--format list' is deprecated and will be removed in the later
37 versions. Use --query-formats instead.";
39 my $quvi_quiet_switch = '-q';
40 my %config;
41 my @queue;
42 my $media;
44 exit main();
46 sub main
48 init();
49 return process_queue();
52 sub init
54 if (grep {$_ eq "--config-file"} @ARGV)
56 argvFile(fileOption => '--config-file');
58 else
60 @ARGV = (
61 @ARGV,
63 "@/usr/local/share/clive/cliverc",
64 "@/usr/share/clive/cliverc",
65 "@/etc/clive/config",
66 "@/etc/xdg/clive/clive.conf",
67 "@/etc/xdg/clive.conf"
71 if ($ENV{HOME})
73 @ARGV = (
74 @ARGV,
76 '@' . "$ENV{HOME}/.cliverc",
77 '@' . "$ENV{HOME}/.clive/config",
78 '@' . "$ENV{HOME}/.config/clive/config"
83 push @ARGV, '@' . "$ENV{CLIVE_CONFIG}" if $ENV{CLIVE_CONFIG};
85 argvFile();
88 GetOptions(
89 \%config,
90 'help' => \&print_help,
91 'version' => sub {print "clive version $VERSION\n"; exit 0},
92 'license' => \&print_license,
93 'quiet|q',
94 'query_formats|query-formats|F',
95 'format|f=s',
96 'output_file|output-file|O=s',
97 'no_download|no-download|n',
99 # Configuration:
100 'quvi=s',
101 'get_with|get-with=s',
102 'filename_format|filename-format=s',
103 'regexp=s',
104 'exec=s',
105 ) or exit 1;
107 $config{format} ||= 'default';
108 $config{filename_format} ||= '%t.%s';
109 $config{regexp} ||= '/(\\w|\\s)/g';
111 # Check --quvi.
112 unless ($config{quvi})
114 print "Detect quvi from \$PATH\n" unless $config{quiet};
116 my $s = detect_cmd('quvi');
117 if ($s)
119 $config{quvi} = "quvi %u";
121 else
123 croak "error: specify path to quvi(1) command with --quvi\n";
126 check_quvi();
127 check_format();
129 # Check --get-with.
130 unless ($config{get_with})
133 print "Detect a download command from \$PATH\n"
134 unless $config{quiet};
136 my %h = (
137 curl => "-L -C - -o %f %u --user-agent Mozilla/5.0",
139 # Add new ones below.
142 for my $k (keys %h)
144 my $s = detect_cmd($k);
145 if ($s)
147 $config{get_with} = "$k $h{$k}";
148 last;
152 croak "error: specify path to a download command with --get-with\n"
153 unless $config{get_with};
156 # Check --regexp.
158 apply_regexp();
160 # Process input.
162 if (scalar @ARGV == 0)
164 append_queue($_) while <STDIN>;
166 else
168 foreach (@ARGV)
170 if (!is_url($_))
172 open my $fh, "<", $_
173 or print STDERR "$_: $!\n" and next;
174 append_queue($_) while <$fh>;
175 close $fh;
177 else
179 append_queue($_);
184 @queue = uniq2(@queue); # Remove duplicate URLs.
186 print STDERR "error: no input urls\n" and exit 0x3 # QUVI_INVARG
187 unless scalar @queue;
189 select STDOUT;
190 $| = 1; # Go unbuffered.
193 sub detect_cmd
195 my ($cmd) = @_;
197 print " Check for $cmd ..." unless $config{quiet};
198 my $o = join '', qx|$cmd --version 2>/dev/null|;
200 if ($? >> 8 == 0)
203 # TODO: Use more a elegant regexp combining all three.
204 my @a =
205 (qr|(\d+.\d+.\d+-\w+-\w+)|, qr|(\d+.\d+.\d+)|, qr|(\d+.\d+)|);
206 foreach (@a)
208 if ($o =~ /$_/)
210 print "$1\n" unless $config{quiet};
211 return $1;
215 else
217 print "no\n" unless $config{quiet};
219 undef;
222 sub is_url
224 return $_ =~ /^\w+\:\/\//;
227 sub append_queue
229 my $ln = trim(shift);
230 chomp $ln;
232 return if $ln =~ /^$/;
233 return if $ln =~ /^#/;
235 push @queue, $ln;
238 sub uniq2
239 { # http://is.gd/g8jQU
240 my %seen = ();
241 my @r = ();
242 foreach my $a (@_)
244 unless ($seen{$a})
246 push @r, $a;
247 $seen{$a} = 1;
253 sub process_queue
255 require JSON::XS;
257 my $n = scalar @queue;
258 my $i = 0;
259 my $r = 0;
260 my $fpath;
262 foreach (@queue)
264 print_checking(++$i, $n);
266 my $q = $config{quvi};
267 $q =~ s/%u/"$_"/;
268 $q .= " $quvi_quiet_switch"
269 if $q !~ /$quvi_quiet_switch/; # Force quiet.
270 $q .= " -f $config{format}";
271 $q .= " -F" if $config{query_formats};
273 my $o = join '', qx/$q/;
274 $r = $? >> 8;
276 next unless $r == 0;
278 print "done.\n" unless $config{quiet};
279 print $o and next if $config{query_formats};
281 $media = JSON::XS::decode_json($o);
282 ($r, $fpath) = get_media();
283 if ($r == 0)
285 $r = invoke_exec($fpath) if $config{exec};
291 sub print_checking
293 return if $config{quiet};
295 my ($i, $n) = @_;
297 print "($i of $n) " if $n > 1;
298 print "Checking ...";
301 sub get_media
303 require File::Basename;
305 my $fpath = get_filename();
306 my $fname = File::Basename::basename($fpath);
308 if ($config{no_download}) {print_media($fname); return 0;}
310 write_media($fpath, $fname);
313 sub invoke_exec
315 my $fpath = shift;
317 my $e = $config{exec};
318 $e =~ s/%f/"$fpath"/g;
320 qx/$e/;
322 $? >> 8;
325 sub to_mb {(shift) / (1024 * 1024);}
327 sub print_media
329 printf "%s %.2fM [%s]\n",
330 shift,
331 to_mb($media->{link}[0]->{length_bytes}),
332 $media->{link}[0]->{content_type};
335 sub write_media
337 my ($fpath, $fname) = @_;
339 my $g = $config{get_with};
340 $g =~ s/%u/"$media->{link}[0]->{url}"/g;
341 $g =~ s/%f/"$fpath"/g;
342 $g =~ s/%n/"$fname"/g;
344 qx/$g/;
346 ($? >> 8, $fpath);
349 sub get_filename
351 my $fpath;
353 if ($config{output_file}) {$fpath = $config{output_file};}
354 else {$fpath = apply_output_path(apply_filename_format());}
356 $fpath;
359 sub apply_output_path
361 require Cwd;
363 # Do not touch.
364 my $cwd = decode_utf8(Cwd::getcwd);
365 my $fname = shift;
367 require File::Spec::Functions;
369 File::Spec::Functions::catfile($cwd, $fname);
372 sub apply_filename_format
374 return $config{output_filename}
375 if $config{output_filename};
377 my $title = trim(apply_regexp($media->{page_title}));
378 my $fname = $config{filename_format};
380 $fname =~ s/%s/$media->{link}[0]->{file_suffix}/g;
381 $fname =~ s/%h/$media->{host}/g if $media->{host}; # quvi 0.2.8+
382 $fname =~ s/%i/$media->{id}/g;
383 $fname =~ s/%t/$title/g;
385 $fname;
388 sub trim
390 my $s = shift;
391 $s =~ s{^[\s]+}//;
392 $s =~ s{\s+$}//;
393 $s =~ s{\s\s+}/ /g;
397 sub apply_regexp
399 my ($title, $rq) = (shift, qr|^/(.*)/(.*)$|);
401 if ($config{regexp} =~ /$rq/)
403 return unless $title; # Must be a syntax check.
405 $title = decode_utf8($title); # Do not touch.
407 my ($pat, $flags, $g, $i) = ($1, $2);
409 if ($flags)
411 $g = ($flags =~ /g/);
412 $i = ($flags =~ /i/);
415 $rq = $i ? qr|$pat|i : qr|$pat|;
417 return $g
418 ? join '', $title =~ /$rq/g
419 : join '', $title =~ /$rq/;
422 croak "error: --regexp: expects "
423 . "`/pattern/flags', for example: `/(\\w)/g'\n";
426 sub detect_quvi_version
428 my $q = (split /\s+/, $config{quvi})[0]; # Improve this.
429 my $o = qx|$q --version|;
430 if ($? >> 8 == 0)
432 return ($1, $2, $3) if (split /\n/, $o)[0] =~ /(\d+).(\d+).(\d+)/;
434 print "warning: unable to detect quvi version\n"
435 unless $config{quiet};
439 sub check_quvi
441 my @v = detect_quvi_version();
442 $quvi_quiet_switch = '-vq' if $v[0] >= 0 && $v[1] >= 4 && $v[2] >= 1;
445 sub check_format
447 if ($config{format} eq "help")
449 printf "Usage:
450 --format arg get format arg of media
451 --format list print domains with formats
452 --format list arg match arg to supported domain names
453 Examples:
454 --format list youtube print youtube formats
455 --format fmt34_360p get format fmt34_360p of media
456 %s\n", $depr_msg;
457 exit 0;
460 elsif ($config{format} eq "list")
462 my $q = (split /\s+/, $config{quvi})[0]; # Improve this.
464 my %h;
465 foreach (qx/$q --support/)
467 my ($k, $v) = split /\s+/, $_;
468 $h{$k} = $v;
471 # -f list <pattern>
472 if (scalar @ARGV > 0)
475 foreach (sort keys %h)
477 print "$_:\n $h{$_}\n" if $_ =~ /$ARGV[0]/;
481 # -f list
482 else
484 print "$_:\n $h{$_}\n\n" foreach sort keys %h;
487 printf "%s\n", $depr_msg;
489 exit 0;
493 sub print_help
495 require Pod::Usage;
496 Pod::Usage::pod2usage(-exitstatus => 0, -verbose => 1);
499 sub print_license
501 print "# clive
502 # Copyright (C) 2010-2011 Toni Gundogdu <legatvs\@gmail.com>
504 # This program is free software: you can redistribute it and/or modify
505 # it under the terms of the GNU General Public License as published by
506 # the Free Software Foundation, either version 3 of the License, or
507 # (at your option) any later version.
509 # This program is distributed in the hope that it will be useful,
510 # but WITHOUT ANY WARRANTY; without even the implied warranty of
511 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
512 # GNU General Public License for more details.
514 # You should have received a copy of the GNU General Public License
515 # along with this program. If not, see <http://www.gnu.org/licenses/>.
517 exit 0;
520 __END__
522 =head1 SYNOPSIS
524 clive [-F] [-n] [--format=E<lt>value<gt>] [--output-file=E<lt>value<gt>]
525 [--filename-format=E<lt>valueE<gt>] [--config-file=E<lt>value<gt>]
526 [--quvi=E<lt>valueE<gt>] [--get-with=E<lt>valueE<gt>]
527 [--regexp=E<lt>valueE<gt>] [--exec=E<lt>valueE<gt>]
528 [--help] [--version] [--license] [--quiet]
529 [E<lt>urlE<gt> | E<lt>fileE<gt>]
531 =head2 OPTIONS
533 --help Print help and exit
534 --version Print version and exit
535 --license Print license and exit
536 --quiet Turn off all output excl. errors
537 -F, --query-formats Query available formats to URL
538 -f, --format arg (=default) Download media format
539 -O, --output-file arg Write media to arg
540 -n, --no-download Do not download media, print details
541 --config-file arg File to read clive arguments from
542 Configuration:
543 --quvi arg Path to quvi(1) with additional args
544 --get-with arg Path to download command with args
545 --filename-format arg (=%t.%s) Downloaded media filename format
546 --regexp arg (=/(\w|\s)/g) Regexp to cleanup media title
547 --exec arg Invoke arg after each finished download
549 =cut
551 # vim: set ts=2 sw=2 tw=72 expandtab: