3 # umph - Command line tool for parsing YouTube feeds
4 # Copyright (C) 2010-2012 Toni Gundogdu <legatvs@cpan.org>
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/>.
21 use feature
'say', 'switch';
26 binmode STDOUT
, ":utf8";
27 binmode STDERR
, ":utf8";
29 use version
0.77 (); our $VERSION = version
->declare("0.2.5");
31 use Getopt
::ArgvFile
(home
=> 1, startupFilename
=> [qw(.umphrc)]);
32 use Getopt
::Long
qw(:config bundling);
39 eval "require Umph::Prompt";
40 my $p = $@ ?
"" : " with Umph::Prompt version $Umph::Prompt::VERSION";
41 say "umph version $VERSION$p";
48 Pod
::Usage
::pod2usage
(-exitstatus
=> 0, -verbose
=> 1);
51 use constant MAX_RESULTS_LIMIT
=> 50; # Refer to http://is.gd/OcSjwU
54 sub chk_max_results_value
56 if ($config{max_results
} > MAX_RESULTS_LIMIT
)
59 "WARNING --max-results exceeds max. accepted value, using "
62 $config{max_results
} = MAX_RESULTS_LIMIT
;
66 sub chk_depr_export_format_opts
71 qq/W: --json is deprecated, use --export-format=json instead/;
72 $config{export_format
} = 'json';
77 qq/W: --csv is deprecated, use --export-format=csv instead/;
78 $config{export_format
} = 'csv';
84 if ($config{'interactive'} and not eval 'require Umph::Prompt')
87 qq/W: "Umph::Prompt" module not found, ignoring --interactive/;
88 $config{interactive
} = 0;
96 my $root = $doc->getDocumentElement;
98 if ($config{export_response
})
100 if ($root->getElementsByTagName("error"))
102 $doc->printToFile($config{export_response
});
104 "\nI: Error response written to $config{export_response}";
105 say STDERR
"I: Program terminated with status 1";
111 for my $e ($root->getElementsByTagName("error"))
113 my $d = tag0
($e, "domain")->getFirstChild->getNodeValue;
114 my $c = tag0
($e, "code")->getFirstChild->getNodeValue;
115 my $errmsg = "error: $d: $c";
116 chk_error_resp_reason
($e, \
$errmsg);
117 chk_error_resp_loc
($e, \
$errmsg);
123 sub chk_error_resp_loc
125 my ($e, $errmsg) = @_;
127 my $l = tag0
($e, "location");
130 my $t = $l->getAttributeNode("type")->getValue;
131 $$errmsg .= ": " . $l->getFirstChild->getNodeValue . " [type=$t]";
134 sub chk_error_resp_reason
136 my ($e, $errmsg) = @_;
138 my $r = tag0
($e, "internalReason");
141 $$errmsg .= ": " . $r->getFirstChild->getNodeValue;
149 'start_index|start-index|s=i',
150 'max_results|max-results|m=i',
153 'export_format|export-format|d=s',
156 'user_agent|user-agent|g=s',
159 'export_response|export-response|E=s',
161 'version' => \
&print_version
,
162 'help' => \
&print_help
,
165 print_help
if scalar @ARGV == 0;
168 $config{user_agent
} ||= 'Mozilla/5.0';
169 $config{export_format
} ||= '';
170 $config{type
} ||= 'p'; # "playlist".
171 $config{start_index
} ||= 1;
172 $config{max_results
} ||= 25;
174 chk_depr_export_format_opts
;
175 chk_max_results_value
;
179 sub spew_qe
{ print STDERR
@_ unless $config{quiet
} }
186 spew_qe
"Checking ... ";
189 my $a = new LWP
::UserAgent
;
190 $a->env_proxy; # http://search.cpan.org/perldoc?LWP::UserAgent
191 $a->proxy('http', $config{proxy
}) if $config{proxy
};
192 $a->no_proxy('') if $config{no_proxy
};
193 $a->agent($config{user_agent
});
196 my $p = new XML
::DOM
::Parser
(LWP_UserAgent
=> $a);
197 my $s = $config{start_index
};
198 my $m = $config{all
} ? MAX_RESULTS_LIMIT
: $config{max_results
};
202 my $d = $p->parsefile(to_url
($ARGV[0], $s, $m));
203 my $r = $d->getDocumentElement;
208 for my $e ($r->getElementsByTagName("entry"))
210 my $t = tag0
($e, "title")->getFirstChild->getNodeValue;
213 for my $l ($e->getElementsByTagName("link"))
215 if ($l->getAttributeNode("rel")->getValue eq "alternate")
217 $u = $l->getAttributeNode("href")->getValue;
221 croak
qq/"link" not found/ unless $u;
223 push_unique_only
($t, $u);
225 spew_qe
((++$n % 5 == 0) ?
" " : ".");
229 last if $n == 0 or not $config{all
};
233 croak
"error: nothing found\n" if scalar @items == 0;
235 open_prompt
() if $config{interactive
};
237 say qq/{\n "video": [/ if $config{export_format
} =~ /^j/;
241 for my $item (@items)
243 if ($item->{selected
} or not $config{interactive
})
247 my $t = $item->{title
} || "";
250 given ($config{export_format
})
256 say qq/ "title": "$t",/;
257 say qq/ "url": "$item->{url}"/;
262 say qq/"$t","$item->{url}"/;
272 say "\n ]\n}" if $config{export_format
} =~ /^j/;
276 use constant GURL
=> "http://gdata.youtube.com/feeds/api";
280 my ($arg0, $s, $m) = @_;
283 given ($config{type
})
287 $u = GURL
. "/users/$arg0/uploads";
291 $u = GURL
. "/users/$arg0/favorites";
295 $arg0 = $1 # Grab playlist ID if URL
296 if $arg0 =~ /^http.*list=([\w_-]+)/;
298 croak
"$arg0: does not look like a playlist ID\n"
299 if length $arg0 < 16;
301 $u = GURL
. "/playlists/$arg0";
306 $u .= "&start-index=$s";
307 $u .= "&max-results=$m";
308 $u .= "&strict=true"; # Refer to http://is.gd/0msY8X
314 $e->getElementsByTagName($t)->item(0);
320 my $q = qr
|v
=([\w\
-_
]+)|;
324 my $a = $1 if $i->{url
} =~ /$q/;
325 my $b = $1 if $u =~ /$q/;
328 push @items, {title
=> $t, url
=> $u, selected
=> 1};
333 my $p = new Umph
::Prompt
(
339 $p->exit(\
@items, $args);
343 $p->display(\
@items, $args);
347 $p->max_shown_items(@
{$args});
351 $p->select(\
@items, $args);
357 {cmd
=> 'normal', desc
=> 'print results in default format'};
358 push @a, {cmd
=> 'json', desc
=> 'print results in json'};
359 push @a, {cmd
=> 'csv', desc
=> 'print results in csv'};
363 $config{export_format
} = '';
364 say STDERR
"=> print in default format";
367 $config{export_format
} = 'json';
368 say STDERR
"=> print in $config{export_format}";
371 $config{export_format
} = 'csv';
372 say STDERR
"=> print in $config{export_format}";
376 # Callbacks. All of these are optional.
379 $p->toggle(\
@items, $args);
381 onitems
=> sub { return \
@items },
384 $p->display(\
@items, $args);
387 # Other (required) settings
388 total_items
=> scalar @items,
389 prompt_msg
=> 'umph',
390 max_shown_items
=> 20
393 say STDERR
qq/Enter prompt. Type "help" to get a list of commands./;
401 umph [-q] [-i] [--type=E<lt>valueE<gt>]
402 [--export-response=E<lt>valueE<gt>] [--export-format=E<lt>valueE<gt>]
403 [[--all | [--start-index=E<lt>valueE<gt>] [--max-results=E<lt>valueE<gt>]]
404 [--proxy=E<lt>addrE<gt> | --no-proxy] [--user-agent=E<lt>valueE<gt>]
405 [--help] E<lt>playlist_idE<gt> | E<lt>usernameE<gt>
409 --help Print help and exit
410 --version Print version and exit
412 -i, --interactive Run in interactive mode
413 -t, --type arg (=p) Get feed type
414 -s, --start-index arg (=1) Index of first matching result
415 -m, --max-results arg (=25) Max number of results included
416 -a, --all Get the entire feed
417 -E, --export-response arg Write server error response to file
418 -d, --export-format arg Interchange format to print in
419 --json [depr.] Print details in JSON
420 --csv [depr.] Print details in CSV
421 -g, --user-agent arg (=Mozilla/5.0) Set the HTTP user-agent
422 --proxy arg (=http_proxy) Use proxy for HTTP connections
423 --no-proxy Disable use of HTTP proxy
427 # vim: set ts=2 sw=2 tw=72 expandtab: