- Add videosite-dl.pl, which allows downloads from the command line
[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 Cwd qw(realpath);
8
9 sub ploader {
10
11     my $dir = shift;
12     my $pattern = shift;
13     my $type = shift;
14     my @list;
15     my $p;
16     my $g;
17     my @g = ();
18
19     unshift(@INC, $dir);
20
21     opendir(D, $dir) || return ();
22     @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
23     closedir(D);
24
25     foreach $p (@list) {
26         $p =~ s/\.pm$//;
27         eval qq{ require $p; };
28         if ($@) {
29             print("Failed to load plugin: $@");
30             next;
31         }
32
33         $g = eval $p.q{->new();};
34         if ($@) {
35             print("Failed to instanciate: $@");
36             delete($INC{$p});
37             next;
38         }
39
40         if ($type eq $g->{'TYPE'}) {
41             push(@g, $g);
42         } else {
43             printf('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
44             delete($INC{$p});
45         }
46     }
47
48     return @g;
49 }
50
51 my $hq = 0;
52 my $ext = '.flv';
53 my $y;
54 my $f;
55 my $m;
56 my @g;
57 my $bp;
58
59 # This is some dark magic to find out our real base directory,
60 # where we hope to find our plugins.
61 $bp = File::Spec->catdir(dirname(realpath($0)), 'videosite');
62
63 @g = ploader($bp, '.*Grabber\.pm$', 'grabber');
64 ($f) = ploader($bp, '^FileGetter\.pm$', 'getter');
65
66 unless(defined(@g) and defined($f)) {
67     print("No plugins could be loaded\n");
68     exit 1;
69 }
70
71 $f->setval('FILEPATTERN', './%3$s' . $ext);
72
73 foreach (@ARGV) {
74     foreach $y (@g) {
75         ($m, undef) = $y->get($_);
76         if (defined($m)) {
77             print("Downloading $m->{'TITLE'}\n");
78             $f->get($m);
79         }
80     }
81 }