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;
38 my %config_cache = ();
42 # The default config. These values will be set in the config
43 # if they do not exist already.
46 'getter' => 'filegetter',
48 'active-connectors' => 'direct',
49 'defined-connectors' => 'direct',
57 'config-version' => '2',
61 # This is a list of default values for the remote API. These
62 # are used if the values are not registered by the library user.
65 io => sub { print @_, "\n" },
66 config_init => \&_builtin_config_init,
67 config_get => \&_builtin_config_get,
68 config_set => \&_builtin_config_set,
69 config_has => \&_builtin_config_has,
70 config_save => \&_builtin_config_save,
71 config_del => \&_builtin_config_del,
72 color => sub { return '' },
73 module_path => sub { return dirname(realpath($0)) },
74 quote => sub { return $_ },
79 # List of known commands and handlers
81 my $videosite_commands = {
111 $remote_api->{reload}->();
136 # Output a string on the client.
137 # Works like (s)printf in that it takes a format string and a list of
138 # values to be replaced. Undefined values will be printed as '(undef)'
140 # All parameters (except for the format string itself) will be quoted
141 # using the client specific quote function
147 @text = ('') unless(@text);
149 # This will define the outputprefix once, so we don't have
150 # do do this every time.
151 $outputprefix = sprintf("%svideosite: %s",
152 _colorpair('magenta'),
153 _colorpair()) unless(defined($outputprefix));
154 $format = $outputprefix . shift(@text);
157 # The format string is assumed to be appropriately quoted.
158 # Quote the rest of the text, replacing undefined strings by (undef)
160 @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
162 $outputstack[0]->{io}->(sprintf($format, @text));
166 # Recursively walk through a hash-of-hashes, calling the given function
167 # for each found leaf with the path to the leaf
169 sub _recursive_hash_walk {
171 my $callback = shift;
174 foreach (keys(%{$hash})) {
175 if (ref($hash->{$_}) eq 'HASH') {
176 _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
178 $callback->([@path, $_], $hash->{$_});
184 # Return the color code for the given foreground/background color
185 # pair. Both can be undef, which means "default"
190 $fg = defined($fg)?$fg:'default';
191 $bg = defined($bg)?$bg:'default';
193 return $remote_api->{color}->($fg, $bg);
197 # Sets the given config item if it is not set already
199 sub _init_config_item {
203 unless(_config_has($path)) {
204 _config_set($path, $value);
209 # Print a message if debug is enabled
214 $data[0] = "DEBUG: " . $data[0];
216 # Check for global debug
220 # Check if current window is in the per-window-debug list
221 if (exists($debugwindows{$outputstack[0]->{window}})) {
228 # Load a list of modules matching a pattern from a given directory.
240 opendir(D, $dir) || return ();
241 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
245 _debug("Trying to load $p:");
248 load "videosite::$p";
251 _io("Failed to load plugin: $@");
256 $g = "videosite::$p"->new();
259 _io("Failed to instanciate: $@");
264 _debug("found $g->{'TYPE'} $g->{'NAME'}");
265 if ($type eq $g->{'TYPE'}) {
269 io_debug => \&_debug,
270 connectors => sub { return _connectorlist('active-connectors') },
271 config_get => \&_config_get,
272 config_set => \&_config_set,
273 config_has => \&_config_has,
276 _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
281 _debug("Loaded %d plugins", $#g+1);
287 # Populate the @grabbers and @getters lists from the given
290 sub _load_modules($) {
294 foreach (keys(%INC)) {
295 if ($INC{$_} =~ m|^$path|) {
296 _debug("Removing %s from \$INC", $_);
300 @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
301 @getters = _ploader($path, '.*Getter\.pm$', 'getter');
305 # Wrapper functions for config management to put in
310 my $dotpath = join('.', @{$path});
313 if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{value})) {
314 $value = $config_cache{$dotpath}->{value};
316 $value = $remote_api->{config_get}->($path);
317 $config_cache{$dotpath} = {value => $value, has => 1};
321 _debug("config: getting %s=%s", $dotpath, $value);
327 my $dotpath = join('.', @{$path});
330 _debug("config: setting %s=%s", $dotpath, $value);
331 $config_cache{$dotpath} = {value => $value, has => 1};
332 return $remote_api->{config_set}->($path, $value);
337 my $dotpath = join('.', @{$path});
340 if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{has})) {
341 $b = $config_cache{$dotpath}->{has};
343 $b = $remote_api->{config_has}->($path);
344 $config_cache{$dotpath}->{has} = $b;
347 _debug("config: testing %s (%s)", $dotpath, $b?'true':'false');
353 my $dotpath = join('.', @{$path});
355 _debug("config: removing %s", $dotpath);
356 delete($config_cache{$dotpath});
357 $remote_api->{config_del}->($path);
361 # The _config_list_* are helper functions taking a path to a comma separated
362 # string. The string is interpreted as a list and the action performed
363 # on it, storing back the modified version
367 # Add an item to the list, checking for duplicates
369 sub _config_list_add {
374 if (_config_has($path)) {
375 @c = split(/\s*,\s*/, _config_get($path));
380 _debug("Adding %s to list %s", $item, join(".", @{$path}));
381 unless(grep { $_ eq $item } @c) {
385 _config_set($path, join(',', @c));
389 # Remove an item from the list
391 sub _config_list_del {
396 unless(_config_has($path)) {
400 _debug("Removing %s from list %s", $item, join('.', @{$path}));
401 @c = grep { $item ne $_ } split(/\s*,\s*/, _config_get($path));
403 _config_set($path, join(',', @c));
407 # Return true if the item contains the given list, false otherwise
409 sub _config_list_has {
413 unless(_config_has($path)) {
417 _debug("Checking for %s in list %s", $item, join('.', @{$path}));
419 return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
423 # Replace a list with the given items
425 sub _config_list_set {
428 _debug("Replacing %s with (%s)", join('.', @{$path}), join(",", @_));
430 _config_set($path, join(',', @_));
434 # Return the list of currently active connectors, in the configured
441 foreach(split(/,/, _config_get([$key]))) {
442 push(@c, _unserialize_connector_hash($_));
449 # Convert a connector hash from it's config structure back to a perl
452 sub _unserialize_connector_hash {
456 if (_config_has(['connectors', $name, 'name'])) {
457 $connector->{name} = _config_get(['connectors', $name, 'name']);
458 $connector->{schemas} = {};
459 foreach ('http', 'https') {
460 if (_config_has(['connectors', $name, 'schemas', $_])) {
461 $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
466 _debug("Returning connector %s: %s", $name, Dumper($connector));
472 # Push a new output function on the IO stack.
475 unshift(@outputstack, shift);
479 # Pop the topmost output function from the stack, leaving
480 # at least one function on it.
483 if (scalar(@outputstack) > 0) {
489 # Takes a string and replaces commonly used URL shorteners recursively,
490 # up to 10 levels deep
492 sub _expand_url_shortener {
496 'is\.gd/[[:alnum:]]+',
497 'otf\.me/[[:alnum:]]+',
498 'hel\.me/[[:alnum:]]+',
499 '7ax\.de/[[:alnum:]]+',
500 'ow\.ly/[[:alnum:]]+',
501 'j\.mp/[[:alnum:]]+',
502 'bit\.ly/[[:alnum:]]+',
503 'tinyurl\.com/[[:alnum:]]+',
504 'pop\.is/[[:alnum:]]+',
505 'post\.ly/[[:alnum:]]+',
506 '1\.ly/[[:alnum:]]+',
507 '2\.ly/[[:alnum:]]+',
508 't\.co/[[:alnum:]]+',
509 'shar\.es/[[:alnum:]]+',
510 'goo\.gl/[[:alnum:]]+',
512 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
515 OUTER: while (($os ne $s) and ($i > 0)) {
520 foreach my $pattern (@urlshortener) {
521 my $p = "https?:\/\/" . $pattern;
523 _debug("Matching %s against %s", $p, $s);
528 _debug("Found %s", $matched);
529 $res = $ua->head($matched);
530 if ($res->is_redirect()) {
531 my $new = $res->headers()->header("Location");
533 _debug("Replacing %s with %s", $matched, $new);
534 $s =~ s/$matched/$new/;
537 _debug("Error resolving %s", $matched);
544 _debug("Loop terminated by counter");
547 _debug("Final string: %s", $s);
553 # Save the config to durable storage
558 if ($remote_api->{config_save}->()) {
561 _io(sprintf("%sConfig save failed%s", _colorpair("*red"), _colorpair()));
566 # Set a configuration element
575 foreach $p (@getters, @grabbers) {
576 if ($p->{'NAME'} eq $target) {
577 $p->setval($key, $val);
581 _io('No such module');
586 # Enable a given module
593 foreach $p (@grabbers) {
594 if ($p->{'NAME'} eq $target) {
599 _io('No such module');
603 # Disable given module
610 foreach $p (@grabbers) {
611 if ($p->{'NAME'} eq $target) {
616 _io('No such module');
620 # Show settings for modules
628 if (defined($target)) {
629 foreach $p (@getters, @grabbers) {
630 if ($p->{'NAME'} eq $target) {
631 _io($p->getconfstr());
635 _io('No such module');
637 _io('Loaded grabbers (* denotes enabled modules):');
638 foreach $p (@grabbers) {
639 $e = $p->_getval('enabled');
640 _io(' %s%s', $p->{'NAME'}, $e?'*':'');
643 _io('Loaded getters:');
644 foreach $p (@getters) {
645 _io(' %s', $p->{'NAME'});
651 # Show help for the commands
658 if (defined($target)) {
659 foreach $p (@getters, @grabbers) {
660 if ($p->{'NAME'} eq $target) {
661 _io($p->gethelpstr());
665 _io('No such module');
669 save: save the current configuration
670 help [modulename]: display this help, or module specific help
671 show [modulename]: show loaded modules, or the current parameters of a module
672 set modulename parameter value: set a module parameter to a new value
673 getter [modulename]: display or set the getter to use
674 enable [modulename]: enable the usage of this module (grabbers only)
675 disable [modulename]: disable the usage of this module (grabbers only)
676 reload: reload all modules (this is somewhat experimental)
677 mode [modename]: display or set the operation mode (download/display)
678 connector [subcommand]: manage connectors (proxies)
679 debug: enable debugging messages
680 nodebug: disable debugging messages
686 # Set the getter to use
693 if (defined($target)) {
694 foreach $p (@getters) {
695 if ($p->{'NAME'} eq $target) {
697 _config_set(['getter'], $target);
698 _io("Getter changed to %s", $target);
702 _io('No such getter');
704 _io('Current getter: %s', _config_get(['getter']));
709 # Show/set the working mode
715 if (defined($mode)) {
717 if (('download' eq $mode) or ('display' eq $mode)) {
718 _config_set(['mode'], $mode);
719 _io('Now using %s mode', $mode);
721 _io('Invalid mode: %s', $mode);
724 _io('Current mode: %s', _config_get(['mode']));
730 # Manage the connectors
737 unless(defined($subcmd)) {
741 $subcmd = lc($subcmd);
743 if ($subcmd eq 'list') {
744 _io("Defined connectors");
745 foreach $c (_connectorlist('defined-connectors')) {
747 my $schemas = $c->{schemas};
748 if (scalar(keys(%{$schemas})) == 0) {
749 _io(" No schemas defined");
751 foreach (keys(%{$schemas})) {
752 _io(' %s: %s', $_, $schemas->{$_});
758 _io("Selected connectors: %s", _config_get(['active-connectors']));
759 } elsif ($subcmd eq 'add') {
762 unless(defined($name)) {
763 _io("No name given");
769 unless($name =~ m|^[a-z]+$|) {
770 _io("%s is not a valid connector name (only letters are allowed)", $name);
774 if (_config_list_has(['defined-connectors'], $name)) {
775 _io("Connector already exists");
779 _config_set(['connectors', $name, 'name'], $name);
780 _config_list_add(['defined-connectors'], $name);
781 } elsif ($subcmd eq 'del') {
785 unless(defined($name)) {
786 _io("No name given");
790 unless (_config_list_has(['defined-connectors'], $name)) {
791 _io("Connector does not exist");
795 if (_config_has(['connectors', $name, '_immutable'])) {
796 _io("Connector cannot be removed");
800 # Remove from list of active connectors
801 _config_list_del(['defined-connectors'], $name);
802 _config_list_del(['active-connectors'], $name);
804 _config_del(['connectors', $name, 'name']);
805 _config_del(['connectors', $name, '_immutable']);
806 _config_del(['connectors', $name, 'schemas', 'http']);
807 _config_del(['connectors', $name, 'schemas', 'https']);
809 @dcon = split(/,/, _config_get(['active-connectors']));
811 if (scalar(@dcon) == 0) {
812 _io("List of selected connectors is empty, resetting to direct");
813 _config_list_add(['active-connectors', 'direct']);
815 } elsif ($subcmd eq 'addschema') {
816 my ($conn, $schema, $proxy) = @_;
818 unless(defined($conn)) {
819 _io("No connector name given");
823 unless(defined($schema)) {
824 _io("No schema given");
828 unless(defined($proxy)) {
829 _io("No proxy given");
834 unless(_config_list_has(['defined-connectors'], $conn)) {
835 _io("Connector does not exist");
839 if (_config_has(['connectors', $conn, '_immutable'])) {
840 _io("Connector cannot be modified");
844 $schema = lc($schema);
845 _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
846 } elsif ($subcmd eq 'delschema') {
847 my ($conn, $schema) = @_;
849 unless(defined($conn)) {
850 _io("No connector name given");
854 unless(defined($schema)) {
855 _io("No schema given");
860 unless(_config_list_has(['defined-connectors'], $conn)) {
861 _io("Connector does not exist");
865 $schema = lc($schema);
866 _config_del(['connectors', $conn, 'schemas', $schema]);
867 } elsif ($subcmd eq 'select') {
868 my @connlist = map { lc } @_;
870 if (scalar(@connlist) == 0) {
871 _io("No connectors given");
875 foreach (@connlist) {
876 unless(_config_list_has(['defined-connectors'], $_)) {
877 _io("Connector %s does not exist", $_);
882 _config_list_set(['active-connectors'], @connlist);
884 _io("connector [list|add|del|addschema|delschema|help] <options>");
885 _io(" help: Show this help");
886 _io(" list: List the defined connectors");
887 _io(" add <name>: Add a connector with name <name>");
888 _io(" del <name>: Delete the connector with name <name>");
889 _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
890 _io(" delschema <name> <schema>: Remove the schema from the connector");
891 _io(" select <name> [<name>...]: Select the connectors to use");
897 # Global debug if the keyword "all" is given, or just for the
898 # current window otherwise
904 if (defined($scope) and (lc($scope) eq 'all')) {
905 _io("Global debug enabled");
908 _io("Debug for this window enabled");
909 $debugwindows{$event->{window}} = 1;
915 # Disable global debug if the keyword "all" is given (this will
916 # also disable all per-window debugs) or just for the current
923 if (defined($scope) and (lc($scope) eq 'all')) {
926 _io("Global debug disabled");
928 delete($debugwindows{$event->{window}});
929 _io("Debug for this window disabled");
934 # Display the content of the config cache
939 _io("Content of config cache:");
940 foreach (sort(keys(%config_cache))) {
941 _io("%s => %s", $_, Dumper($config_cache{$_}));
947 # Return the list of loaded grabbers.
948 # This is used by the test programs, and is not meant to be
956 # ==============================================
957 # Builtin config handling functions
958 # These are used if the library used does not
959 # register it's own config_* handlers
960 # ==============================================
962 sub _builtin_config_init {
964 if (defined($builtin_config_path)) {
965 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
967 _debug("Trying to load configuration from %s", $filename);
972 open(CONF, '<', $filename);
973 %builtin_config = %{JSON->new->utf8->decode(<CONF>)};
976 _io("Error loading configuration: %s", $@);
979 } elsif (defined($builtin_config_default)) {
980 _debug("Initializing builtin config from external default");
981 foreach (keys(%{$builtin_config_default})) {
982 _debug("Setting %s=%s", $_, $builtin_config_default->{$_});
983 $builtin_config{$_} = $builtin_config_default->{$_};
988 sub _builtin_config_get {
989 return $builtin_config{join(".", @{$_[0]})};
992 sub _builtin_config_set {
993 $builtin_config{join(".", @{$_[0]})} = $_[1];
996 sub _builtin_config_has {
997 return exists($builtin_config{join(".", @{$_[0]})});
1000 sub _builtin_config_save {
1002 if (defined($builtin_config_path)) {
1003 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
1005 _debug("Attempting to save config to %s", $filename);
1008 my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path);
1009 print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config);
1011 rename($tempfn, $filename);
1020 sub _builtin_config_del {
1021 delete($builtin_config{join(".", @{$_[0]})});
1025 # ==============================================
1026 # From this point on publicly callable functions
1027 # ==============================================
1032 # Initialization function for the library
1033 # Actually not the first thing to be called, it expects an API
1034 # has (register_api) to be registered first
1037 unless($remote_api) {
1038 $error = "No API set";
1042 # Initialize configuration data
1043 $remote_api->{config_init}->();
1045 # Check/create default values, if they do not exist
1046 _recursive_hash_walk($defaultconfig, \&_init_config_item);
1049 _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
1051 unless (@grabbers && @getters) {
1052 _io('No grabbers or no getters found, can not proceed.');
1057 $getter = $getters[0];
1058 foreach my $p (@getters) {
1059 if (_config_get(['getter']) eq $p->{'NAME'}) {
1063 _debug('Selected %s as getter', $getter->{'NAME'});
1064 _config_set(['getter'], $getter->{'NAME'});
1067 _io('initialized successfully');
1072 # Register a remote API. This API contains a basic output function (used
1073 # when no window specific function is available), some config functions
1074 # and a color code function.
1078 my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
1082 unless(defined($a)) {
1083 die("No API given");
1087 # The config_* handlers are special in that they either all have
1088 # provided by the user, or none. In the latter case builtin
1089 # handlers will be used, but the config will not persist.
1092 foreach (@config_functions) {
1093 if (exists($a->{$_})) {
1100 unless (($c == 0) or ($c == scalar(@config_functions))) {
1101 $error = sprintf("Missing config function: %s", $missing[0]);
1105 foreach (keys(%{$a})) {
1106 if (ref($a->{$_}) ne 'CODE') {
1107 $error = sprintf("API handler %s is not a subroutine reference", $_);
1109 $remote_api->{$_} = $a->{$_};
1112 if (exists($a->{_debug})) {
1113 $debug = $a->{_debug}->();
1116 if (exists($a->{_config_path})) {
1117 $builtin_config_path = $a->{_config_path}->();
1120 if (exists($a->{_config_default})) {
1121 $builtin_config_default = $a->{_config_default}->();
1124 if (exists($a->{_config_cache})) {
1125 $config_cache = $a->{_config_cache}->();
1128 @outputstack = ({io => $remote_api->{'io'}, window => ""});
1134 # Check a message for useable links
1136 sub check_for_link {
1138 my $message = $event->{message};
1143 my $mode = _config_get(['mode']);
1147 # If /nosave is present in the message switch to display mode, regardless
1150 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
1154 _push_output($event);
1155 $message = _expand_url_shortener($message);
1159 # Offer the message to all Grabbers in turn
1160 GRABBER: foreach $g (@grabbers) {
1161 ($m, $p) = $g->get($message);
1162 while (defined($m)) {
1163 _debug('Metadata: %s', Dumper($m));
1165 if (exists($remote_api->{link_callback})) {
1166 $skip = $remote_api->{link_callback}->($m);
1169 if ('download' eq $mode) {
1171 sprintf('%s>>> %sSaving %s%%s%s %s%%s',
1174 _colorpair('*yellow'),
1176 _colorpair('*green'),
1181 unless($getter->get($m)) {
1182 _io(sprintf('%s>>> FAILED', _colorpair('*red')));
1184 } elsif ('display' eq $mode) {
1186 sprintf('%s>>> %sSaw %s%%s%s %s%%s',
1187 _colorpair('*magenta'),
1189 _colorpair('*yellow'),
1191 _colorpair('*green')
1197 _io(sprintf('%s>>> Invalid operation mode', _colorpair('*red')));
1201 # Remove the matched part from the message and try again (there may be
1205 last GRABBER if ($message =~ /^\s*$/);
1207 ($m, $p) = $g->get($message);
1215 # Handle a videosite command (/videosite ...) entered in the client
1217 sub handle_command {
1219 my ($cmd, @params) = split(/\s+/, $event->{message});
1221 _push_output($event);
1223 if (exists($videosite_commands->{$cmd})) {
1224 $videosite_commands->{$cmd}->($event, @params);