1 # library to autodownload flash videos
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
6 # Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
8 # based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
13 use vars qw(@ISA @EXPORT_OK);
23 @EXPORT_OK = qw(init register_api check_for_link);
31 my %builtin_config = ();
35 # The default config. These values will be set in the config
36 # if they do not exist already.
39 'getter' => 'filegetter',
41 'active-connectors' => 'direct',
42 'defined-connectors' => 'direct',
50 'config-version' => '2',
54 # List of known commands and handlers
56 my $videosite_commands = {
99 foreach (@grabbers, @getters) {
102 _io('Enabled debugging');
107 foreach (@grabbers, @getters) {
110 _io('Disabled debugging');
115 # This is a list of default values for the remote API. These
116 # are used if the values are not registered by the library user.
119 io => sub { print @_, "\n" },
120 config_init => \&_builtin_config_init,
121 config_get => \&_builtin_config_get,
122 config_set => \&_builtin_config_set,
123 config_has => \&_builtin_config_has,
124 config_save => \&_builtin_config_save,
125 config_del => \&_builtin_config_del,
126 color => sub { return '' },
127 module_path => sub { return dirname(realpath($0)) },
128 quote => sub { return $_ },
132 # Output a string on the client.
133 # Works like (s)printf in that it takes a format string and a list of
134 # values to be replaced. Undefined values will be printed as '(undef)'
136 # All parameters (except for the format string itself) will be quoted
137 # using the client specific quote function
143 @text = ('') unless(@text);
145 # This will define the outputprefix once, so we don't have
146 # do do this every time.
147 $outputprefix = sprintf("%svideosite: %s",
148 $remote_api->{color}->('magenta'),
149 $remote_api->{color}->()) unless(defined($outputprefix));
150 $format = $outputprefix . shift(@text);
153 # The format string is assumed to be appropriately quoted.
154 # Quote the rest of the text, replacing undefined strings by (undef)
156 @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
158 $outputstack[0]->(sprintf($format, @text));
162 # Recursively walk through a hash-of-hashes, calling the given function
163 # for each found leaf with the path to the leaf
165 sub _recursive_hash_walk {
167 my $callback = shift;
170 foreach (keys(%{$hash})) {
171 if (ref($hash->{$_}) eq 'HASH') {
172 _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
174 $callback->([@path, $_], $hash->{$_});
180 # Return the color code for the given foreground/background color
181 # pair. Both can be undef, which means "default"
186 $fg = defined($fg)?$fg:'default';
187 $bg = defined($bg)?$bg:'default';
189 return $remote_api->{color}->($fg, $bg);
193 # Sets the given config item if it is not set already
195 sub _init_config_item {
199 unless(_config_has($path)) {
200 _config_set($path, $value);
205 # Print a message if debug is enabled
214 # Load a list of modules matching a pattern from a given directory.
226 opendir(D, $dir) || return ();
227 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
231 _debug("Trying to load $p:");
234 load "videosite::$p";
237 _io("Failed to load plugin: $@");
242 $g = "videosite::$p"->new();
245 _io("Failed to instanciate: $@");
250 _debug("found $g->{'TYPE'} $g->{'NAME'}");
251 if ($type eq $g->{'TYPE'}) {
255 connectors => sub { return _connectorlist('active-connectors') },
256 config_get => \&_config_get,
257 config_set => \&_config_set,
258 config_has => \&_config_has,
260 $g->setdebug($debug);
262 _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
267 _debug("Loaded %d plugins", $#g+1);
273 # Populate the @grabbers and @getters lists from the given
276 sub _load_modules($) {
280 foreach (keys(%INC)) {
281 if ($INC{$_} =~ m|^$path|) {
282 _debug("Removing %s from \$INC", $_);
286 @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
287 @getters = _ploader($path, '.*Getter\.pm$', 'getter');
291 # Wrapper functions for config management to put in
298 $value = $remote_api->{config_get}->($path);
299 _debug("config: getting %s=%s", join('.', @{$path}), $value);
308 _debug("config: setting %s=%s", join('.', @{$path}), $value);
309 return $remote_api->{config_set}->($path, $value);
316 $b = $remote_api->{config_has}->($path);
317 _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false');
323 # The _config_list_* are helper functions taking a path to a comma separated
324 # string. The string is interpreted as a list and the action performed
325 # on it, storing back the modified version
329 # Add an item to the list, checking for duplicates
331 sub _config_list_add {
336 if (_config_has($path)) {
337 @c = split(/\s*,\s*/, _config_get($path));
342 _debug("Adding %s to list %s", $item, join(".", $path));
343 unless(grep { $_ eq $item } @c) {
347 _config_set($path, join(',', @c));
351 # Remove an item from the list
353 sub _config_list_del {
358 unless(_config_has($path)) {
362 _debug("Removing %s from list %s", $item, join('.', $path));
363 @c = map { $item ne $_ } split(/\s*,\s*/, _config_get($path));
365 _config_set($path, join('.', @c));
369 # Return true if the item contains the given list, false otherwise
371 sub _config_list_has {
375 unless(_config_has($path)) {
379 _debug("Checking for %s in list %s", $item, join('.', $path));
381 return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
385 # Replace a list with the given items
387 sub _config_list_set {
390 _debug("Replacing %s with (%s)", join('.', $path), join(",", @_));
392 _config_set($path, join(',', @_));
396 # Return the list of currently active connectors, in the configured
403 foreach(split(/,/, _config_get([$key]))) {
404 push(@c, _unserialize_connector_hash($_));
411 # Convert a connector hash from it's config structure back to a perl
414 sub _unserialize_connector_hash {
418 if (_config_has(['connectors', $name, 'name'])) {
419 $connector->{name} = _config_get(['connectors', $name, 'name']);
420 $connector->{schemas} = {};
421 foreach ('http', 'https') {
422 if (_config_has(['connectors', $name, 'schemas', $_])) {
423 $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
428 _debug("Returning connector %s: %s", $name, Dumper($connector));
434 # Push a new output function on the IO stack.
437 unshift(@outputstack, shift);
441 # Pop the topmost output function from the stack, leaving
442 # at least one function on it.
445 if (scalar(@outputstack) > 0) {
451 # Takes a string and replaces commonly used URL shorteners recursively,
452 # up to 10 levels deep
454 sub _expand_url_shortener {
458 'is\.gd/[[:alnum:]]+',
459 'otf\.me/[[:alnum:]]+',
460 'hel\.me/[[:alnum:]]+',
461 '7ax\.de/[[:alnum:]]+',
462 'ow\.ly/[[:alnum:]]+',
463 'j\.mp/[[:alnum:]]+',
464 'bit\.ly/[[:alnum:]]+',
465 'tinyurl\.com/[[:alnum:]]+',
466 'pop\.is/[[:alnum:]]+',
467 'post\.ly/[[:alnum:]]+',
468 '1\.ly/[[:alnum:]]+',
469 '2\.ly/[[:alnum:]]+',
470 't\.co/[[:alnum:]]+',
471 'shar\.es/[[:alnum:]]+',
472 'goo\.gl/[[:alnum:]]+',
474 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
477 OUTER: while (($os ne $s) and ($i > 0)) {
482 foreach my $pattern (@urlshortener) {
483 my $p = "https?:\/\/" . $pattern;
485 _debug("Matching %s against %s", $p, $s);
490 _debug("Found %s", $matched);
491 $res = $ua->head($matched);
492 if ($res->is_redirect()) {
493 my $new = $res->headers()->header("Location");
495 _debug("Replacing %s with %s", $matched, $new);
496 $s =~ s/$matched/$new/;
499 _debug("Error resolving %s", $matched);
506 _debug("Loop terminated by counter");
509 _debug("Final string: %s", $s);
515 # Save the config to durable storage
518 $remote_api->{config_save}->();
522 # Set a configuration element
530 foreach $p (@getters, @grabbers) {
531 if ($p->{'NAME'} eq $target) {
532 $p->setval($key, $val);
536 _io('No such module');
541 # Enable a given module
547 foreach $p (@grabbers) {
548 if ($p->{'NAME'} eq $target) {
553 _io('No such module');
557 # Disable given module
563 foreach $p (@grabbers) {
564 if ($p->{'NAME'} eq $target) {
569 _io('No such module');
573 # Show settings for modules
580 if (defined($target)) {
581 foreach $p (@getters, @grabbers) {
582 if ($p->{'NAME'} eq $target) {
583 _io($p->getconfstr());
587 _io('No such module');
589 _io('Loaded grabbers (* denotes enabled modules):');
590 foreach $p (@grabbers) {
591 $e = $p->_getval('enabled');
592 _io(' %s%s', $p->{'NAME'}, $e?'*':'');
595 _io('Loaded getters:');
596 foreach $p (@getters) {
597 _io(' %s', $p->{'NAME'});
603 # Show help for the commands
609 if (defined($target)) {
610 foreach $p (@getters, @grabbers) {
611 if ($p->{'NAME'} eq $target) {
612 _io($p->gethelpstr());
616 _io('No such module');
620 save: save the current configuration
621 help [modulename]: display this help, or module specific help
622 show [modulename]: show loaded modules, or the current parameters of a module
623 set modulename parameter value: set a module parameter to a new value
624 getter [modulename]: display or set the getter to use
625 enable [modulename]: enable the usage of this module (grabbers only)
626 disable [modulename]: disable the usage of this module (grabbers only)
627 reload: reload all modules (this is somewhat experimental)
628 mode [modename]: display or set the operation mode (download/display)
629 connector [subcommand]: manage connectors (proxies)
630 debug: enable debugging messages
631 nodebug: disable debugging messages
637 # Set the getter to use
643 if (defined($target)) {
644 foreach $p (@getters) {
645 if ($p->{'NAME'} eq $target) {
647 _config_set(['getter'], $target);
648 _io("Getter changed to %s", $target);
652 _io('No such getter');
654 _io('Current getter: %s', _config_get(['getter']));
659 # Show/set the working mode
664 if (defined($mode)) {
666 if (('download' eq $mode) or ('display' eq $mode)) {
667 _config_set(['mode'], $mode);
668 _io('Now using %s mode', $mode);
670 _io('Invalid mode: %s', $mode);
673 _io('Current mode: %s', _config_get(['mode']));
679 # Manage the connectors
685 unless(defined($subcmd)) {
689 $subcmd = lc($subcmd);
691 if ($subcmd eq 'list') {
692 _io("Defined connectors");
693 foreach $c (_connectorlist('defined-connectors')) {
695 my $schemas = $c->{schemas};
696 if (scalar(keys(%{$schemas})) == 0) {
697 _io(" No schemas defined");
699 foreach (keys(%{$schemas})) {
700 _io(' %s: %s', $_, $schemas->{$_});
706 _io("Selected connectors: %s", _config_get(['active-connectors']));
707 } elsif ($subcmd eq 'add') {
710 unless(defined($name)) {
711 _io("No name given");
717 if (_config_list_has(['defined-connectors'], $name)) {
718 _io("Connector already exists");
722 _config_set(['connectors', $name, 'name'], $name);
723 _config_list_add(['defined-connectors'], $name);
724 } elsif ($subcmd eq 'del') {
728 unless(defined($name)) {
729 _io("No name given");
733 unless (_config_list_has(['defined-connectors'])) {
734 _io("Connector does not exist");
738 if (_config_has(['connectors', $name, '_immutable'])) {
739 _io("Connector cannot be removed");
743 # Remove from list of active connectors
744 _config_list_del(['defined-connectors'], $name);
745 _config_list_del(['active-connectors'], $name);
747 _config_del(['connectors', $name, 'name']);
748 _config_del(['connectors', $name, '_immutable']);
749 _config_del(['connectors', $name, 'schemas', 'http']);
750 _config_del(['connectors', $name, 'schemas', 'https']);
752 @dcon = split(/,/, _config_get(['active-connectors']));
754 if (scalar(@dcon) == 0) {
755 _io("List of selected connectors is empty, resetting to direct");
756 _config_list_add(['active-connectors', 'direct']);
758 } elsif ($subcmd eq 'addschema') {
759 my ($conn, $schema, $proxy) = @_;
761 unless(defined($conn)) {
762 _io("No connector name given");
766 unless(defined($schema)) {
767 _io("No schema given");
771 unless(defined($proxy)) {
772 _io("No proxy given");
777 unless(_config_list_has(['defined-connectors'], $conn)) {
778 _io("Connector does not exist");
782 if (_config_has(['connectors', $conn, '_immutable'])) {
783 _io("Connector cannot be modified");
787 $schema = lc($schema);
788 _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
789 } elsif ($subcmd eq 'delschema') {
790 my ($conn, $schema) = @_;
792 unless(defined($conn)) {
793 _io("No connector name given");
797 unless(defined($schema)) {
798 _io("No schema given");
803 unless(_config_list_has(['defined-connectors'], $conn)) {
804 _io("Connector does not exist");
808 $schema = lc($schema);
809 _config_del(['connectors', $conn, 'schemas', $schema]);
810 } elsif ($subcmd eq 'select') {
811 my @connlist = map { lc } @_;
813 if (scalar(@connlist) == 0) {
814 _io("No connectors given");
818 foreach (@connlist) {
819 unless(_config_list_has(['defined-connectors'], $_)) {
820 _io("Connector %s does not exist", $_);
825 _config_list_set(['active-connectors'], @connlist);
827 _io("connector [list|add|del|addschema|delschema|help] <options>");
828 _io(" help: Show this help");
829 _io(" list: List the defined connectors");
830 _io(" add <name>: Add a connector with name <name>");
831 _io(" del <name>: Delete the connector with name <name>");
832 _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
833 _io(" delschema <name> <schema>: Remove the schema from the connector");
834 _io(" select <name> [<name>...]: Select the connectors to use");
839 # Return the list of loaded grabbers.
840 # This is used by the test programs, and is not meant to be
848 # ==============================================
849 # Builtin config handling functions
850 # These are used if the library used does not
851 # register it's own config_* handlers
852 # ==============================================
854 sub _builtin_config_init {
857 sub _builtin_config_get {
858 return $builtin_config{join(".", @{$_[0]})};
861 sub _builtin_config_set {
862 $builtin_config{join(".", @{$_[0]})} = $_[1];
865 sub _builtin_config_has {
866 return exists($builtin_config{join(".", @{$_[0]})});
869 sub _builtin_config_save {
872 sub _builtin_config_del {
873 delete($builtin_config{join(".", @{$_[0]})});
877 # ==============================================
878 # From this point on publicly callable functions
879 # ==============================================
884 # Initialization function for the library
885 # Actually not the first thing to be called, it expects an API
886 # has (register_api) to be registered first
889 unless($remote_api) {
890 $error = "No API set";
894 # Initialize configuration data
895 $remote_api->{config_init}->();
897 # Check/create default values, if they do not exist
898 _recursive_hash_walk($defaultconfig, \&_init_config_item);
901 _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
903 unless (@grabbers && @getters) {
904 _io('No grabbers or no getters found, can not proceed.');
909 $getter = $getters[0];
910 foreach my $p (@getters) {
911 if (_config_get(['getter']) eq $p->{'NAME'}) {
915 _debug('Selected %s as getter', $getter->{'NAME'});
916 _config_set(['getter'], $getter->{'NAME'});
919 _io('initialized successfully');
924 # Register a remote API. This API contains a basic output function (used
925 # when no window specific function is available), some config functions
926 # and a color code function.
930 my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
934 unless(defined($a)) {
939 # The config_* handlers are special in that they either all have
940 # provided by the user, or none. In the latter case builtin
941 # handlers will be used, but the config will not persist.
944 foreach (@config_functions) {
945 if (exists($a->{$_})) {
952 unless (($c == 0) or ($c == scalar(@config_functions))) {
953 $error = sprintf("Missing config function: %s", $missing[0]);
957 foreach (keys(%{$a})) {
958 if (ref($a->{$_}) ne 'CODE') {
959 $error = sprintf("API handler %s is not a subroutine reference", $_);
961 $remote_api->{$_} = $a->{$_};
964 if (exists($a->{_debug})) {
965 $debug = $a->{_debug}->();
968 @outputstack = ($remote_api->{'io'});
974 # Check a message for useable links
978 my $message = $event->{message};
984 # Look if we should ignore this line
985 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
989 _push_output($event->{ewpf});
990 $message = _expand_url_shortener($message);
994 # Offer the message to all Grabbers in turn
995 GRABBER: foreach $g (@grabbers) {
996 ($m, $p) = $g->get($message);
997 while (defined($m)) {
998 _debug('Metadata: %s', Dumper($m));
999 if (exists($remote_api->{link_callback})) {
1000 $remote_api->{link_callback}->($m);
1002 if ('download' eq _config_get(['mode'])) {
1004 sprintf('%s>>> %sSaving %s%%s%s %s%%s',
1005 $remote_api->{color}->('*red'),
1006 $remote_api->{color}->(),
1007 $remote_api->{color}->('*yellow'),
1008 $remote_api->{color}->(),
1009 $remote_api->{color}->('*green'),
1014 unless($getter->get($m)) {
1015 _io(sprintf('%s>>> FAILED', $remote_api->{color}->('*red')));
1017 } elsif ('display' eq _config_get(['mode'])) {
1019 sprintf('%s>>> %sSaw %s%%s%s %s%%s',
1020 $remote_api->{color}->('*magenta'),
1021 $remote_api->{color}->(),
1022 $remote_api->{color}->('*yellow'),
1023 $remote_api->{color}->(),
1024 $remote_api->{color}->('*green')
1030 _io(sprintf('%s>>> Invalid operation mode', $remote_api->{color}->('*red')));
1033 # Remove the matched part from the message and try again (there may be
1037 last GRABBER if ($message =~ /^\s*$/);
1039 ($m, $p) = $g->get($message);
1047 # Handle a videosite command (/videosite ...) entered in the client
1049 sub handle_command {
1051 my ($cmd, @params) = split(/\s+/, $event->{message});
1053 _push_output($event->{ewpf});
1055 if (exists($videosite_commands->{$cmd})) {
1056 $videosite_commands->{$cmd}->(@params);