videosite-dl: Add -o option
[videosite.git] / videosite-dl.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5 use File::Spec;
6 use File::Basename;
7 use Module::Load;
8 use Cwd qw(realpath);
9 use Carp;
10
11 $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
12
13 my $info = 0;
14 my $debug = 0;
15 my %config = (
16     mode => 'download',
17     getter => 'filegetter',
18     'plugin.youtube.QUALITY' => 'hd',
19     'plugin.youtube.ADAPTIVE' => 1,
20     'plugin.filegetter.FILEPATTERN' => './%3$s.flv',
21 );
22 my @options;
23
24 sub link_callback {
25     my $m = shift;
26
27     if ($info) {
28         foreach (keys(%{$m})) {
29             printf("%s: %s\n", $_, defined($m->{$_})?$m->{$_}:'(undef)');
30         }
31         return 1;
32     } else {
33         print("Downloading $m->{'TITLE'}\n");
34         return 0;
35     }
36 }
37
38
39 GetOptions("i" => \$info, "d" => \$debug, "o=s" => \@options);
40 %config = (%config, map { split(/=/, $_, 2) } @options);
41
42 push(@INC, dirname(realpath($0)));
43 load 'libvideosite';
44
45 unless(libvideosite::register_api({
46     link_callback => \&link_callback,
47     _config_default => sub { return \%config },
48     _debug => sub { return $debug },
49 })) {
50     die("Error registering API: $libvideosite::error");
51 }
52
53 unless(libvideosite::init()) {
54     die("Could not init libvideosite: $libvideosite::error");
55 }
56
57 foreach (@ARGV) {
58     printf("Handling %s...\n", $_);
59     libvideosite::check_for_link({
60         message => $_,
61         io => sub { print @_, "\n" },
62         window => "",
63     });
64 }