Merge branch 'devel'
[videosite.git] / videosite-dl.pl
index 1b0079c..a907b84 100755 (executable)
@@ -4,113 +4,58 @@ use strict;
 use Getopt::Long;
 use File::Spec;
 use File::Basename;
+use Module::Load;
 use Cwd qw(realpath);
+use Carp;
 
-sub ploader {
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
 
-    my $dir = shift;
-    my $pattern = shift;
-    my $type = shift;
-    my @list;
-    my $p;
-    my $g;
-    my @g = ();
-
-    unshift(@INC, $dir);
-
-    opendir(D, $dir) || return ();
-    @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
-    closedir(D);
-
-    foreach $p (@list) {
-        $p =~ s/\.pm$//;
-        eval qq{ require videosite::$p; };
-        if ($@) {
-            print("Failed to load plugin: $@");
-            next;
-        }
-
-        $g = eval qq{ videosite::$p->new();};
-        if ($@) {
-            print("Failed to instanciate: $@");
-            delete($INC{$p});
-            next;
-        }
-
-        if ($type eq $g->{'TYPE'}) {
-            push(@g, $g);
-        } else {
-            printf('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
-            delete($INC{$p});
+my $info = 0;
+my $debug = 0;
+my %config = (
+    mode => 'download',
+    getter => 'filegetter',
+    'plugin.youtube.QUALITY' => 'hd',
+    'plugin.filegetter.FILEPATTERN' => './%3$s.flv',
+);
+
+sub link_callback {
+    my $m = shift;
+
+    if ($info) {
+        foreach (keys(%{$m})) {
+            printf("%s: %s\n", $_, defined($m->{$_})?$m->{$_}:'(undef)');
         }
+        return 1;
+    } else {
+        print("Downloading $m->{'TITLE'}\n");
+        return 0;
     }
-
-    return @g;
-}
-
-sub connectors {
-    my $c = {name => 'environment', schemas => {}};
-
-    if (exists($ENV{'http_proxy'})) {
-        $c->{schemas}->{'http'} = $ENV{'http_proxy'}
-    }
-
-    if (exists($ENV{'https_proxy'})) {
-        $c->{schemas}->{'https'} = $ENV{'https_proxy'}
-    }
-
-    return ( $c );
 }
 
 
-my $hq = 0;
-my $ext = '.flv';
-my $y;
-my $f;
-my $m;
-my @g;
-my $bp;
-my $info = 0;
-my $debug = 0;
-
 GetOptions("i" => \$info, "d" => \$debug);
 
-# This is some dark magic to find out our real base directory,
-# where we hope to find our plugins.
-$bp = File::Spec->catdir(dirname(realpath($0)), 'videosite');
-unshift(@INC, dirname(realpath($0)));
+push(@INC, dirname(realpath($0)));
+load 'libvideosite';
 
-@g = ploader($bp, '.*Grabber\.pm$', 'grabber');
-($f) = ploader($bp, '^FileGetter\.pm$', 'getter');
-
-unless(@g and defined($f)) {
-    print("No plugins could be loaded\n");
-    exit 1;
+unless(libvideosite::register_api({
+    link_callback => \&link_callback,
+    _config_default => sub { return \%config },
+    _debug => sub { return $debug },
+})) {
+    die("Error registering API: $libvideosite::error");
 }
 
-foreach (@g, $f) {
-    $_->setio(sub { printf(@_); print("\n"); } );
-    $_->setconn(\&connectors);
-
-    if ($debug) {
-        $_->setdebug(1);
-    }
+unless(libvideosite::init()) {
+    die("Could not init libvideosite: $libvideosite::error");
 }
 
-$f->setval('FILEPATTERN', './%3$s' . $ext);
-
 foreach (@ARGV) {
-    foreach $y (@g) {
-        ($m, undef) = $y->get($_);
-        if (defined($m)) {
-            if ($info) {
-                foreach (keys(%{$m})) {
-                    printf("%s: %s\n", $_, defined($m->{$_})?$m->{$_}:'(undef)');
-                }
-            } else {
-                print("Downloading $m->{'TITLE'}\n");
-                $f->get($m);
-            }
-        }
-    }
+    printf("Handling %s...\n", $_);
+    libvideosite::check_for_link({
+        message => $_,
+        io => sub { print @_, "\n" },
+        window => "",
+    });
 }