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);
20 use JSON -support_by_pp;
21 use File::Temp qw(tempfile);
25 @EXPORT_OK = qw(init register_api check_for_link);
30 my %debugwindows = ();
34 my %builtin_config = ();
35 my $builtin_config_path;
36 my $builtin_config_default;
40 # The default config. These values will be set in the config
41 # if they do not exist already.
44 'getter' => 'filegetter',
46 'active-connectors' => 'direct',
47 'defined-connectors' => 'direct',
55 'config-version' => '2',
59 # This is a list of default values for the remote API. These
60 # are used if the values are not registered by the library user.
63 io => sub { print @_, "\n" },
64 config_init => \&_builtin_config_init,
65 config_get => \&_builtin_config_get,
66 config_set => \&_builtin_config_set,
67 config_has => \&_builtin_config_has,
68 config_save => \&_builtin_config_save,
69 config_del => \&_builtin_config_del,
70 color => sub { return '' },
71 module_path => sub { return dirname(realpath($0)) },
72 quote => sub { return $_ },
77 # List of known commands and handlers
79 my $videosite_commands = {
109 $remote_api->{reload}->();
130 # Output a string on the client.
131 # Works like (s)printf in that it takes a format string and a list of
132 # values to be replaced. Undefined values will be printed as '(undef)'
134 # All parameters (except for the format string itself) will be quoted
135 # using the client specific quote function
141 @text = ('') unless(@text);
143 # This will define the outputprefix once, so we don't have
144 # do do this every time.
145 $outputprefix = sprintf("%svideosite: %s",
146 _colorpair('magenta'),
147 _colorpair()) unless(defined($outputprefix));
148 $format = $outputprefix . shift(@text);
151 # The format string is assumed to be appropriately quoted.
152 # Quote the rest of the text, replacing undefined strings by (undef)
154 @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
156 $outputstack[0]->{io}->(sprintf($format, @text));
160 # Recursively walk through a hash-of-hashes, calling the given function
161 # for each found leaf with the path to the leaf
163 sub _recursive_hash_walk {
165 my $callback = shift;
168 foreach (keys(%{$hash})) {
169 if (ref($hash->{$_}) eq 'HASH') {
170 _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
172 $callback->([@path, $_], $hash->{$_});
178 # Return the color code for the given foreground/background color
179 # pair. Both can be undef, which means "default"
184 $fg = defined($fg)?$fg:'default';
185 $bg = defined($bg)?$bg:'default';
187 return $remote_api->{color}->($fg, $bg);
191 # Sets the given config item if it is not set already
193 sub _init_config_item {
197 unless(_config_has($path)) {
198 _config_set($path, $value);
203 # Print a message if debug is enabled
208 $data[0] = "DEBUG: " . $data[0];
210 # Check for global debug
214 # Check if current window is in the per-window-debug list
215 if (exists($debugwindows{$outputstack[0]->{window}})) {
222 # Load a list of modules matching a pattern from a given directory.
234 opendir(D, $dir) || return ();
235 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
239 _debug("Trying to load $p:");
242 load "videosite::$p";
245 _io("Failed to load plugin: $@");
250 $g = "videosite::$p"->new();
253 _io("Failed to instanciate: $@");
258 _debug("found $g->{'TYPE'} $g->{'NAME'}");
259 if ($type eq $g->{'TYPE'}) {
263 io_debug => \&_debug,
264 connectors => sub { return _connectorlist('active-connectors') },
265 config_get => \&_config_get,
266 config_set => \&_config_set,
267 config_has => \&_config_has,
270 _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
275 _debug("Loaded %d plugins", $#g+1);
281 # Populate the @grabbers and @getters lists from the given
284 sub _load_modules($) {
288 foreach (keys(%INC)) {
289 if ($INC{$_} =~ m|^$path|) {
290 _debug("Removing %s from \$INC", $_);
294 @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
295 @getters = _ploader($path, '.*Getter\.pm$', 'getter');
299 # Wrapper functions for config management to put in
306 $value = $remote_api->{config_get}->($path);
307 _debug("config: getting %s=%s", join('.', @{$path}), $value);
316 _debug("config: setting %s=%s", join('.', @{$path}), $value);
317 return $remote_api->{config_set}->($path, $value);
324 $b = $remote_api->{config_has}->($path);
325 _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false');
333 _debug("config: removing %s", join('.', @{$path}));
334 $remote_api->{config_del}->($path);
338 # The _config_list_* are helper functions taking a path to a comma separated
339 # string. The string is interpreted as a list and the action performed
340 # on it, storing back the modified version
344 # Add an item to the list, checking for duplicates
346 sub _config_list_add {
351 if (_config_has($path)) {
352 @c = split(/\s*,\s*/, _config_get($path));
357 _debug("Adding %s to list %s", $item, join(".", @{$path}));
358 unless(grep { $_ eq $item } @c) {
362 _config_set($path, join(',', @c));
366 # Remove an item from the list
368 sub _config_list_del {
373 unless(_config_has($path)) {
377 _debug("Removing %s from list %s", $item, join('.', @{$path}));
378 @c = grep { $item ne $_ } split(/\s*,\s*/, _config_get($path));
380 _config_set($path, join(',', @c));
384 # Return true if the item contains the given list, false otherwise
386 sub _config_list_has {
390 unless(_config_has($path)) {
394 _debug("Checking for %s in list %s", $item, join('.', @{$path}));
396 return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
400 # Replace a list with the given items
402 sub _config_list_set {
405 _debug("Replacing %s with (%s)", join('.', @{$path}), join(",", @_));
407 _config_set($path, join(',', @_));
411 # Return the list of currently active connectors, in the configured
418 foreach(split(/,/, _config_get([$key]))) {
419 push(@c, _unserialize_connector_hash($_));
426 # Convert a connector hash from it's config structure back to a perl
429 sub _unserialize_connector_hash {
433 if (_config_has(['connectors', $name, 'name'])) {
434 $connector->{name} = _config_get(['connectors', $name, 'name']);
435 $connector->{schemas} = {};
436 foreach ('http', 'https') {
437 if (_config_has(['connectors', $name, 'schemas', $_])) {
438 $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
443 _debug("Returning connector %s: %s", $name, Dumper($connector));
449 # Push a new output function on the IO stack.
452 unshift(@outputstack, shift);
456 # Pop the topmost output function from the stack, leaving
457 # at least one function on it.
460 if (scalar(@outputstack) > 0) {
466 # Takes a string and replaces commonly used URL shorteners recursively,
467 # up to 10 levels deep
469 sub _expand_url_shortener {
473 'is\.gd/[[:alnum:]]+',
474 'otf\.me/[[:alnum:]]+',
475 'hel\.me/[[:alnum:]]+',
476 '7ax\.de/[[:alnum:]]+',
477 'ow\.ly/[[:alnum:]]+',
478 'j\.mp/[[:alnum:]]+',
479 'bit\.ly/[[:alnum:]]+',
480 'tinyurl\.com/[[:alnum:]]+',
481 'pop\.is/[[:alnum:]]+',
482 'post\.ly/[[:alnum:]]+',
483 '1\.ly/[[:alnum:]]+',
484 '2\.ly/[[:alnum:]]+',
485 't\.co/[[:alnum:]]+',
486 'shar\.es/[[:alnum:]]+',
487 'goo\.gl/[[:alnum:]]+',
489 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
492 OUTER: while (($os ne $s) and ($i > 0)) {
497 foreach my $pattern (@urlshortener) {
498 my $p = "https?:\/\/" . $pattern;
500 _debug("Matching %s against %s", $p, $s);
505 _debug("Found %s", $matched);
506 $res = $ua->head($matched);
507 if ($res->is_redirect()) {
508 my $new = $res->headers()->header("Location");
510 _debug("Replacing %s with %s", $matched, $new);
511 $s =~ s/$matched/$new/;
514 _debug("Error resolving %s", $matched);
521 _debug("Loop terminated by counter");
524 _debug("Final string: %s", $s);
530 # Save the config to durable storage
535 if ($remote_api->{config_save}->()) {
538 _io(sprintf("%sConfig save failed%s", _colorpair("*red"), _colorpair()));
543 # Set a configuration element
552 foreach $p (@getters, @grabbers) {
553 if ($p->{'NAME'} eq $target) {
554 $p->setval($key, $val);
558 _io('No such module');
563 # Enable a given module
570 foreach $p (@grabbers) {
571 if ($p->{'NAME'} eq $target) {
576 _io('No such module');
580 # Disable given module
587 foreach $p (@grabbers) {
588 if ($p->{'NAME'} eq $target) {
593 _io('No such module');
597 # Show settings for modules
605 if (defined($target)) {
606 foreach $p (@getters, @grabbers) {
607 if ($p->{'NAME'} eq $target) {
608 _io($p->getconfstr());
612 _io('No such module');
614 _io('Loaded grabbers (* denotes enabled modules):');
615 foreach $p (@grabbers) {
616 $e = $p->_getval('enabled');
617 _io(' %s%s', $p->{'NAME'}, $e?'*':'');
620 _io('Loaded getters:');
621 foreach $p (@getters) {
622 _io(' %s', $p->{'NAME'});
628 # Show help for the commands
635 if (defined($target)) {
636 foreach $p (@getters, @grabbers) {
637 if ($p->{'NAME'} eq $target) {
638 _io($p->gethelpstr());
642 _io('No such module');
646 save: save the current configuration
647 help [modulename]: display this help, or module specific help
648 show [modulename]: show loaded modules, or the current parameters of a module
649 set modulename parameter value: set a module parameter to a new value
650 getter [modulename]: display or set the getter to use
651 enable [modulename]: enable the usage of this module (grabbers only)
652 disable [modulename]: disable the usage of this module (grabbers only)
653 reload: reload all modules (this is somewhat experimental)
654 mode [modename]: display or set the operation mode (download/display)
655 connector [subcommand]: manage connectors (proxies)
656 debug: enable debugging messages
657 nodebug: disable debugging messages
663 # Set the getter to use
670 if (defined($target)) {
671 foreach $p (@getters) {
672 if ($p->{'NAME'} eq $target) {
674 _config_set(['getter'], $target);
675 _io("Getter changed to %s", $target);
679 _io('No such getter');
681 _io('Current getter: %s', _config_get(['getter']));
686 # Show/set the working mode
692 if (defined($mode)) {
694 if (('download' eq $mode) or ('display' eq $mode)) {
695 _config_set(['mode'], $mode);
696 _io('Now using %s mode', $mode);
698 _io('Invalid mode: %s', $mode);
701 _io('Current mode: %s', _config_get(['mode']));
707 # Manage the connectors
714 unless(defined($subcmd)) {
718 $subcmd = lc($subcmd);
720 if ($subcmd eq 'list') {
721 _io("Defined connectors");
722 foreach $c (_connectorlist('defined-connectors')) {
724 my $schemas = $c->{schemas};
725 if (scalar(keys(%{$schemas})) == 0) {
726 _io(" No schemas defined");
728 foreach (keys(%{$schemas})) {
729 _io(' %s: %s', $_, $schemas->{$_});
735 _io("Selected connectors: %s", _config_get(['active-connectors']));
736 } elsif ($subcmd eq 'add') {
739 unless(defined($name)) {
740 _io("No name given");
746 unless($name =~ m|^[a-z]+$|) {
747 _io("%s is not a valid connector name (only letters are allowed)", $name);
751 if (_config_list_has(['defined-connectors'], $name)) {
752 _io("Connector already exists");
756 _config_set(['connectors', $name, 'name'], $name);
757 _config_list_add(['defined-connectors'], $name);
758 } elsif ($subcmd eq 'del') {
762 unless(defined($name)) {
763 _io("No name given");
767 unless (_config_list_has(['defined-connectors'], $name)) {
768 _io("Connector does not exist");
772 if (_config_has(['connectors', $name, '_immutable'])) {
773 _io("Connector cannot be removed");
777 # Remove from list of active connectors
778 _config_list_del(['defined-connectors'], $name);
779 _config_list_del(['active-connectors'], $name);
781 _config_del(['connectors', $name, 'name']);
782 _config_del(['connectors', $name, '_immutable']);
783 _config_del(['connectors', $name, 'schemas', 'http']);
784 _config_del(['connectors', $name, 'schemas', 'https']);
786 @dcon = split(/,/, _config_get(['active-connectors']));
788 if (scalar(@dcon) == 0) {
789 _io("List of selected connectors is empty, resetting to direct");
790 _config_list_add(['active-connectors', 'direct']);
792 } elsif ($subcmd eq 'addschema') {
793 my ($conn, $schema, $proxy) = @_;
795 unless(defined($conn)) {
796 _io("No connector name given");
800 unless(defined($schema)) {
801 _io("No schema given");
805 unless(defined($proxy)) {
806 _io("No proxy given");
811 unless(_config_list_has(['defined-connectors'], $conn)) {
812 _io("Connector does not exist");
816 if (_config_has(['connectors', $conn, '_immutable'])) {
817 _io("Connector cannot be modified");
821 $schema = lc($schema);
822 _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
823 } elsif ($subcmd eq 'delschema') {
824 my ($conn, $schema) = @_;
826 unless(defined($conn)) {
827 _io("No connector name given");
831 unless(defined($schema)) {
832 _io("No schema given");
837 unless(_config_list_has(['defined-connectors'], $conn)) {
838 _io("Connector does not exist");
842 $schema = lc($schema);
843 _config_del(['connectors', $conn, 'schemas', $schema]);
844 } elsif ($subcmd eq 'select') {
845 my @connlist = map { lc } @_;
847 if (scalar(@connlist) == 0) {
848 _io("No connectors given");
852 foreach (@connlist) {
853 unless(_config_list_has(['defined-connectors'], $_)) {
854 _io("Connector %s does not exist", $_);
859 _config_list_set(['active-connectors'], @connlist);
861 _io("connector [list|add|del|addschema|delschema|help] <options>");
862 _io(" help: Show this help");
863 _io(" list: List the defined connectors");
864 _io(" add <name>: Add a connector with name <name>");
865 _io(" del <name>: Delete the connector with name <name>");
866 _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
867 _io(" delschema <name> <schema>: Remove the schema from the connector");
868 _io(" select <name> [<name>...]: Select the connectors to use");
874 # Global debug if the keyword "all" is given, or just for the
875 # current window otherwise
881 if (defined($scope) and (lc($scope) eq 'all')) {
882 _io("Global debug enabled");
885 _io("Debug for this window enabled");
886 $debugwindows{$event->{window}} = 1;
892 # Disable global debug if the keyword "all" is given (this will
893 # also disable all per-window debugs) or just for the current
900 if (defined($scope) and (lc($scope) eq 'all')) {
903 _io("Global debug disabled");
905 delete($debugwindows{$event->{window}});
906 _io("Debug for this window disabled");
912 # Return the list of loaded grabbers.
913 # This is used by the test programs, and is not meant to be
921 # ==============================================
922 # Builtin config handling functions
923 # These are used if the library used does not
924 # register it's own config_* handlers
925 # ==============================================
927 sub _builtin_config_init {
929 if (defined($builtin_config_path)) {
930 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
932 _debug("Trying to load configuration from %s", $filename);
937 open(CONF, '<', $filename);
938 %builtin_config = %{JSON->new->utf8->decode(<CONF>)};
941 _io("Error loading configuration: %s", $@);
944 } elsif (defined($builtin_config_default)) {
945 _debug("Initializing builtin config from external default");
946 foreach (keys(%{$builtin_config_default})) {
947 _debug("Setting %s=%s", $_, $builtin_config_default->{$_});
948 $builtin_config{$_} = $builtin_config_default->{$_};
953 sub _builtin_config_get {
954 return $builtin_config{join(".", @{$_[0]})};
957 sub _builtin_config_set {
958 $builtin_config{join(".", @{$_[0]})} = $_[1];
961 sub _builtin_config_has {
962 return exists($builtin_config{join(".", @{$_[0]})});
965 sub _builtin_config_save {
967 if (defined($builtin_config_path)) {
968 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
970 _debug("Attempting to save config to %s", $filename);
973 my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path);
974 print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config);
976 rename($tempfn, $filename);
985 sub _builtin_config_del {
986 delete($builtin_config{join(".", @{$_[0]})});
990 # ==============================================
991 # From this point on publicly callable functions
992 # ==============================================
997 # Initialization function for the library
998 # Actually not the first thing to be called, it expects an API
999 # has (register_api) to be registered first
1002 unless($remote_api) {
1003 $error = "No API set";
1007 # Initialize configuration data
1008 $remote_api->{config_init}->();
1010 # Check/create default values, if they do not exist
1011 _recursive_hash_walk($defaultconfig, \&_init_config_item);
1014 _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
1016 unless (@grabbers && @getters) {
1017 _io('No grabbers or no getters found, can not proceed.');
1022 $getter = $getters[0];
1023 foreach my $p (@getters) {
1024 if (_config_get(['getter']) eq $p->{'NAME'}) {
1028 _debug('Selected %s as getter', $getter->{'NAME'});
1029 _config_set(['getter'], $getter->{'NAME'});
1032 _io('initialized successfully');
1037 # Register a remote API. This API contains a basic output function (used
1038 # when no window specific function is available), some config functions
1039 # and a color code function.
1043 my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
1047 unless(defined($a)) {
1048 die("No API given");
1052 # The config_* handlers are special in that they either all have
1053 # provided by the user, or none. In the latter case builtin
1054 # handlers will be used, but the config will not persist.
1057 foreach (@config_functions) {
1058 if (exists($a->{$_})) {
1065 unless (($c == 0) or ($c == scalar(@config_functions))) {
1066 $error = sprintf("Missing config function: %s", $missing[0]);
1070 foreach (keys(%{$a})) {
1071 if (ref($a->{$_}) ne 'CODE') {
1072 $error = sprintf("API handler %s is not a subroutine reference", $_);
1074 $remote_api->{$_} = $a->{$_};
1077 if (exists($a->{_debug})) {
1078 $debug = $a->{_debug}->();
1081 if (exists($a->{_config_path})) {
1082 $builtin_config_path = $a->{_config_path}->();
1085 if (exists($a->{_config_default})) {
1086 $builtin_config_default = $a->{_config_default}->();
1089 @outputstack = ({io => $remote_api->{'io'}, window => ""});
1095 # Check a message for useable links
1097 sub check_for_link {
1099 my $message = $event->{message};
1104 my $mode = _config_get(['mode']);
1108 # If /nosave is present in the message switch to display mode, regardless
1111 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
1115 _push_output($event);
1116 $message = _expand_url_shortener($message);
1120 # Offer the message to all Grabbers in turn
1121 GRABBER: foreach $g (@grabbers) {
1122 ($m, $p) = $g->get($message);
1123 while (defined($m)) {
1124 _debug('Metadata: %s', Dumper($m));
1126 if (exists($remote_api->{link_callback})) {
1127 $skip = $remote_api->{link_callback}->($m);
1130 if ('download' eq $mode) {
1132 sprintf('%s>>> %sSaving %s%%s%s %s%%s',
1135 _colorpair('*yellow'),
1137 _colorpair('*green'),
1142 unless($getter->get($m)) {
1143 _io(sprintf('%s>>> FAILED', _colorpair('*red')));
1145 } elsif ('display' eq $mode) {
1147 sprintf('%s>>> %sSaw %s%%s%s %s%%s',
1148 _colorpair('*magenta'),
1150 _colorpair('*yellow'),
1152 _colorpair('*green')
1158 _io(sprintf('%s>>> Invalid operation mode', _colorpair('*red')));
1162 # Remove the matched part from the message and try again (there may be
1166 last GRABBER if ($message =~ /^\s*$/);
1168 ($m, $p) = $g->get($message);
1176 # Handle a videosite command (/videosite ...) entered in the client
1178 sub handle_command {
1180 my ($cmd, @params) = split(/\s+/, $event->{message});
1182 _push_output($event);
1184 if (exists($videosite_commands->{$cmd})) {
1185 $videosite_commands->{$cmd}->($event, @params);