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 'name' => 'environment',
62 'config-version' => '2',
66 # This is a list of default values for the remote API. These
67 # are used if the values are not registered by the library user.
70 io => sub { print @_, "\n" },
71 config_init => \&_builtin_config_init,
72 config_get => \&_builtin_config_get,
73 config_set => \&_builtin_config_set,
74 config_has => \&_builtin_config_has,
75 config_save => \&_builtin_config_save,
76 config_del => \&_builtin_config_del,
77 color => sub { return '' },
78 module_path => sub { return dirname(realpath($0)) },
79 quote => sub { return $_ },
84 # List of known commands and handlers
86 my $videosite_commands = {
116 $remote_api->{reload}->();
141 # Output a string on the client.
142 # Works like (s)printf in that it takes a format string and a list of
143 # values to be replaced. Undefined values will be printed as '(undef)'
145 # All parameters (except for the format string itself) will be quoted
146 # using the client specific quote function
152 @text = ('') unless(@text);
154 # This will define the outputprefix once, so we don't have
155 # do do this every time.
156 $outputprefix = sprintf("%svideosite: %s",
157 _colorpair('magenta'),
158 _colorpair()) unless(defined($outputprefix));
159 $format = $outputprefix . shift(@text);
162 # The format string is assumed to be appropriately quoted.
163 # Quote the rest of the text, replacing undefined strings by (undef)
165 @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
167 $outputstack[0]->{io}->(sprintf($format, @text));
171 # Recursively walk through a hash-of-hashes, calling the given function
172 # for each found leaf with the path to the leaf
174 sub _recursive_hash_walk {
176 my $callback = shift;
179 foreach (keys(%{$hash})) {
180 if (ref($hash->{$_}) eq 'HASH') {
181 _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
183 $callback->([@path, $_], $hash->{$_});
189 # Return the color code for the given foreground/background color
190 # pair. Both can be undef, which means "default"
195 $fg = defined($fg)?$fg:'default';
196 $bg = defined($bg)?$bg:'default';
198 return $remote_api->{color}->($fg, $bg);
202 # Sets the given config item if it is not set already
204 sub _init_config_item {
208 unless(_config_has($path)) {
209 _config_set($path, $value);
214 # Print a message if debug is enabled
219 $data[0] = "DEBUG: " . $data[0];
221 # Check for global debug
225 # Check if current window is in the per-window-debug list
226 if (exists($debugwindows{$outputstack[0]->{window}})) {
233 # Load a list of modules matching a pattern from a given directory.
245 opendir(D, $dir) || return ();
246 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
250 _debug("Trying to load $p:");
253 load "videosite::$p";
256 _io("Failed to load plugin: $@");
261 $g = "videosite::$p"->new();
264 _io("Failed to instanciate: $@");
269 _debug("found $g->{'TYPE'} $g->{'NAME'}");
270 if ($type eq $g->{'TYPE'}) {
274 io_debug => \&_debug,
275 connectors => sub { return _connectorlist('active-connectors') },
276 config_get => \&_config_get,
277 config_set => \&_config_set,
278 config_has => \&_config_has,
281 _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
286 _debug("Loaded %d plugins", $#g+1);
292 # Populate the @grabbers and @getters lists from the given
295 sub _load_modules($) {
299 foreach (keys(%INC)) {
300 if ($INC{$_} =~ m|^$path|) {
301 _debug("Removing %s from \$INC", $_);
305 @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
306 @getters = _ploader($path, '.*Getter\.pm$', 'getter');
310 # Wrapper functions for config management to put in
315 my $dotpath = join('.', @{$path});
318 if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{value})) {
319 $value = $config_cache{$dotpath}->{value};
321 $value = $remote_api->{config_get}->($path);
322 $config_cache{$dotpath} = {value => $value, has => 1};
326 _debug("config: getting %s=%s", $dotpath, $value);
332 my $dotpath = join('.', @{$path});
335 _debug("config: setting %s=%s", $dotpath, $value);
336 $config_cache{$dotpath} = {value => $value, has => 1};
337 return $remote_api->{config_set}->($path, $value);
342 my $dotpath = join('.', @{$path});
345 if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{has})) {
346 $b = $config_cache{$dotpath}->{has};
348 $b = $remote_api->{config_has}->($path);
349 $config_cache{$dotpath}->{has} = $b;
352 _debug("config: testing %s (%s)", $dotpath, $b?'true':'false');
358 my $dotpath = join('.', @{$path});
360 _debug("config: removing %s", $dotpath);
361 delete($config_cache{$dotpath});
362 $remote_api->{config_del}->($path);
366 # The _config_list_* are helper functions taking a path to a comma separated
367 # string. The string is interpreted as a list and the action performed
368 # on it, storing back the modified version
372 # Add an item to the list, checking for duplicates
374 sub _config_list_add {
379 if (_config_has($path)) {
380 @c = split(/\s*,\s*/, _config_get($path));
385 _debug("Adding %s to list %s", $item, join(".", @{$path}));
386 unless(grep { $_ eq $item } @c) {
390 _config_set($path, join(',', @c));
394 # Remove an item from the list
396 sub _config_list_del {
401 unless(_config_has($path)) {
405 _debug("Removing %s from list %s", $item, join('.', @{$path}));
406 @c = grep { $item ne $_ } split(/\s*,\s*/, _config_get($path));
408 _config_set($path, join(',', @c));
412 # Return true if the item contains the given list, false otherwise
414 sub _config_list_has {
418 unless(_config_has($path)) {
422 _debug("Checking for %s in list %s", $item, join('.', @{$path}));
424 return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
428 # Replace a list with the given items
430 sub _config_list_set {
433 _debug("Replacing %s with (%s)", join('.', @{$path}), join(",", @_));
435 _config_set($path, join(',', @_));
439 # Return the list of currently active connectors, in the configured
446 foreach(split(/,/, _config_get([$key]))) {
447 push(@c, _unserialize_connector_hash($_));
454 # Convert a connector hash from it's config structure back to a perl
457 sub _unserialize_connector_hash {
461 if (_config_has(['connectors', $name, 'name'])) {
462 $connector->{name} = _config_get(['connectors', $name, 'name']);
463 $connector->{schemas} = {};
464 foreach ('http', 'https') {
465 if (_config_has(['connectors', $name, 'schemas', $_])) {
466 $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
471 _debug("Returning connector %s: %s", $name, Dumper($connector));
477 # Push a new output function on the IO stack.
480 unshift(@outputstack, shift);
484 # Pop the topmost output function from the stack, leaving
485 # at least one function on it.
488 if (scalar(@outputstack) > 0) {
494 # Takes a string and replaces commonly used URL shorteners recursively,
495 # up to 10 levels deep
497 sub _expand_url_shortener {
501 'is\.gd/[[:alnum:]]+',
502 'otf\.me/[[:alnum:]]+',
503 'hel\.me/[[:alnum:]]+',
504 '7ax\.de/[[:alnum:]]+',
505 'ow\.ly/[[:alnum:]]+',
506 'j\.mp/[[:alnum:]]+',
507 'bit\.ly/[[:alnum:]]+',
508 'tinyurl\.com/[[:alnum:]]+',
509 'pop\.is/[[:alnum:]]+',
510 'post\.ly/[[:alnum:]]+',
511 '1\.ly/[[:alnum:]]+',
512 '2\.ly/[[:alnum:]]+',
513 't\.co/[[:alnum:]]+',
514 'shar\.es/[[:alnum:]]+',
515 'goo\.gl/[[:alnum:]]+',
517 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
520 OUTER: while (($os ne $s) and ($i > 0)) {
525 foreach my $pattern (@urlshortener) {
526 my $p = "https?:\/\/" . $pattern;
528 _debug("Matching %s against %s", $p, $s);
533 _debug("Found %s", $matched);
534 $res = $ua->head($matched);
535 if ($res->is_redirect()) {
536 my $new = $res->headers()->header("Location");
538 _debug("Replacing %s with %s", $matched, $new);
539 $s =~ s/$matched/$new/;
542 _debug("Error resolving %s", $matched);
549 _debug("Loop terminated by counter");
552 _debug("Final string: %s", $s);
558 # Save the config to durable storage
563 if ($remote_api->{config_save}->()) {
566 _io(sprintf("%sConfig save failed%s", _colorpair("*red"), _colorpair()));
571 # Set a configuration element
580 foreach $p (@getters, @grabbers) {
581 if ($p->{'NAME'} eq $target) {
582 $p->setval($key, $val);
586 _io('No such module');
591 # Enable a given module
598 foreach $p (@grabbers) {
599 if ($p->{'NAME'} eq $target) {
604 _io('No such module');
608 # Disable given module
615 foreach $p (@grabbers) {
616 if ($p->{'NAME'} eq $target) {
621 _io('No such module');
625 # Show settings for modules
633 if (defined($target)) {
634 foreach $p (@getters, @grabbers) {
635 if ($p->{'NAME'} eq $target) {
636 _io($p->getconfstr());
640 _io('No such module');
642 _io('Loaded grabbers (* denotes enabled modules):');
643 foreach $p (@grabbers) {
644 $e = $p->_getval('enabled');
645 _io(' %s%s', $p->{'NAME'}, $e?'*':'');
648 _io('Loaded getters:');
649 foreach $p (@getters) {
650 _io(' %s', $p->{'NAME'});
656 # Show help for the commands
663 if (defined($target)) {
664 foreach $p (@getters, @grabbers) {
665 if ($p->{'NAME'} eq $target) {
666 _io($p->gethelpstr());
670 _io('No such module');
674 save: save the current configuration
675 help [modulename]: display this help, or module specific help
676 show [modulename]: show loaded modules, or the current parameters of a module
677 set modulename parameter value: set a module parameter to a new value
678 getter [modulename]: display or set the getter to use
679 enable [modulename]: enable the usage of this module (grabbers only)
680 disable [modulename]: disable the usage of this module (grabbers only)
681 reload: reload all modules (this is somewhat experimental)
682 mode [modename]: display or set the operation mode (download/display)
683 connector [subcommand]: manage connectors (proxies)
684 debug: enable debugging messages
685 nodebug: disable debugging messages
691 # Set the getter to use
698 if (defined($target)) {
699 foreach $p (@getters) {
700 if ($p->{'NAME'} eq $target) {
702 _config_set(['getter'], $target);
703 _io("Getter changed to %s", $target);
707 _io('No such getter');
709 _io('Current getter: %s', _config_get(['getter']));
714 # Show/set the working mode
720 if (defined($mode)) {
722 if (('download' eq $mode) or ('display' eq $mode)) {
723 _config_set(['mode'], $mode);
724 _io('Now using %s mode', $mode);
726 _io('Invalid mode: %s', $mode);
729 _io('Current mode: %s', _config_get(['mode']));
735 # Manage the connectors
742 unless(defined($subcmd)) {
746 $subcmd = lc($subcmd);
748 if ($subcmd eq 'list') {
749 _io("Defined connectors");
750 foreach $c (_connectorlist('defined-connectors')) {
752 my $schemas = $c->{schemas};
753 if (scalar(keys(%{$schemas})) == 0) {
754 _io(" No schemas defined");
756 foreach (keys(%{$schemas})) {
757 _io(' %s: %s', $_, $schemas->{$_});
763 _io("Selected connectors: %s", _config_get(['active-connectors']));
764 } elsif ($subcmd eq 'add') {
767 unless(defined($name)) {
768 _io("No name given");
774 unless($name =~ m|^[a-z]+$|) {
775 _io("%s is not a valid connector name (only letters are allowed)", $name);
779 if (_config_list_has(['defined-connectors'], $name)) {
780 _io("Connector already exists");
784 _config_set(['connectors', $name, 'name'], $name);
785 _config_list_add(['defined-connectors'], $name);
786 } elsif ($subcmd eq 'del') {
790 unless(defined($name)) {
791 _io("No name given");
795 unless (_config_list_has(['defined-connectors'], $name)) {
796 _io("Connector does not exist");
800 if (_config_has(['connectors', $name, '_immutable'])) {
801 _io("Connector cannot be removed");
805 # Remove from list of active connectors
806 _config_list_del(['defined-connectors'], $name);
807 _config_list_del(['active-connectors'], $name);
809 _config_del(['connectors', $name, 'name']);
810 _config_del(['connectors', $name, '_immutable']);
811 _config_del(['connectors', $name, 'schemas', 'http']);
812 _config_del(['connectors', $name, 'schemas', 'https']);
814 @dcon = split(/,/, _config_get(['active-connectors']));
816 if (scalar(@dcon) == 0) {
817 _io("List of selected connectors is empty, resetting to direct");
818 _config_list_add(['active-connectors', 'direct']);
820 } elsif ($subcmd eq 'addschema') {
821 my ($conn, $schema, $proxy) = @_;
823 unless(defined($conn)) {
824 _io("No connector name given");
828 unless(defined($schema)) {
829 _io("No schema given");
833 unless(defined($proxy)) {
834 _io("No proxy given");
839 unless(_config_list_has(['defined-connectors'], $conn)) {
840 _io("Connector does not exist");
844 if (_config_has(['connectors', $conn, '_immutable'])) {
845 _io("Connector cannot be modified");
849 $schema = lc($schema);
850 _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
851 } elsif ($subcmd eq 'delschema') {
852 my ($conn, $schema) = @_;
854 unless(defined($conn)) {
855 _io("No connector name given");
859 unless(defined($schema)) {
860 _io("No schema given");
865 unless(_config_list_has(['defined-connectors'], $conn)) {
866 _io("Connector does not exist");
870 $schema = lc($schema);
871 _config_del(['connectors', $conn, 'schemas', $schema]);
872 } elsif ($subcmd eq 'select') {
873 my @connlist = map { lc } @_;
875 if (scalar(@connlist) == 0) {
876 _io("No connectors given");
880 foreach (@connlist) {
881 unless(_config_list_has(['defined-connectors'], $_)) {
882 _io("Connector %s does not exist", $_);
887 _config_list_set(['active-connectors'], @connlist);
889 _io("connector [list|add|del|addschema|delschema|help] <options>");
890 _io(" help: Show this help");
891 _io(" list: List the defined connectors");
892 _io(" add <name>: Add a connector with name <name>");
893 _io(" del <name>: Delete the connector with name <name>");
894 _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
895 _io(" delschema <name> <schema>: Remove the schema from the connector");
896 _io(" select <name> [<name>...]: Select the connectors to use");
902 # Global debug if the keyword "all" is given, or just for the
903 # current window otherwise
909 if (defined($scope) and (lc($scope) eq 'all')) {
910 _io("Global debug enabled");
913 _io("Debug for this window enabled");
914 $debugwindows{$event->{window}} = 1;
920 # Disable global debug if the keyword "all" is given (this will
921 # also disable all per-window debugs) or just for the current
928 if (defined($scope) and (lc($scope) eq 'all')) {
931 _io("Global debug disabled");
933 delete($debugwindows{$event->{window}});
934 _io("Debug for this window disabled");
939 # Handle generic service commands
943 my $subcmd = shift || '';
945 $subcmd = lc($subcmd);
947 if ($subcmd eq 'cache') {
948 _cmd_service_cache($event, @_);
954 # Display or clear the content of the config cache
956 sub _cmd_service_cache {
960 $subcmd = 'list' unless defined($subcmd);
961 $subcmd = lc($subcmd);
963 if ($subcmd eq 'list') {
964 _io("Content of config cache:");
965 foreach (sort(keys(%config_cache))) {
966 if (exists($config_cache{$_}->{value})) {
967 _io(" %s => %s", $_, $config_cache{$_}->{value});
969 _io(" %s present", $_);
972 } elsif ($subcmd eq 'clear') {
974 _io("Cache cleared");
980 # Return the list of loaded grabbers.
981 # This is used by the test programs, and is not meant to be
989 # ==============================================
990 # Builtin config handling functions
991 # These are used if the library used does not
992 # register it's own config_* handlers
993 # ==============================================
995 sub _builtin_config_init {
997 if (defined($builtin_config_path)) {
998 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
1000 _debug("Trying to load configuration from %s", $filename);
1005 open(CONF, '<', $filename);
1006 %builtin_config = %{JSON->new->utf8->decode(<CONF>)};
1009 _io("Error loading configuration: %s", $@);
1012 } elsif (defined($builtin_config_default)) {
1013 _debug("Initializing builtin config from external default");
1014 foreach (keys(%{$builtin_config_default})) {
1015 _debug("Setting %s=%s", $_, $builtin_config_default->{$_});
1016 $builtin_config{$_} = $builtin_config_default->{$_};
1021 sub _builtin_config_get {
1022 return $builtin_config{join(".", @{$_[0]})};
1025 sub _builtin_config_set {
1026 $builtin_config{join(".", @{$_[0]})} = $_[1];
1029 sub _builtin_config_has {
1030 return exists($builtin_config{join(".", @{$_[0]})});
1033 sub _builtin_config_save {
1035 if (defined($builtin_config_path)) {
1036 my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
1038 _debug("Attempting to save config to %s", $filename);
1041 my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path);
1042 print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config);
1044 rename($tempfn, $filename);
1053 sub _builtin_config_del {
1054 delete($builtin_config{join(".", @{$_[0]})});
1058 # ==============================================
1059 # From this point on publicly callable functions
1060 # ==============================================
1065 # Initialization function for the library
1066 # Actually not the first thing to be called, it expects an API
1067 # has (register_api) to be registered first
1070 unless($remote_api) {
1071 $error = "No API set";
1075 # Initialize configuration data
1076 $remote_api->{config_init}->();
1078 # Check/create default values, if they do not exist
1079 _recursive_hash_walk($defaultconfig, \&_init_config_item);
1082 _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
1084 unless (@grabbers && @getters) {
1085 _io('No grabbers or no getters found, can not proceed.');
1090 $getter = $getters[0];
1091 foreach my $p (@getters) {
1092 if (_config_get(['getter']) eq $p->{'NAME'}) {
1096 _debug('Selected %s as getter', $getter->{'NAME'});
1097 _config_set(['getter'], $getter->{'NAME'});
1100 _io('initialized successfully');
1105 # Register a remote API. This API contains a basic output function (used
1106 # when no window specific function is available), some config functions
1107 # and a color code function.
1111 my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
1115 unless(defined($a)) {
1116 die("No API given");
1120 # The config_* handlers are special in that they either all have
1121 # provided by the user, or none. In the latter case builtin
1122 # handlers will be used, but the config will not persist.
1125 foreach (@config_functions) {
1126 if (exists($a->{$_})) {
1133 unless (($c == 0) or ($c == scalar(@config_functions))) {
1134 $error = sprintf("Missing config function: %s", $missing[0]);
1138 foreach (keys(%{$a})) {
1139 if (ref($a->{$_}) ne 'CODE') {
1140 $error = sprintf("API handler %s is not a subroutine reference", $_);
1142 $remote_api->{$_} = $a->{$_};
1145 if (exists($a->{_debug})) {
1146 $debug = $a->{_debug}->();
1149 if (exists($a->{_config_path})) {
1150 $builtin_config_path = $a->{_config_path}->();
1153 if (exists($a->{_config_default})) {
1154 $builtin_config_default = $a->{_config_default}->();
1157 if (exists($a->{_config_cache})) {
1158 $config_cache = $a->{_config_cache}->();
1161 @outputstack = ({io => $remote_api->{'io'}, window => ""});
1167 # Check a message for useable links
1169 sub check_for_link {
1171 my $message = $event->{message};
1176 my $mode = _config_get(['mode']);
1180 # If /nosave is present in the message switch to display mode, regardless
1183 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
1187 _push_output($event);
1188 $message = _expand_url_shortener($message);
1192 # Offer the message to all Grabbers in turn
1193 GRABBER: foreach $g (@grabbers) {
1194 ($m, $p) = $g->get($message);
1195 while (defined($m)) {
1196 _debug('Metadata: %s', Dumper($m));
1198 if (exists($remote_api->{link_callback})) {
1199 $skip = $remote_api->{link_callback}->($m);
1202 if ('download' eq $mode) {
1204 sprintf('%s>>> %sSaving %s%%s%s %s%%s',
1207 _colorpair('*yellow'),
1209 _colorpair('*green'),
1214 unless($getter->get($m)) {
1215 _io(sprintf('%s>>> FAILED', _colorpair('*red')));
1217 } elsif ('display' eq $mode) {
1219 sprintf('%s>>> %sSaw %s%%s%s %s%%s',
1220 _colorpair('*magenta'),
1222 _colorpair('*yellow'),
1224 _colorpair('*green')
1230 _io(sprintf('%s>>> Invalid operation mode', _colorpair('*red')));
1234 # Remove the matched part from the message and try again (there may be
1238 last GRABBER if ($message =~ /^\s*$/);
1240 ($m, $p) = $g->get($message);
1248 # Handle a videosite command (/videosite ...) entered in the client
1250 sub handle_command {
1252 my ($cmd, @params) = split(/\s+/, $event->{message});
1254 _push_output($event);
1256 if (exists($videosite_commands->{$cmd})) {
1257 $videosite_commands->{$cmd}->($event, @params);