From 296c0caba6b8def1e6ca35106b9fe6c7b8b0516c Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Sat, 6 Aug 2011 14:28:36 +0300 Subject: [PATCH] Apply style changes --- bin/grake | 758 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 390 insertions(+), 368 deletions(-) rewrite bin/grake (63%) diff --git a/bin/grake b/bin/grake dissimilarity index 63% index b0c9708..4c6ec67 100755 --- a/bin/grake +++ b/bin/grake @@ -1,368 +1,390 @@ -#!/usr/bin/perl -# -*- coding: ascii -*- - -# grake -# Copyright (C) 2010,2011 Toni Gundogdu -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - -use warnings; -use strict; - -use version 0.77 (); our $VERSION = version->declare("0.0.7"); - -binmode STDOUT, ":utf8"; -binmode STDERR, ":utf8"; - -use Getopt::ArgvFile( home => 1, startupFilename => [qw(.grakerc)] ); -use Getopt::Long qw(:config bundling); - -my %config; - -exit main(); - -sub init { - GetOptions( - \%config, - 'interactive|i', - 'title|t', - 'json', - 'csv', - 'proxy=s', - 'no_proxy|no-proxy', - 'quiet|q', - 'version' => \&print_version, - 'license' => \&print_license, - 'help' => \&print_help, - ) or exit 1; - - $config{title} ||= $config{json}; - $config{title} ||= $config{csv}; -} - -sub print_version { - print "grake version $VERSION\n"; - exit 0; -} - -sub print_license { - print "# Copyright (C) 2010,2011 Toni Gundogdu. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -"; - exit 0; -} - -sub print_help { - require Pod::Usage; - Pod::Usage::pod2usage( -exitstatus => 0, -verbose => 1 ); -} - -my @ids; -my @links; - -sub main { - - init(); - - print_help() unless scalar @ARGV; - - print STDERR "Checking ..." unless $config{quiet}; - - require LWP; - - my $a = new LWP::UserAgent; - - $a->env_proxy; # http://search.cpan.org/perldoc?LWP::UserAgent - - $a->proxy( 'http', $config{proxy} ) if $config{proxy}; - $a->no_proxy('') if $config{no_proxy}; - - # Match: /watch?v=, /v/, /embed/ - # (At least) Hulu uses the same "/embed/" which is why we no longer - # use the "(?>[-_\w]{11})". - - my $q = qr{[?/](?:embed|v)[=/]((?>[-_\w]+))}; - - require URI::Escape; - - foreach (@ARGV) { - - my $r = $a->get($_); - - unless ( $r->is_success ) { - printf STDERR "\nerror: $_: %s\n", $r->status_line; - next; - } - - my $d = URI::Escape::uri_unescape( $r->content ); - @ids = weed( uniq2( ( @ids, $d =~ /$q/g ) ) ); - - print STDERR "." unless $config{quiet}; - } - - unless ( scalar @ids ) { - print STDERR "error: nothing found.\n"; - return 0; - } - else { print STDERR "done.\n" unless $config{quiet}; } - - if ( $config{title} ) { - print STDERR ":: Getting video title ..." unless $config{quiet}; - } - - foreach my $id (@ids) { - - my %tmp = ( - id => $id, - url => "http://youtube.com/watch?v=$id", - gvi => "http://www.youtube.com/get_video_info?&video_id=$id" - . "&el=detailpage&ps=default&eurl=&gl=US&hl=en", - title => undef, - selected => 1 - ); - - $tmp{title} = get_title( $a, \%tmp ) if $config{title}; - - push @links, \%tmp; - } - - if ( $config{title} ) { - print STDERR "done.\n" unless $config{quiet}; - } - - prompt() if $config{interactive}; - - if ( $config{json} ) { print qq/{\n "video": [\n/; } - - my $i = 0; - - foreach (@links) { - - if ( $_->{selected} or not $config{interactive} ) { - - ++$i; - - my $t = $_->{title} || ""; - $t =~ s/"/\\"/g; - - if ( $config{json} ) { - - print ",\n" if $i > 1; - - print " {\n" - . qq/ "title": "$t",\n/ - . qq/ "url": "$_->{url}"\n/ . " }", - ; - } - - elsif ( $config{csv} ) { print qq/"$t","$_->{url}"\n/; } - - else { print "$_->{url}\n"; } - - } - - } - - if ( $config{json} ) { print "\n ]\n}\n"; } - - return 0; -} - -sub get_title { - - my ( $a, $video ) = @_; - - my $r = $a->get( $$video{gvi} ); - - unless ( $r->is_success ) { - printf STDERR "\nerror: $$video{url}: %s\n", $r->status_line; - return; - } - - require CGI; - - my $q = CGI->new( $r->content ); - - my $title; - - if ( $q->param('reason') ) { - printf STDERR "\nerror: %s: %s (errorcode: %d)\n", - $$video{url}, trim( $q->param("reason") ), - $q->param("errorcode"); - } - else { - require Encode; - $title = trim( Encode::decode_utf8( $q->param('title') ) ); - print STDERR "." unless $config{quiet}; - } - - $title; -} - -sub trim { - my $s = shift; - $s =~ s{^[\s]+}//; - $s =~ s{\s+$}//; - $s =~ s{\s\s+}/ /g; - $s; -} - -sub weed { - my @r = (); - foreach (@_) { - push @r, $_ if length $_ == 11; - } - @r; -} - -sub uniq2 { # http://is.gd/g8jQU - my %seen = (); - my @r = (); - foreach my $a (@_) { - unless ( $seen{$a} ) { - push @r, $a; - $seen{$a} = 1; - } - } - @r; -} - -my $done = 0; - -sub prompt { - - my %cmds = ( - 'h' => \&help, - 'q' => \&quit, - 'l' => \&list, - 'a' => \&select_all, - 'n' => \&select_none, - 'i' => \&invert_selection, - 'd' => \&dump, - ); - - print STDERR - "Enter prompt. Type \"help\" to get a list of commands.\n"; - list(); - - my $p = "(grake) "; - - while ( not $done ) { - - print STDERR $p; - - my $ln = ; - - next unless $ln; - chomp $ln; - - if ( $ln =~ /(\d+)/ ) { toggle_caption($1); } - - else { - next unless $ln =~ /(\w)/; - $cmds{$1}() if defined $cmds{$1}; - } - - } - -} - -sub help { - print STDERR "Commands: - help .. this - list .. display found links (> indicates selected for download) - all .. select all - none .. select none - invert .. invert selection - (number) .. toggle caption - dump .. dump selected links and exit - quit .. quit without dumping links\n" - . qq/Command name abbreviations are allowed, e.g. "h" instead of "help"\n/; -} - -sub quit { exit 0; } - -sub list { - my $i = 0; - foreach (@links) { - printf STDERR "%2s%02d: %s\n", $_->{selected} - ? ">" - : "", - ++$i, - $_->{title} || $_->{url}; - } -} - -sub select_all { - $_->{selected} = 1 foreach @links; - list(); -} - -sub select_none { - $_->{selected} = 0 foreach @links; - list(); -} - -sub invert_selection { - $_->{selected} = not $_->{selected} foreach @links; - list(); -} - -sub dump { $done = 1; } - -sub toggle_caption { - my $i = (shift) - 1; - if ( $i >= 0 && exists $links[$i] ) { - $links[$i]->{selected} = not $links[$i]->{selected}; - list(); - } - else { print STDERR "error: out of range\n"; } -} - -__END__ - -=head1 SYNOPSIS - -grake [-q] [-i] [-t] [--csv | --json] [--proxy EaddrE | --no-proxy] - [...] - -=head1 OPTIONS - - --help Print help and exit - --version Print version and exit - --license Print license and exit - -q, --quiet Be quiet - -i, --interactive Run in interactive mode - -t, --title Get title for video link - --json Print details in json, implies -t - --csv Print details in csv, implies -t - --proxy arg (=http_proxy) Use proxy for HTTP connections - --no-proxy Disable use of HTTP proxy - -=cut - -# vim: set ts=4 sw=4 tw=72 expandtab: +#!/usr/bin/perl +# -*- coding: ascii -*- +# +# grake +# Copyright (C) 2010-2011 Toni Gundogdu +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +use warnings; +use strict; + +use version 0.77 (); our $VERSION = version->declare("0.0.7"); + +binmode STDOUT, ":utf8"; +binmode STDERR, ":utf8"; + +use Getopt::ArgvFile(home => 1, startupFilename => [qw(.grakerc)]); +use Getopt::Long qw(:config bundling); + +my %config; + +exit main(); + +sub init +{ + GetOptions( + \%config, + 'interactive|i', + 'title|t', + 'json', + 'csv', + 'proxy=s', + 'no_proxy|no-proxy', + 'quiet|q', + 'version' => \&print_version, + 'license' => \&print_license, + 'help' => \&print_help, + ) or exit 1; + + $config{title} ||= $config{json}; + $config{title} ||= $config{csv}; +} + +sub print_version +{ + print "grake version $VERSION\n"; + exit 0; +} + +sub print_license +{ + print "# Copyright (C) 2010-2011 Toni Gundogdu. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"; + exit 0; +} + +sub print_help +{ + require Pod::Usage; + Pod::Usage::pod2usage(-exitstatus => 0, -verbose => 1); +} + +my @ids; +my @links; + +sub main +{ + init(); + + print_help() unless scalar @ARGV; + + print STDERR "Checking ..." unless $config{quiet}; + + require LWP; + + my $a = new LWP::UserAgent; + + $a->env_proxy; # http://search.cpan.org/perldoc?LWP::UserAgent + + $a->proxy('http', $config{proxy}) if $config{proxy}; + $a->no_proxy('') if $config{no_proxy}; + + # Match: /watch?v=, /v/, /embed/ + # (At least) Hulu uses the same "/embed/" which is why we no longer + # use the "(?>[-_\w]{11})". + + my $q = qr{[?/](?:embed|v)[=/]((?>[-_\w]+))}; + + require URI::Escape; + + foreach (@ARGV) + { + my $r = $a->get($_); + + unless ($r->is_success) + { + printf STDERR "\nerror: $_: %s\n", $r->status_line; + next; + } + + my $d = URI::Escape::uri_unescape($r->content); + @ids = weed(uniq2((@ids, $d =~ /$q/g))); + + print STDERR "." unless $config{quiet}; + } + + unless (scalar @ids) + { + print STDERR "error: nothing found.\n"; + return 0; + } + else {print STDERR "done.\n" unless $config{quiet};} + + if ($config{title}) + { + print STDERR ":: Getting video title ..." unless $config{quiet}; + } + + foreach my $id (@ids) + { + my %tmp = ( + id => $id, + url => "http://youtube.com/watch?v=$id", + gvi => "http://www.youtube.com/get_video_info?&video_id=$id" + . "&el=detailpage&ps=default&eurl=&gl=US&hl=en", + title => undef, + selected => 1 + ); + + $tmp{title} = get_title($a, \%tmp) if $config{title}; + + push @links, \%tmp; + } + + if ($config{title}) + { + print STDERR "done.\n" unless $config{quiet}; + } + + prompt() if $config{interactive}; + + if ($config{json}) {print qq/{\n "video": [\n/;} + + my $i = 0; + + foreach (@links) + { + if ($_->{selected} or not $config{interactive}) + { + ++$i; + + my $t = $_->{title} || ""; + $t =~ s/"/\\"/g; + + if ($config{json}) + { + print ",\n" if $i > 1; + + print " {\n" + . qq/ "title": "$t",\n/ + . qq/ "url": "$_->{url}"\n/ . " }", + ; + } + + elsif ($config{csv}) {print qq/"$t","$_->{url}"\n/;} + + else {print "$_->{url}\n";} + } + } + + if ($config{json}) {print "\n ]\n}\n";} + + return 0; +} + +sub get_title +{ + my ($a, $video) = @_; + + my $r = $a->get($$video{gvi}); + + unless ($r->is_success) + { + printf STDERR "\nerror: $$video{url}: %s\n", $r->status_line; + return; + } + + require CGI; + my $q = CGI->new($r->content); + + my $title; + + if ($q->param('reason')) + { + printf STDERR "\nerror: %s: %s (errorcode: %d)\n", + $$video{url}, trim($q->param("reason")), + $q->param("errorcode"); + } + else + { + require Encode; + $title = trim(Encode::decode_utf8($q->param('title'))); + print STDERR "." unless $config{quiet}; + } + + $title; +} + +sub trim +{ + my $s = shift; + $s =~ s{^[\s]+}//; + $s =~ s{\s+$}//; + $s =~ s{\s\s+}/ /g; + $s; +} + +sub weed +{ + my @r = (); + foreach (@_) + { + push @r, $_ if length $_ == 11; + } + @r; +} + +sub uniq2 +{ # http://is.gd/g8jQU + my %seen = (); + my @r = (); + foreach my $a (@_) + { + unless ($seen{$a}) + { + push @r, $a; + $seen{$a} = 1; + } + } + @r; +} + +my $done = 0; + +sub prompt +{ + my %cmds = ( + 'h' => \&help, + 'q' => \&quit, + 'l' => \&list, + 'a' => \&select_all, + 'n' => \&select_none, + 'i' => \&invert_selection, + 'd' => \&dump, + ); + + print STDERR + "Enter prompt. Type \"help\" to get a list of commands.\n"; + list(); + + my $p = "(grake) "; + + while (not $done) + { + print STDERR $p; + + my $ln = ; + + next unless $ln; + chomp $ln; + + if ($ln =~ /(\d+)/) {toggle_caption($1);} + + else + { + next unless $ln =~ /(\w)/; + $cmds{$1}() if defined $cmds{$1}; + } + + } +} + +sub help +{ + print STDERR "Commands: + help .. this + list .. display found links (> indicates selected for download) + all .. select all + none .. select none + invert .. invert selection + (number) .. toggle caption + dump .. dump selected links and exit + quit .. quit without dumping links\n" + . qq/Command name abbreviations are allowed, e.g. "h" instead of "help"\n/; +} + +sub quit {exit 0;} + +sub list +{ + my $i = 0; + foreach (@links) + { + printf STDERR "%2s%02d: %s\n", $_->{selected} + ? ">" + : "", + ++$i, + $_->{title} || $_->{url}; + } +} + +sub select_all +{ + $_->{selected} = 1 foreach @links; + list(); +} + +sub select_none +{ + $_->{selected} = 0 foreach @links; + list(); +} + +sub invert_selection +{ + $_->{selected} = not $_->{selected} foreach @links; + list(); +} + +sub dump {$done = 1;} + +sub toggle_caption +{ + my $i = (shift) - 1; + if ($i >= 0 && exists $links[$i]) + { + $links[$i]->{selected} = not $links[$i]->{selected}; + list(); + } + else {print STDERR "error: out of range\n";} +} + +__END__ + +=head1 SYNOPSIS + +grake [-q] [-i] [-t] [--csv | --json] [--proxy EaddrE | --no-proxy] + [...] + +=head1 OPTIONS + + --help Print help and exit + --version Print version and exit + --license Print license and exit + -q, --quiet Be quiet + -i, --interactive Run in interactive mode + -t, --title Get title for video link + --json Print details in json, implies -t + --csv Print details in csv, implies -t + --proxy arg (=http_proxy) Use proxy for HTTP connections + --no-proxy Disable use of HTTP proxy + +=cut + +# vim: set ts=4 sw=4 tw=72 expandtab: -- 2.11.4.GIT