--- /dev/null
+Installation instructions
+=========================
+
+Copy/symlink the following files/directories into ~/.irssi/scripts (create
+the directory if it does not exist)
+
+* videosite-irssi.pl
+* libvideosite.pm
+* videosite/
+
+In irssi, use "/load videosite-irssi.pl" to activate.
+
+
+Upgrade
+=======
+
+If you've been using a videosite version without libvideosite.pm it's a
+good idea to restart irssi for the upgrade. Follow the installation
+instructions above. Existing configuration using videosite.xml or
+videosite.json files will be converted to use the irssi
+internal configuration storage on the first start.
--- /dev/null
+Installation instructions
+=========================
+
+Copy/symlink the following files/directories into ~/.weechat/per (create
+the directory if it does not exist)
+
+* videosite-weechat.pl
+* libvideosite.pm
+* videosite/
+
+In weechat, use "/script load videosite-weechat.pl" to activate.
--- /dev/null
+Installation instructions
+=========================
+
+Copy/symlink the following files/directories into ~/.xchat2/perl (create
+the directory if it does not exist)
+
+* videosite-xchat2.pl
+* libvideosite.pm
+* videosite/
+
+In xchat, use XChat->Load Plugin or Script to load videosite-xchat2.pl from
+~/.xchat/perl.
+
+The configuration will be saved as a file in ~/.xchat2/videosite.json.
--- /dev/null
+# library to autodownload flash videos
+#
+# (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
+# which in turn is
+# based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
+
+package libvideosite;
+require Exporter;
+
+use vars qw(@ISA @EXPORT_OK);
+use File::Spec;
+use Module::Load;
+use LWP::UserAgent;
+use Data::Dumper;
+use File::Basename;
+use Cwd qw(realpath);
+use JSON -support_by_pp;
+use File::Temp qw(tempfile);
+use strict;
+
+@ISA = qw(Exporter);
+@EXPORT_OK = qw(init register_api check_for_link);
+
+my @outputstack;
+my $outputprefix;
+my $debug = 0;
+my %debugwindows = ();
+my @grabbers;
+my @getters;
+my $getter;
+my %builtin_config = ();
+my $builtin_config_path;
+my $builtin_config_default;
+our $error;
+
+#
+# The default config. These values will be set in the config
+# if they do not exist already.
+#
+my $defaultconfig = {
+ 'getter' => 'filegetter',
+ 'mode' => 'download',
+ 'active-connectors' => 'direct',
+ 'defined-connectors' => 'direct',
+ 'connectors' => {
+ 'direct' => {
+ 'name' => 'direct',
+ '_immutable' => '1',
+ 'schemas' => {},
+ }
+ },
+ 'config-version' => '2',
+};
+
+#
+# This is a list of default values for the remote API. These
+# are used if the values are not registered by the library user.
+#
+my $remote_api = {
+ io => sub { print @_, "\n" },
+ config_init => \&_builtin_config_init,
+ config_get => \&_builtin_config_get,
+ config_set => \&_builtin_config_set,
+ config_has => \&_builtin_config_has,
+ config_save => \&_builtin_config_save,
+ config_del => \&_builtin_config_del,
+ color => sub { return '' },
+ module_path => sub { return dirname(realpath($0)) },
+ quote => sub { return $_ },
+ reload => sub {},
+};
+
+#
+# List of known commands and handlers
+#
+my $videosite_commands = {
+ 'save' => sub {
+ _cmd_save();
+ },
+
+ 'set' => sub {
+ _cmd_set(@_);
+ },
+
+ 'show' => sub {
+ _cmd_show(@_);
+ },
+
+ 'help' => sub {
+ _cmd_help(@_);
+ },
+
+ 'getter' => sub {
+ _cmd_getter(@_);
+ },
+
+ 'enable' => sub {
+ _cmd_enable(@_);
+ },
+
+ 'disable' => sub {
+ _cmd_disable(@_);
+ },
+
+ 'reload' => sub {
+ $remote_api->{reload}->();
+ },
+
+ 'mode' => sub {
+ _cmd_mode(@_);
+ },
+
+ 'connector' => sub {
+ _cmd_connector(@_);
+ },
+
+ 'debug' => sub {
+ _cmd_debug(@_);
+ },
+
+ 'nodebug' => sub {
+ _cmd_nodebug(@_);
+ },
+};
+
+#
+# Output a string on the client.
+# Works like (s)printf in that it takes a format string and a list of
+# values to be replaced. Undefined values will be printed as '(undef)'
+#
+# All parameters (except for the format string itself) will be quoted
+# using the client specific quote function
+#
+sub _io {
+ my @text = @_;
+ my $format;
+
+ @text = ('') unless(@text);
+
+ # This will define the outputprefix once, so we don't have
+ # do do this every time.
+ $outputprefix = sprintf("%svideosite: %s",
+ _colorpair('magenta'),
+ _colorpair()) unless(defined($outputprefix));
+ $format = $outputprefix . shift(@text);
+
+ #
+ # The format string is assumed to be appropriately quoted.
+ # Quote the rest of the text, replacing undefined strings by (undef)
+ #
+ @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
+
+ $outputstack[0]->{ewpf}->(sprintf($format, @text));
+}
+
+#
+# Recursively walk through a hash-of-hashes, calling the given function
+# for each found leaf with the path to the leaf
+#
+sub _recursive_hash_walk {
+ my $hash = shift;
+ my $callback = shift;
+ my @path = @_;
+
+ foreach (keys(%{$hash})) {
+ if (ref($hash->{$_}) eq 'HASH') {
+ _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
+ } else {
+ $callback->([@path, $_], $hash->{$_});
+ }
+ }
+}
+
+#
+# Return the color code for the given foreground/background color
+# pair. Both can be undef, which means "default"
+#
+sub _colorpair {
+ my ($fg, $bg) = @_;
+
+ $fg = defined($fg)?$fg:'default';
+ $bg = defined($bg)?$bg:'default';
+
+ return $remote_api->{color}->($fg, $bg);
+}
+
+#
+# Sets the given config item if it is not set already
+#
+sub _init_config_item {
+ my $path = shift;
+ my $value = shift;
+
+ unless(_config_has($path)) {
+ _config_set($path, $value);
+ }
+}
+
+#
+# Print a message if debug is enabled
+#
+sub _debug {
+ my @data = @_;
+
+ $data[0] = "DEBUG: " . $data[0];
+
+ # Check for global debug
+ if ($debug) {
+ _io(@data);
+ } else {
+ # Check if current window is in the per-window-debug list
+ if (exists($debugwindows{$outputstack[0]->{window}})) {
+ _io(@data);
+ }
+ }
+}
+
+#
+# Load a list of modules matching a pattern from a given directory.
+#
+sub _ploader {
+
+ my $dir = shift;
+ my $pattern = shift;
+ my $type = shift;
+ my @list;
+ my $p;
+ my $g;
+ my @g = ();
+
+ opendir(D, $dir) || return ();
+ @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
+ closedir(D);
+
+ foreach $p (@list) {
+ _debug("Trying to load $p:");
+ $p =~ s/\.pm$//;
+ eval {
+ load "videosite::$p";
+ };
+ if ($@) {
+ _io("Failed to load plugin: $@");
+ next;
+ }
+
+ eval {
+ $g = "videosite::$p"->new();
+ };
+ if ($@) {
+ _io("Failed to instanciate: $@");
+ delete($INC{$p});
+ next;
+ }
+
+ _debug("found $g->{'TYPE'} $g->{'NAME'}");
+ if ($type eq $g->{'TYPE'}) {
+ push(@g, $g);
+ $g->register_api({
+ io => \&_io,
+ io_debug => \&_debug,
+ connectors => sub { return _connectorlist('active-connectors') },
+ config_get => \&_config_get,
+ config_set => \&_config_set,
+ config_has => \&_config_has,
+ });
+ } else {
+ _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
+ delete($INC{$p});
+ }
+ }
+
+ _debug("Loaded %d plugins", $#g+1);
+
+ return @g;
+}
+
+#
+# Populate the @grabbers and @getters lists from the given
+# directory
+#
+sub _load_modules($) {
+
+ my $path = shift;
+
+ foreach (keys(%INC)) {
+ if ($INC{$_} =~ m|^$path|) {
+ _debug("Removing %s from \$INC", $_);
+ delete($INC{$_});
+ }
+ }
+ @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
+ @getters = _ploader($path, '.*Getter\.pm$', 'getter');
+}
+
+#
+# Wrapper functions for config management to put in
+# debugging
+#
+sub _config_get {
+ my $path = shift;
+ my $value;
+
+ $value = $remote_api->{config_get}->($path);
+ _debug("config: getting %s=%s", join('.', @{$path}), $value);
+
+ return $value;
+}
+
+sub _config_set {
+ my $path = shift;
+ my $value = shift;
+
+ _debug("config: setting %s=%s", join('.', @{$path}), $value);
+ return $remote_api->{config_set}->($path, $value);
+}
+
+sub _config_has {
+ my $path = shift;
+ my $b;
+
+ $b = $remote_api->{config_has}->($path);
+ _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false');
+
+ return $b;
+}
+
+sub _config_del {
+ my $path = shift;
+
+ _debug("config: removing %s", join('.', @{$path}));
+ $remote_api->{config_del}->($path);
+}
+
+#
+# The _config_list_* are helper functions taking a path to a comma separated
+# string. The string is interpreted as a list and the action performed
+# on it, storing back the modified version
+#
+
+#
+# Add an item to the list, checking for duplicates
+#
+sub _config_list_add {
+ my $path = shift;
+ my $item = shift;
+ my @c;
+
+ if (_config_has($path)) {
+ @c = split(/\s*,\s*/, _config_get($path));
+ } else {
+ @c = ();
+ }
+
+ _debug("Adding %s to list %s", $item, join(".", @{$path}));
+ unless(grep { $_ eq $item } @c) {
+ push(@c, $item);
+ };
+
+ _config_set($path, join(',', @c));
+}
+
+#
+# Remove an item from the list
+#
+sub _config_list_del {
+ my $path = shift;
+ my $item = shift;
+ my @c;
+
+ unless(_config_has($path)) {
+ return;
+ }
+
+ _debug("Removing %s from list %s", $item, join('.', @{$path}));
+ @c = grep { $item ne $_ } split(/\s*,\s*/, _config_get($path));
+
+ _config_set($path, join(',', @c));
+}
+
+#
+# Return true if the item contains the given list, false otherwise
+#
+sub _config_list_has {
+ my $path = shift;
+ my $item = shift;
+
+ unless(_config_has($path)) {
+ return 0;
+ }
+
+ _debug("Checking for %s in list %s", $item, join('.', @{$path}));
+
+ return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
+}
+
+#
+# Replace a list with the given items
+#
+sub _config_list_set {
+ my $path = shift;
+
+ _debug("Replacing %s with (%s)", join('.', @{$path}), join(",", @_));
+
+ _config_set($path, join(',', @_));
+}
+
+#
+# Return the list of currently active connectors, in the configured
+# order
+#
+sub _connectorlist {
+ my $key = shift;
+ my @c;
+
+ foreach(split(/,/, _config_get([$key]))) {
+ push(@c, _unserialize_connector_hash($_));
+ }
+
+ return @c;
+}
+
+#
+# Convert a connector hash from it's config structure back to a perl
+# hash
+#
+sub _unserialize_connector_hash {
+ my $name = shift;
+ my $connector = {};
+
+ if (_config_has(['connectors', $name, 'name'])) {
+ $connector->{name} = _config_get(['connectors', $name, 'name']);
+ $connector->{schemas} = {};
+ foreach ('http', 'https') {
+ if (_config_has(['connectors', $name, 'schemas', $_])) {
+ $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
+ }
+ }
+ }
+
+ _debug("Returning connector %s: %s", $name, Dumper($connector));
+
+ return $connector;
+}
+
+#
+# Push a new output function on the IO stack.
+#
+sub _push_output {
+ unshift(@outputstack, shift);
+}
+
+#
+# Pop the topmost output function from the stack, leaving
+# at least one function on it.
+#
+sub _pop_output {
+ if (scalar(@outputstack) > 0) {
+ shift(@outputstack);
+ }
+}
+
+#
+# Takes a string and replaces commonly used URL shorteners recursively,
+# up to 10 levels deep
+#
+sub _expand_url_shortener {
+ my $s = shift;
+ my $os = '';
+ my @urlshortener = (
+ 'is\.gd/[[:alnum:]]+',
+ 'otf\.me/[[:alnum:]]+',
+ 'hel\.me/[[:alnum:]]+',
+ '7ax\.de/[[:alnum:]]+',
+ 'ow\.ly/[[:alnum:]]+',
+ 'j\.mp/[[:alnum:]]+',
+ 'bit\.ly/[[:alnum:]]+',
+ 'tinyurl\.com/[[:alnum:]]+',
+ 'pop\.is/[[:alnum:]]+',
+ 'post\.ly/[[:alnum:]]+',
+ '1\.ly/[[:alnum:]]+',
+ '2\.ly/[[:alnum:]]+',
+ 't\.co/[[:alnum:]]+',
+ 'shar\.es/[[:alnum:]]+',
+ 'goo\.gl/[[:alnum:]]+',
+ );
+ my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
+ my $i = 10;
+
+ OUTER: while (($os ne $s) and ($i > 0)) {
+ study($s);
+ $os = $s;
+ $i--;
+
+ foreach my $pattern (@urlshortener) {
+ my $p = "https?:\/\/" . $pattern;
+
+ _debug("Matching %s against %s", $p, $s);
+ if ($s =~ m|($p)|) {
+ my $matched = $1;
+ my $res;
+
+ _debug("Found %s", $matched);
+ $res = $ua->head($matched);
+ if ($res->is_redirect()) {
+ my $new = $res->headers()->header("Location");
+
+ _debug("Replacing %s with %s", $matched, $new);
+ $s =~ s/$matched/$new/;
+ next OUTER;
+ } else {
+ _debug("Error resolving %s", $matched);
+ }
+ }
+ }
+ }
+
+ if ($i == 0) {
+ _debug("Loop terminated by counter");
+ }
+
+ _debug("Final string: %s", $s);
+
+ return $s;
+}
+
+#
+# Save the config to durable storage
+#
+sub _cmd_save {
+ my $event = shift;
+
+ if ($remote_api->{config_save}->()) {
+ _io("Config saved");
+ } else {
+ _io(sprintf("%sConfig save failed%s", _colorpair("*red"), _colorpair()));
+ }
+}
+
+#
+# Set a configuration element
+#
+sub _cmd_set {
+ my $event = shift;
+ my $target = shift;
+ my $key = shift;
+ my $val = shift;
+ my $p;
+
+ foreach $p (@getters, @grabbers) {
+ if ($p->{'NAME'} eq $target) {
+ $p->setval($key, $val);
+ return;
+ }
+ }
+ _io('No such module');
+}
+
+
+#
+# Enable a given module
+#
+sub _cmd_enable {
+ my $event = shift;
+ my $target = shift;
+ my $p;
+
+ foreach $p (@grabbers) {
+ if ($p->{'NAME'} eq $target) {
+ $p->enable();
+ return;
+ }
+ }
+ _io('No such module');
+}
+
+#
+# Disable given module
+#
+sub _cmd_disable {
+ my $event = shift;
+ my $target = shift;
+ my $p;
+
+ foreach $p (@grabbers) {
+ if ($p->{'NAME'} eq $target) {
+ $p->disable();
+ return;
+ }
+ }
+ _io('No such module');
+}
+
+#
+# Show settings for modules
+#
+sub _cmd_show {
+ my $event = shift;
+ my $target = shift;
+ my $p;
+ my $e;
+
+ if (defined($target)) {
+ foreach $p (@getters, @grabbers) {
+ if ($p->{'NAME'} eq $target) {
+ _io($p->getconfstr());
+ return;
+ }
+ }
+ _io('No such module');
+ } else {
+ _io('Loaded grabbers (* denotes enabled modules):');
+ foreach $p (@grabbers) {
+ $e = $p->_getval('enabled');
+ _io(' %s%s', $p->{'NAME'}, $e?'*':'');
+ };
+
+ _io('Loaded getters:');
+ foreach $p (@getters) {
+ _io(' %s', $p->{'NAME'});
+ };
+ }
+}
+
+#
+# Show help for the commands
+#
+sub _cmd_help {
+ my $event = shift;
+ my $target = shift;
+ my $p;
+
+ if (defined($target)) {
+ foreach $p (@getters, @grabbers) {
+ if ($p->{'NAME'} eq $target) {
+ _io($p->gethelpstr());
+ return;
+ }
+ }
+ _io('No such module');
+ } else {
+ _io(<<'EOT');
+Supported commands:
+ save: save the current configuration
+ help [modulename]: display this help, or module specific help
+ show [modulename]: show loaded modules, or the current parameters of a module
+ set modulename parameter value: set a module parameter to a new value
+ getter [modulename]: display or set the getter to use
+ enable [modulename]: enable the usage of this module (grabbers only)
+ disable [modulename]: disable the usage of this module (grabbers only)
+ reload: reload all modules (this is somewhat experimental)
+ mode [modename]: display or set the operation mode (download/display)
+ connector [subcommand]: manage connectors (proxies)
+ debug: enable debugging messages
+ nodebug: disable debugging messages
+EOT
+ }
+}
+
+#
+# Set the getter to use
+#
+sub _cmd_getter {
+ my $event = shift;
+ my $target = shift;
+ my $p;
+
+ if (defined($target)) {
+ foreach $p (@getters) {
+ if ($p->{'NAME'} eq $target) {
+ $getter = $p;
+ _config_set(['getter'], $target);
+ _io("Getter changed to %s", $target);
+ return;
+ }
+ }
+ _io('No such getter');
+ } else {
+ _io('Current getter: %s', _config_get(['getter']));
+ }
+}
+
+#
+# Show/set the working mode
+#
+sub _cmd_mode {
+ my $event = shift;
+ my $mode = shift;
+
+ if (defined($mode)) {
+ $mode = lc($mode);
+ if (('download' eq $mode) or ('display' eq $mode)) {
+ _config_set(['mode'], $mode);
+ _io('Now using %s mode', $mode);
+ } else {
+ _io('Invalid mode: %s', $mode);
+ }
+ } else {
+ _io('Current mode: %s', _config_get(['mode']));
+ }
+}
+
+
+#
+# Manage the connectors
+#
+sub _cmd_connector {
+ my $event = shift;
+ my $subcmd = shift;
+ my $c;
+
+ unless(defined($subcmd)) {
+ $subcmd = "help";
+ }
+
+ $subcmd = lc($subcmd);
+
+ if ($subcmd eq 'list') {
+ _io("Defined connectors");
+ foreach $c (_connectorlist('defined-connectors')) {
+ _io($c->{name});
+ my $schemas = $c->{schemas};
+ if (scalar(keys(%{$schemas})) == 0) {
+ _io(" No schemas defined");
+ } else {
+ foreach (keys(%{$schemas})) {
+ _io(' %s: %s', $_, $schemas->{$_});
+ }
+ }
+ }
+
+ _io();
+ _io("Selected connectors: %s", _config_get(['active-connectors']));
+ } elsif ($subcmd eq 'add') {
+ my ($name) = @_;
+
+ unless(defined($name)) {
+ _io("No name given");
+ return;
+ }
+
+ $name = lc($name);
+
+ unless($name =~ m|^[a-z]+$|) {
+ _io("%s is not a valid connector name (only letters are allowed)", $name);
+ return;
+ }
+
+ if (_config_list_has(['defined-connectors'], $name)) {
+ _io("Connector already exists");
+ return;
+ }
+
+ _config_set(['connectors', $name, 'name'], $name);
+ _config_list_add(['defined-connectors'], $name);
+ } elsif ($subcmd eq 'del') {
+ my ($name) = @_;
+ my @dcon;
+
+ unless(defined($name)) {
+ _io("No name given");
+ return;
+ }
+
+ unless (_config_list_has(['defined-connectors'], $name)) {
+ _io("Connector does not exist");
+ return;
+ }
+
+ if (_config_has(['connectors', $name, '_immutable'])) {
+ _io("Connector cannot be removed");
+ return;
+ }
+
+ # Remove from list of active connectors
+ _config_list_del(['defined-connectors'], $name);
+ _config_list_del(['active-connectors'], $name);
+
+ _config_del(['connectors', $name, 'name']);
+ _config_del(['connectors', $name, '_immutable']);
+ _config_del(['connectors', $name, 'schemas', 'http']);
+ _config_del(['connectors', $name, 'schemas', 'https']);
+
+ @dcon = split(/,/, _config_get(['active-connectors']));
+
+ if (scalar(@dcon) == 0) {
+ _io("List of selected connectors is empty, resetting to direct");
+ _config_list_add(['active-connectors', 'direct']);
+ }
+ } elsif ($subcmd eq 'addschema') {
+ my ($conn, $schema, $proxy) = @_;
+
+ unless(defined($conn)) {
+ _io("No connector name given");
+ return;
+ }
+
+ unless(defined($schema)) {
+ _io("No schema given");
+ return;
+ }
+
+ unless(defined($proxy)) {
+ _io("No proxy given");
+ return;
+ }
+
+ $conn = lc($conn);
+ unless(_config_list_has(['defined-connectors'], $conn)) {
+ _io("Connector does not exist");
+ return;
+ }
+
+ if (_config_has(['connectors', $conn, '_immutable'])) {
+ _io("Connector cannot be modified");
+ return;
+ }
+
+ $schema = lc($schema);
+ _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
+ } elsif ($subcmd eq 'delschema') {
+ my ($conn, $schema) = @_;
+
+ unless(defined($conn)) {
+ _io("No connector name given");
+ return;
+ }
+
+ unless(defined($schema)) {
+ _io("No schema given");
+ return;
+ }
+
+ $conn = lc($conn);
+ unless(_config_list_has(['defined-connectors'], $conn)) {
+ _io("Connector does not exist");
+ return;
+ }
+
+ $schema = lc($schema);
+ _config_del(['connectors', $conn, 'schemas', $schema]);
+ } elsif ($subcmd eq 'select') {
+ my @connlist = map { lc } @_;
+
+ if (scalar(@connlist) == 0) {
+ _io("No connectors given");
+ return;
+ }
+
+ foreach (@connlist) {
+ unless(_config_list_has(['defined-connectors'], $_)) {
+ _io("Connector %s does not exist", $_);
+ return;
+ }
+ }
+
+ _config_list_set(['active-connectors'], @connlist);
+ } else {
+ _io("connector [list|add|del|addschema|delschema|help] <options>");
+ _io(" help: Show this help");
+ _io(" list: List the defined connectors");
+ _io(" add <name>: Add a connector with name <name>");
+ _io(" del <name>: Delete the connector with name <name>");
+ _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
+ _io(" delschema <name> <schema>: Remove the schema from the connector");
+ _io(" select <name> [<name>...]: Select the connectors to use");
+ }
+}
+
+#
+# Enable debug.
+# Global debug if the keyword "all" is given, or just for the
+# current window otherwise
+#
+sub _cmd_debug {
+ my $event = shift;
+ my $scope = shift;
+
+ if (defined($scope) and (lc($scope) eq 'all')) {
+ _io("Global debug enabled");
+ $debug = 1;
+ } else {
+ _io("Debug for this window enabled");
+ $debugwindows{$event->{window}} = 1;
+ }
+}
+
+#
+# Disable debug
+# Disable global debug if the keyword "all" is given (this will
+# also disable all per-window debugs) or just for the current
+# window
+#
+sub _cmd_nodebug {
+ my $event = shift;
+ my $scope = shift;
+
+ if (defined($scope) and (lc($scope) eq 'all')) {
+ $debug = 0;
+ %debugwindows = ();
+ _io("Global debug disabled");
+ } else {
+ delete($debugwindows{$event->{window}});
+ _io("Debug for this window disabled");
+ }
+}
+
+
+#
+# Return the list of loaded grabbers.
+# This is used by the test programs, and is not meant to be
+# used in general.
+#
+sub _grabbers {
+ return @grabbers;
+}
+
+#
+# ==============================================
+# Builtin config handling functions
+# These are used if the library used does not
+# register it's own config_* handlers
+# ==============================================
+#
+sub _builtin_config_init {
+
+ if (defined($builtin_config_path)) {
+ my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
+
+ _debug("Trying to load configuration from %s", $filename);
+
+ if (-r $filename) {
+ eval {
+ local $/;
+ open(CONF, '<', $filename);
+ %builtin_config = %{JSON->new->utf8->decode(<CONF>)};
+ close(CONF);
+ } or do {
+ _io("Error loading configuration: %s", $@);
+ }
+ };
+ } elsif (defined($builtin_config_default)) {
+ _debug("Initializing builtin config from external default");
+ foreach (keys(%{$builtin_config_default})) {
+ _debug("Setting %s=%s", $_, $builtin_config_default->{$_});
+ $builtin_config{$_} = $builtin_config_default->{$_};
+ }
+ }
+}
+
+sub _builtin_config_get {
+ return $builtin_config{join(".", @{$_[0]})};
+}
+
+sub _builtin_config_set {
+ $builtin_config{join(".", @{$_[0]})} = $_[1];
+}
+
+sub _builtin_config_has {
+ return exists($builtin_config{join(".", @{$_[0]})});
+}
+
+sub _builtin_config_save {
+
+ if (defined($builtin_config_path)) {
+ my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json');
+
+ _debug("Attempting to save config to %s", $filename);
+
+ eval {
+ my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path);
+ print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config);
+ close($tempfile);
+ rename($tempfn, $filename);
+ } or do {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+sub _builtin_config_del {
+ delete($builtin_config{join(".", @{$_[0]})});
+}
+
+#
+# ==============================================
+# From this point on publicly callable functions
+# ==============================================
+#
+
+
+#
+# Initialization function for the library
+# Actually not the first thing to be called, it expects an API
+# has (register_api) to be registered first
+#
+sub init {
+ unless($remote_api) {
+ $error = "No API set";
+ return 0;
+ }
+
+ # Initialize configuration data
+ $remote_api->{config_init}->();
+
+ # Check/create default values, if they do not exist
+ _recursive_hash_walk($defaultconfig, \&_init_config_item);
+
+ # Load modules
+ _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
+
+ unless (@grabbers && @getters) {
+ _io('No grabbers or no getters found, can not proceed.');
+ return 0;
+ }
+
+ # Set the getter
+ $getter = $getters[0];
+ foreach my $p (@getters) {
+ if (_config_get(['getter']) eq $p->{'NAME'}) {
+ $getter = $p;
+ }
+ }
+ _debug('Selected %s as getter', $getter->{'NAME'});
+ _config_set(['getter'], $getter->{'NAME'});
+
+ # Done.
+ _io('initialized successfully');
+ return 1;
+}
+
+#
+# Register a remote API. This API contains a basic output function (used
+# when no window specific function is available), some config functions
+# and a color code function.
+#
+sub register_api {
+ my $a = shift;
+ my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
+ my $c;
+ my @missing;
+
+ unless(defined($a)) {
+ die("No API given");
+ }
+
+ #
+ # The config_* handlers are special in that they either all have
+ # provided by the user, or none. In the latter case builtin
+ # handlers will be used, but the config will not persist.
+ #
+ $c = 0;
+ foreach (@config_functions) {
+ if (exists($a->{$_})) {
+ $c++;
+ } else {
+ push(@missing, $_);
+ }
+ }
+
+ unless (($c == 0) or ($c == scalar(@config_functions))) {
+ $error = sprintf("Missing config function: %s", $missing[0]);
+ return 0;
+ }
+
+ foreach (keys(%{$a})) {
+ if (ref($a->{$_}) ne 'CODE') {
+ $error = sprintf("API handler %s is not a subroutine reference", $_);
+ }
+ $remote_api->{$_} = $a->{$_};
+ }
+
+ if (exists($a->{_debug})) {
+ $debug = $a->{_debug}->();
+ }
+
+ if (exists($a->{_config_path})) {
+ $builtin_config_path = $a->{_config_path}->();
+ }
+
+ if (exists($a->{_config_default})) {
+ $builtin_config_default = $a->{_config_default}->();
+ }
+
+ @outputstack = ({ewpf => $remote_api->{'io'}, window => ""});
+
+ return 1;
+}
+
+#
+# Check a message for useable links
+#
+sub check_for_link {
+ my $event = shift;
+ my $message = $event->{message};
+ my $g;
+ my $m;
+ my $p;
+ my $skip;
+ my $mode = _config_get(['mode']);
+
+
+ #
+ # If /nosave is present in the message switch to display mode, regardless
+ # of config setting
+ #
+ if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
+ $mode = 'display';
+ }
+
+ _push_output($event);
+ $message = _expand_url_shortener($message);
+
+ study($message);
+
+ # Offer the message to all Grabbers in turn
+ GRABBER: foreach $g (@grabbers) {
+ ($m, $p) = $g->get($message);
+ while (defined($m)) {
+ _debug('Metadata: %s', Dumper($m));
+ $skip = 0;
+ if (exists($remote_api->{link_callback})) {
+ $skip = $remote_api->{link_callback}->($m);
+ }
+ unless($skip) {
+ if ('download' eq $mode) {
+ _io(
+ sprintf('%s>>> %sSaving %s%%s%s %s%%s',
+ _colorpair('*red'),
+ _colorpair(),
+ _colorpair('*yellow'),
+ _colorpair(),
+ _colorpair('*green'),
+ ),
+ $m->{'SOURCE'},
+ $m->{'TITLE'}
+ );
+ unless($getter->get($m)) {
+ _io(sprintf('%s>>> FAILED', _colorpair('*red')));
+ }
+ } elsif ('display' eq $mode) {
+ _io(
+ sprintf('%s>>> %sSaw %s%%s%s %s%%s',
+ _colorpair('*magenta'),
+ _colorpair(),
+ _colorpair('*yellow'),
+ _colorpair(),
+ _colorpair('*green')
+ ),
+ $m->{'SOURCE'},
+ $m->{'TITLE'}
+ );
+ } else {
+ _io(sprintf('%s>>> Invalid operation mode', _colorpair('*red')));
+ }
+ }
+
+ # Remove the matched part from the message and try again (there may be
+ # more!)
+ $message =~ s/$p//;
+ study($message);
+ last GRABBER if ($message =~ /^\s*$/);
+
+ ($m, $p) = $g->get($message);
+ }
+ }
+
+ _pop_output();
+}
+
+#
+# Handle a videosite command (/videosite ...) entered in the client
+#
+sub handle_command {
+ my $event = shift;
+ my ($cmd, @params) = split(/\s+/, $event->{message});
+
+ _push_output($event);
+
+ if (exists($videosite_commands->{$cmd})) {
+ $videosite_commands->{$cmd}->($event, @params);
+ }
+
+ _pop_output();
+}
+
+1;
use Getopt::Long;
use File::Spec;
use File::Basename;
+use Module::Load;
use Cwd qw(realpath);
+use Carp;
-sub ploader {
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
- my $dir = shift;
- my $pattern = shift;
- my $type = shift;
- my @list;
- my $p;
- my $g;
- my @g = ();
-
- unshift(@INC, $dir);
-
- opendir(D, $dir) || return ();
- @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
- closedir(D);
-
- foreach $p (@list) {
- $p =~ s/\.pm$//;
- eval qq{ require videosite::$p; };
- if ($@) {
- print("Failed to load plugin: $@");
- next;
- }
-
- $g = eval qq{ videosite::$p->new();};
- if ($@) {
- print("Failed to instanciate: $@");
- delete($INC{$p});
- next;
- }
-
- if ($type eq $g->{'TYPE'}) {
- push(@g, $g);
- } else {
- printf('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
- delete($INC{$p});
+my $info = 0;
+my $debug = 0;
+my %config = (
+ mode => 'download',
+ getter => 'filegetter',
+ 'plugin.youtube.QUALITY' => 'hd',
+ 'plugin.filegetter.FILEPATTERN' => './%3$s.flv',
+);
+
+sub link_callback {
+ my $m = shift;
+
+ if ($info) {
+ foreach (keys(%{$m})) {
+ printf("%s: %s\n", $_, defined($m->{$_})?$m->{$_}:'(undef)');
}
+ return 1;
+ } else {
+ print("Downloading $m->{'TITLE'}\n");
+ return 0;
}
-
- return @g;
-}
-
-sub connectors {
- my $c = {name => 'environment', schemas => {}};
-
- if (exists($ENV{'http_proxy'})) {
- $c->{schemas}->{'http'} = $ENV{'http_proxy'}
- }
-
- if (exists($ENV{'https_proxy'})) {
- $c->{schemas}->{'https'} = $ENV{'https_proxy'}
- }
-
- return ( $c );
}
-my $hq = 0;
-my $ext = '.flv';
-my $y;
-my $f;
-my $m;
-my @g;
-my $bp;
-my $info = 0;
-my $debug = 0;
-
GetOptions("i" => \$info, "d" => \$debug);
-# This is some dark magic to find out our real base directory,
-# where we hope to find our plugins.
-$bp = File::Spec->catdir(dirname(realpath($0)), 'videosite');
-unshift(@INC, dirname(realpath($0)));
+push(@INC, dirname(realpath($0)));
+load 'libvideosite';
-@g = ploader($bp, '.*Grabber\.pm$', 'grabber');
-($f) = ploader($bp, '^FileGetter\.pm$', 'getter');
-
-unless(@g and defined($f)) {
- print("No plugins could be loaded\n");
- exit 1;
+unless(libvideosite::register_api({
+ link_callback => \&link_callback,
+ _config_default => sub { return \%config },
+ _debug => sub { return $debug },
+})) {
+ die("Error registering API: $libvideosite::error");
}
-foreach (@g, $f) {
- $_->setio(sub { printf(@_); print("\n"); } );
- $_->setconn(\&connectors);
-
- if ($debug) {
- $_->setdebug(1);
- }
+unless(libvideosite::init()) {
+ die("Could not init libvideosite: $libvideosite::error");
}
-$f->setval('FILEPATTERN', './%3$s' . $ext);
-
foreach (@ARGV) {
- foreach $y (@g) {
- ($m, undef) = $y->get($_);
- if (defined($m)) {
- if ($info) {
- foreach (keys(%{$m})) {
- printf("%s: %s\n", $_, defined($m->{$_})?$m->{$_}:'(undef)');
- }
- } else {
- print("Downloading $m->{'TITLE'}\n");
- $f->get($m);
- }
- }
- }
+ printf("Handling %s...\n", $_);
+ libvideosite::check_for_link({
+ message => $_,
+ ewpf => sub { print @_, "\n" },
+ window => "",
+ });
}
--- /dev/null
+# shim to connect libvideosite to irssi
+#
+# (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+use strict;
+use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
+use vars qw($VERSION %IRSSI);
+use File::Spec;
+use Module::Load;
+use XML::Simple;
+use JSON -support_by_pp;
+use Carp;
+
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
+
+#
+# List of foreground colors. This list is not complete, it just
+# contains the colors needed by videosite.
+#
+# The % are doubled because these are used in sprintf.
+#
+my %foreground_colors = (
+ 'magenta' => '%%m',
+ '*magenta' => '%%M',
+ '*yellow' => '%%Y',
+ '*green' => '%%G',
+ '*red' => '%%R',
+ 'default' => '%%n',
+);
+
+#
+# This is a canary value used in the config system as the default
+# value. As irssi does not have a way to test if a setting exists
+# this is used instead. A config value is never expected to be set
+# to this value and be valid.
+#
+my $config_canary = "\1";
+
+#
+# Initialize the config subsystem. Called by the core.
+#
+# Due to historic reasons this has to deal with a number of possible config sources:
+# * irssi internal config
+# * JSON config, old format
+# * XML config, old format
+#
+# JSON and XML configs are parsed, converted and moved to the irssi internal
+# format. This happens only once, as the config search stops with the first
+# format found
+#
+sub config_init {
+ my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
+ my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
+ my $conf;
+
+ # Check for irssi internal config. If not found...
+ if (config_has(['config-version'])) {
+ # Configuration in irssi config file. We're done.
+ return;
+ }
+
+ # Try to find old config files and load them.
+ if (-r $conffile) {
+ Irssi::print("Converting configuration from videosite.json. This will happen only once.");
+ eval {
+ local $/;
+ open(CONF, '<', $conffile);
+ $conf = JSON->new->utf8->decode(<CONF>);
+ close(CONF);
+ };
+ } elsif (-r $xmlconffile) {
+ Irssi::print("Converting configuration from videosite.xml. This will happen only once.");
+ $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
+ } else {
+ # No old config files around. Just exit.
+ return;
+ }
+
+ #
+ # Configuration conversion:
+ # Replace this structure:
+ #
+ # key => {
+ # content => value
+ # }
+ #
+ # with this structure
+ #
+ # key => value
+ #
+ Irssi::print("Converting configuration, stage 1");
+
+ # Only the getter/grabbers have this, so just check that part of the config
+ foreach my $g (keys(%{$conf->{videosite}->{config}})) {
+ foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
+ if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
+ $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
+ }
+ }
+ }
+
+ #
+ # Walk the configuration hash, creating irssi config entries for
+ # each leaf node.
+ #
+ # Some config values changed, so not the entire config is copied over.
+ # There is a helper function for this in libvideosite that we're using.
+ #
+ Irssi::print("Converting configuration, stage 2");
+
+ # Copy the "basic" settings.
+ foreach (qw(getter mode)) {
+ config_set([$_], $conf->{videosite}->{$_});
+ }
+
+ # Copy the per-getter/setter settings
+ foreach my $g (keys(%{$conf->{videosite}->{config}})) {
+ foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
+ config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
+ }
+ }
+
+ # Copy the connectors. The connectors themselves are copied as-is,
+ # the list of active connectors is copied under a different name,
+ # and a list of all existing connectors is created
+ my @connectors;
+
+ foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
+ push(@connectors, $c);
+ config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
+ if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
+ config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
+ }
+ foreach (qw(http https)) {
+ if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
+ config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
+ }
+ }
+ }
+ config_set(['active-connectors'], join(",", @{$conf->{videosite}->{connectorlist}}));
+ config_set(['defined-connectors'], join(",", @connectors));
+ config_set(['config-version'], '2');
+}
+
+#
+# Reading a configuration value. Called by the core
+#
+sub config_get {
+ my $path = shift;
+ my $item = join('.', 'videosite', @{$path});
+ my $val;
+
+
+ Irssi::settings_add_str('videosite', $item, $config_canary);
+ $val = Irssi::settings_get_str($item);
+
+ return ($val ne $config_canary)?$val:undef;
+}
+
+#
+# Returns a true value if the config item exists
+#
+sub config_has {
+ my $path = shift;
+ my $item = join('.', 'videosite', @{$path});
+
+ Irssi::settings_add_str('videosite', $item, $config_canary);
+ return Irssi::settings_get_str($item) ne $config_canary;
+}
+
+#
+# Setting a configuration value. Called by the core
+#
+sub config_set {
+ my $path = shift;
+ my $value = shift;
+ my $item = join('.', 'videosite', @{$path});
+
+ Irssi::settings_add_str('videosite', $item, $config_canary);
+ Irssi::settings_set_str($item, $value);
+}
+
+#
+# Delete a configuration value. Called by the core.
+#
+# Now, according to the configuration Irssi::settings_remove() removes a
+# config settings. This does not work in any irssi version available to me.
+# So just set the key to the canary value.
+#
+sub config_del {
+ my $path = shift;
+
+ config_set($path, $config_canary);
+}
+
+#
+# Return a color code. Called by the core
+#
+# Does not handle background colors yet.
+#
+sub colorpair {
+ my ($fg, $bg) = @_;
+
+ $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
+ $bg = '';
+
+ return $fg . $bg;
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+ my ($cmdline, $server, $witem) = @_;
+ my %event = (
+ message => $cmdline,
+ ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
+ window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
+ );
+
+ libvideosite::handle_command(\%event);
+}
+
+#
+# Handle a received message
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+ my ($server, $msg, $nick, $userhost, $channel) = @_;
+ my $witem = $server->window_item_find($channel);
+ my %event = (
+ message => $msg,
+ ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
+ window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
+ );
+
+ libvideosite::check_for_link(\%event);
+}
+
+sub videosite_reset {
+ my $libpath;
+
+ # Find out the script directory, and add it to @INC.
+ # This is necessary to find libvideosite.pm
+ $libpath = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
+
+ # If the library is already loaded unload it
+ foreach (keys(%INC)) {
+ if ($INC{$_} eq File::Spec->catfile($libpath, 'libvideosite.pm')) {
+ delete($INC{$_});
+ }
+ }
+
+ push(@INC, $libpath);
+ load 'libvideosite';
+
+ unless(libvideosite::register_api({
+ io => sub { Irssi::print($_[0]) },
+ config_init => \&config_init,
+ config_get => \&config_get,
+ config_set => \&config_set,
+ config_has => \&config_has,
+ config_save => sub { 1 },
+ config_del => \&config_del,
+ color => \&colorpair,
+ module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
+ quote => sub { s/%/%%/g; return $_ },
+ reload => \&videosite_reset,
+ })) {
+ Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ unless(libvideosite::init()) {
+ Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ return 1;
+}
+
+sub videosite_init {
+
+ if (videosite_reset()) {
+ signal_add_last("message public", sub { message_hook(@_) });
+ signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
+ signal_add_last("message private", sub { message_hook($_[0], $_[1], $_[2], $_[3], $_[2]) });
+ signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
+ signal_add_last("message irc action", sub { message_hook(@_) });
+ signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
+
+ Irssi::command_bind('videosite', sub { videosite_hook(@_) });
+ }
+}
+
+videosite_init();
use Getopt::Long;
use File::Spec;
use File::Basename;
+use Module::Load;
use Cwd qw(realpath);
+use Carp;
-sub ploader {
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
- my $dir = shift;
- my $pattern = shift;
- my $type = shift;
- my @list;
- my $p;
- my $g;
- my @g = ();
-
- unshift(@INC, $dir);
-
- opendir(D, $dir) || return ();
- @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
- closedir(D);
-
- foreach $p (@list) {
- $p =~ s/\.pm$//;
- eval qq{ require videosite::$p; };
- if ($@) {
- print("Failed to load plugin: $@");
- next;
- }
-
- $g = eval qq{ videosite::$p->new();};
- if ($@) {
- print("Failed to instanciate: $@");
- delete($INC{$p});
- next;
- }
-
- if ($type eq $g->{'TYPE'}) {
- push(@g, $g);
- } else {
- printf('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
- delete($INC{$p});
- }
- }
-
- return @g;
-}
-
-sub connectors {
- my $c = {name => 'environment', schemas => {}};
-
- if (exists($ENV{'http_proxy'})) {
- $c->{schemas}->{'http'} = $ENV{'http_proxy'}
- }
-
- if (exists($ENV{'https_proxy'})) {
- $c->{schemas}->{'https'} = $ENV{'https_proxy'}
- }
-
- return ( $c );
-}
-
-
-my $hq = 0;
-my $ext = '.flv';
-my $y;
-my $f;
-my $m;
-my @g;
-my $bp;
my $debug = 0;
-my ($success, $notest, $fail) = (0,0,0);
-
-GetOptions("d" => \$debug);
-
-# This is some dark magic to find out our real base directory,
-# where we hope to find our plugins.
-$bp = File::Spec->catdir(dirname(realpath($0)), 'videosite');
-unshift(@INC, dirname(realpath($0)));
-
-@g = ploader($bp, '.*Grabber\.pm$', 'grabber');
-($f) = ploader($bp, '^FileGetter\.pm$', 'getter');
-
-unless(@g and defined($f)) {
- print("No plugins could be loaded\n");
- exit 1;
+my %config = (
+ mode => 'download',
+ getter => 'filegetter',
+);
+my $success = 0;
+my $fail = 0;
+my $notest = 0;
+
+push(@INC, dirname(realpath($0)));
+load 'libvideosite';
+
+unless(libvideosite::register_api({
+ _debug => sub { return $debug },
+})) {
+ die("Error registering API: $libvideosite::error");
}
-foreach (@g, $f) {
- $_->setio(sub { printf(@_); print("\n"); } );
-
- if ($debug) {
- $_->setdebug(1);
- $_->setconn(\&connectors);
- }
+unless(libvideosite::init()) {
+ die("Could not init libvideosite: $libvideosite::error");
}
select(STDOUT);
$| = 1;
printf("Doing self tests:\n");
-foreach(@g) {
+foreach my $g (libvideosite::_grabbers()) {
my $r;
- printf(" %s...", $_->{'NAME'});
- $r = $_->_selftest();
+ if (@ARGV) {
+ my $found;
+
+ # If there are grabber names given on the command line check
+ # the current name against that list and skip if not present
+ $found = grep { $_ eq $g->{'NAME'} } @ARGV;
+
+ if ($found == 0) {
+ next;
+ }
+ }
+
+ printf(" %s...", $g->{'NAME'});
+ $r = $g->_selftest();
if(defined($r)) {
if ($r == 1) {
printf(" OK\n");
--- /dev/null
+# shim to connect libvideosite to weechat
+#
+# (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+use strict;
+use File::Spec;
+use Module::Load;
+use Data::Dumper;
+use Carp;
+
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
+
+weechat::register(
+ "videosite",
+ "Ralf Ertzinger (ralf\@skytale.net)",
+ "0.1",
+ "GPL",
+ "videosite Video URL grabber script (usage: /videosite)",
+ "",
+ "");
+
+#
+# Reading a configuration value. Called by the core
+#
+sub config_get {
+ my $path = shift;
+ my $item = join('.', @{$path});
+
+ if (weechat::config_is_set_plugin($item)) {
+ return weechat::config_get_plugin($item);
+ } else {
+ return undef;
+ }
+}
+
+#
+# Returns a true value if the config item exists
+#
+sub config_has {
+ my $path = shift;
+ my $item = join('.', @{$path});
+
+ return weechat::config_is_set_plugin($item);
+}
+
+#
+# Setting a configuration value. Called by the core
+#
+sub config_set {
+ my $path = shift;
+ my $value = shift;
+ my $item = join('.', @{$path});
+
+ weechat::config_set_plugin($item, $value);
+}
+
+#
+# Delete a configuration value. Called by the core.
+#
+sub config_del {
+ my $path = shift;
+ my $item = join('.', @{$path});
+
+ weechat::config_unset_plugin($item);
+}
+
+#
+# Return a color code. Called by the core
+#
+sub colorpair {
+ my ($fg, $bg) = @_;
+
+ return weechat::color($fg . ",", $bg);
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+ my ($data, $buffer, $args) = @_;
+ my %event = (
+ message => $args,
+ ewpf => sub { weechat::print($buffer, @_) },
+ window => $buffer,
+ );
+
+ libvideosite::handle_command(\%event);
+
+ return weechat::WEECHAT_RC_OK;
+}
+
+#
+# Handle a received message.
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+ my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message ) = @_;
+ my %event = (
+ message => $message,
+ ewpf => sub { weechat::print($buffer, @_) },
+ window => $buffer
+ );
+
+ libvideosite::check_for_link(\%event);
+
+ return weechat::WEECHAT_RC_OK;
+}
+
+#
+# Reset the plugin
+#
+sub videosite_reset {
+ unless(libvideosite::register_api({
+ io => sub { weechat::print("", @_) },
+ config_init => sub {},
+ config_get => \&config_get,
+ config_set => \&config_set,
+ config_has => \&config_has,
+ config_save => sub { 1 },
+ config_del => \&config_del,
+ color => \&colorpair,
+ module_path => sub { return File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl') },
+ quote => sub { return $_ },
+ reload => sub { weechat::print("", "Please use \"/script reload ...\" to reload") },
+ })) {
+ weechat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ unless(libvideosite::init()) {
+ weechat::print("", sprintf("videosite init failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ return 1;
+}
+
+sub videosite_init {
+ # Find out the script directory, and add it to @INC.
+ # This is necessary to find libvideosite.pm
+
+ push(@INC, File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl'));
+ load 'libvideosite';
+
+ if (videosite_reset()) {
+ weechat::hook_print("", "notify_message", "://", 1, "message_hook", "");
+ weechat::hook_print("", "notify_private", "://", 1, "message_hook", "");
+ weechat::hook_print("", "notify_highlight", "://", 1, "message_hook", "");
+ weechat::hook_print("", "notify_none", "://", 1, "message_hook", "");
+ weechat::hook_command( "videosite", "videosite control functions", "", "", "", "videosite_hook", "");
+ }
+}
+
+videosite_init();
--- /dev/null
+# shim to connect libvideosite to xchat2
+#
+# (c) 2007-2013 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+use strict;
+use File::Spec;
+use Module::Load;
+use Data::Dumper;
+use Xchat;
+
+#
+# List of foreground colors. This list is not complete, it just
+# contains the colors needed by videosite.
+#
+my %foreground_colors = (
+ 'magenta' => "\x0313",
+ '*magenta' => "\x0313\x02",
+ '*yellow' => "\x038\x02",
+ '*green' => "\x039\x02",
+ '*red' => "\x035\x02",
+ 'default' => "\x0f",
+);
+
+#
+# A hash to hold the config
+#
+my %config = ();
+
+Xchat::register(
+ "videosite",
+ "0.1",
+ "videosite Video URL grabber script (usage: /videosite)");
+
+#
+# Return a color code. Called by the core
+#
+# Does not handle background colors yet.
+#
+sub colorpair {
+ my ($fg, $bg) = @_;
+
+ $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
+ $bg = '';
+
+ return $fg . $bg;
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+ my (undef, $msg) = @_;
+ my %event;
+ my $context = Xchat::get_context();
+
+ %event = (
+ message => $msg->[1],
+ ewpf => sub {
+ my $oldcxt = Xchat::get_context();
+ Xchat::set_context($context);
+ Xchat::print(@_);
+ Xchat::set_context($oldcxt);
+ },
+ window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
+ );
+
+ libvideosite::handle_command(\%event);
+
+ return Xchat::EAT_XCHAT;
+}
+
+#
+# Handle a received message.
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+ my $msg = shift;
+ my %event;
+ my $context = Xchat::get_context();
+
+ %event = (
+ message => $msg->[1],
+ ewpf => sub {
+ my $oldcxt = Xchat::get_context();
+ Xchat::set_context($context);
+ Xchat::print(@_);
+ Xchat::set_context($oldcxt);
+ },
+ window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
+ );
+
+ libvideosite::check_for_link(\%event);
+ return Xchat::EAT_NONE;
+}
+
+#
+# Reset the plugin
+#
+sub videosite_reset {
+ unless(libvideosite::register_api({
+ io => sub { Xchat::print(@_) },
+ _config_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir")) },
+ color => \&colorpair,
+ module_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir"), 'perl') },
+ quote => sub { return $_ },
+ reload => \&videosite_reset,
+ })) {
+ Xchat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ unless(libvideosite::init()) {
+ Xchat::print("", sprintf("videosite init failed: %s", $libvideosite::error));
+ return 0;
+ }
+
+ return 1;
+}
+
+sub videosite_init {
+ # Find out the script directory, and add it to @INC.
+ # This is necessary to find libvideosite.pm
+
+ push(@INC, File::Spec->catfile(Xchat::get_info("xchatdir"), 'perl'));
+ load 'libvideosite';
+
+ if (videosite_reset()) {
+ foreach ('Channel Message', 'Your Message') {
+ Xchat::hook_print($_, \&message_hook);
+ }
+ Xchat::hook_command('videosite', \&videosite_hook);
+ }
+}
+
+videosite_init();
+++ /dev/null
-# autodownload flash videos
-#
-# (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
-# licensed under GNU GPL v2
-#
-# Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
-# which in turn is
-# based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
-
-
-BEGIN {
- # Get rid of a (possibly old) version of BettIrssi
- # This is a hack to prevent having to reload irssi just
- # because BettIrssi.pm changed
-
- delete($INC{'BettIrssi.pm'});
-}
-
-use strict;
-use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
-use vars qw($VERSION %IRSSI);
-use XML::Simple;
-use Data::Dumper;
-use File::Spec;
-use File::Temp qw(tempfile);
-use BettIrssi 101 qw(_bcb _bcs);
-use LWP::UserAgent;
-use JSON -support_by_pp;
-
-my @grabbers;
-my @getters;
-my $getter;
-my $conf;
-my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
-my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
-my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
-my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
-my @outputstack = (undef);
-
-my $PARAMS = {
- 'getter' => '',
- 'mode' => 'download',
- 'connectorlist' => ['direct'],
- 'connectors' => {},
-};
-
-
-# activate debug here
-my $debug = 0;
-
-# "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
-signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
-# "message own_public", SERVER_REC, char *msg, char *target
-signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)}));
-
-# "message private", SERVER_REC, char *msg, char *nick, char *address
-signal_add_last(_bcs("message private" => sub {check_for_link(@_)}));
-# "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
-signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)}));
-
-# "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
-signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)}));
-# "message irc own_action", SERVER_REC, char *msg, char *target
-signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
-
-# For tab completion
-# This does not use BettIrssi (yet)
-signal_add_first('complete word', \&sig_complete);
-
-sub push_output {
- unshift(@outputstack, shift);
-}
-
-sub pop_output {
- shift(@outputstack);
-
- @outputstack = (undef) unless (@outputstack);
-}
-
-my $videosite_commands = {
- 'save' => sub {
- cmd_save();
- },
-
- 'set' => sub {
- cmd_set(@_);
- },
-
- 'show' => sub {
- cmd_show(@_);
- },
-
- 'help' => sub {
- cmd_help(@_);
- },
-
- 'getter' => sub {
- cmd_getter(@_);
- },
-
- 'enable' => sub {
- cmd_enable(@_);
- },
-
- 'disable' => sub {
- cmd_disable(@_);
- },
-
- 'reload' => sub {
- init_videosite(0);
- },
-
- 'mode' => sub {
- cmd_mode(@_);
- },
-
- 'connector' => sub {
- cmd_connector(@_);
- },
-
- 'debug' => sub {
- $debug = 1;
- foreach (@grabbers, @getters) {
- $_->setdebug(1);
- }
- write_irssi('Enabled debugging');
- },
-
- 'nodebug' => sub {
- $debug = 0;
- foreach (@grabbers, @getters) {
- $_->setdebug(0);
- }
- write_irssi('Disabled debugging');
- },
-};
-
-sub write_irssi {
- my @text = @_;
- my $output = $outputstack[0];
-
- my $format = "%%mvideosite: %%n" . shift(@text);
-
- # escape % in parameters from irssi
- s/%/%%/g foreach @text;
-
- if (defined $output) {
- $output->(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
- } else {
- Irssi::print(sprintf($format, @text));
- }
-
-}
-
-sub write_debug {
- if ($debug) {
- write_irssi(@_);
- }
-}
-
-sub expand_url_shortener {
- my $s = shift;
- my $os = '';
- my @urlshortener = (
- 'is\.gd/[[:alnum:]]+',
- 'otf\.me/[[:alnum:]]+',
- 'hel\.me/[[:alnum:]]+',
- '7ax\.de/[[:alnum:]]+',
- 'ow\.ly/[[:alnum:]]+',
- 'j\.mp/[[:alnum:]]+',
- 'bit\.ly/[[:alnum:]]+',
- 'tinyurl\.com/[[:alnum:]]+',
- 'pop\.is/[[:alnum:]]+',
- 'post\.ly/[[:alnum:]]+',
- '1\.ly/[[:alnum:]]+',
- '2\.ly/[[:alnum:]]+',
- 't\.co/[[:alnum:]]+',
- 'shar\.es/[[:alnum:]]+',
- 'goo\.gl/[[:alnum:]]+',
- );
- my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
- my $i = 10;
-
- OUTER: while (($os ne $s) and ($i > 0)) {
- study($s);
- $os = $s;
- $i--;
-
- foreach my $pattern (@urlshortener) {
- my $p = "https?:\/\/" . $pattern;
-
- write_debug("Matching %s against %s", $p, $s);
- if ($s =~ m|($p)|) {
- my $matched = $1;
- my $res;
-
- write_debug("Found %s", $matched);
- $res = $ua->head($matched);
- if ($res->is_redirect()) {
- my $new = $res->headers()->header("Location");
-
- write_debug("Replacing %s with %s", $matched, $new);
- $s =~ s/$matched/$new/;
- next OUTER;
- } else {
- write_debug("Error resolving %s", $matched);
- }
- }
- }
- }
-
- if ($i == 0) {
- write_debug("Loop terminated by counter");
- }
-
- write_debug("Final string: %s", $s);
-
- return $s;
-}
-
-sub connectorlist {
- my @c;
-
- foreach (@{$conf->{'videosite'}->{'connectorlist'}}) {
- push(@c, $conf->{'videosite'}->{'connectors'}->{$_});
- }
-
- return @c;
-}
-
-
-sub check_for_link {
- my $event = shift;
- my $message = $event->message();
- my $witem = $event->channel();
- my $g;
- my $m;
- my $p;
-
-
- # Look if we should ignore this line
- if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
- return;
- }
-
- push_output($event->ewpf);
- $message = expand_url_shortener($message);
-
- study($message);
-
- # Offer the message to all Grabbers in turn
- GRABBER: foreach $g (@grabbers) {
- ($m, $p) = $g->get($message);
- while (defined($m)) {
- write_debug('Metadata: %s', Dumper($m));
- if ('download' eq ($conf->{'videosite'}->{'mode'})) {
- write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
- unless($getter->get($m)) {
- write_irssi('%%R>>> FAILED');
- }
- } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
- write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
- } else {
- write_irssi('%%R>>> Invalid operation mode');
- }
-
- # Remove the matched part from the message and try again (there may be
- # more!)
- $message =~ s/$p//;
- study($message);
- last GRABBER if ($message =~ /^\s*$/);
-
- ($m, $p) = $g->get($message);
- }
- }
-
- pop_output();
-}
-
-sub cmd_save {
-
- eval {
- my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => Irssi::get_irssi_dir());
- print $tempfile JSON->new->pretty->utf8->encode($conf);
- close($tempfile);
- rename($tempfn, $conffile);
- };
- if ($@) {
- write_irssi('Could not save config to %s: %s', ($conffile, $@));
- } else {
- write_irssi('configuration saved to %s', $conffile);
- }
-}
-
-sub cmd_set {
- my $target = shift;
- my $key = shift;
- my $val = shift;
- my $p;
-
- foreach $p (@getters, @grabbers) {
- if ($p->{'NAME'} eq $target) {
- $p->setval($key, $val);
- return;
- }
- }
- write_irssi('No such module');
-}
-
-
-sub cmd_enable {
- my $target = shift;
- my $p;
-
- foreach $p (@grabbers) {
- if ($p->{'NAME'} eq $target) {
- $p->enable();
- return;
- }
- }
- write_irssi('No such module');
-}
-
-
-sub cmd_disable {
- my $target = shift;
- my $p;
-
- foreach $p (@grabbers) {
- if ($p->{'NAME'} eq $target) {
- $p->disable();
- return;
- }
- }
- write_irssi('No such module');
-}
-
-
-sub cmd_show {
- my $target = shift;
- my $p;
- my $e;
-
- if (defined($target)) {
- foreach $p (@getters, @grabbers) {
- if ($p->{'NAME'} eq $target) {
- write_irssi($p->getconfstr());
- return;
- }
- }
- write_irssi('No such module');
- } else {
- write_irssi('Loaded grabbers (* denotes enabled modules):');
- foreach $p (@grabbers) {
- $e = $p->_getval('enabled');
- write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
- };
-
- write_irssi('Loaded getters:');
- foreach $p (@getters) {
- write_irssi(' %s', $p->{'NAME'});
- };
- }
-}
-
-sub cmd_help {
- my $target = shift;
- my $p;
-
- if (defined($target)) {
- foreach $p (@getters, @grabbers) {
- if ($p->{'NAME'} eq $target) {
- write_irssi($p->gethelpstr());
- return;
- }
- }
- write_irssi('No such module');
- } else {
- write_irssi(<<'EOT');
-Supported commands:
- save: save the current configuration
- help [modulename]: display this help, or module specific help
- show [modulename]: show loaded modules, or the current parameters of a module
- set modulename parameter value: set a module parameter to a new value
- getter [modulename]: display or set the getter to use
- enable [modulename]: enable the usage of this module (grabbers only)
- disable [modulename]: disable the usage of this module (grabbers only)
- reload: reload all modules (this is somewhat experimental)
- mode [modename]: display or set the operation mode (download/display)
- connector [subcommand]: manage connectors (proxies)
- debug: enable debugging messages
- nodebug: disable debugging messages
-EOT
- }
-}
-
-sub cmd_getter {
- my $target = shift;
- my $p;
-
- if (defined($target)) {
- foreach $p (@getters) {
- if ($p->{'NAME'} eq $target) {
- $getter = $p;
- $conf->{'videosite'}->{'getter'} = $target;
- write_irssi("Getter changed to %s", $target);
- return;
- }
- }
- write_irssi('No such getter');
- } else {
- write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
- }
-}
-
-sub cmd_mode {
- my $mode = shift;
-
- if (defined($mode)) {
- $mode = lc($mode);
- if (('download' eq $mode) or ('display' eq $mode)) {
- $conf->{'videosite'}->{'mode'} = $mode;
- write_irssi('Now using %s mode', $mode);
- } else {
- write_irssi('Invalid mode: %s', $mode);
- }
- } else {
- write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
- }
-}
-
-sub cmd_connector {
- my $subcmd = shift;
- my $connconf = $conf->{'videosite'}->{'connectors'};
-
- unless(defined($subcmd)) {
- $subcmd = "help";
- }
-
- $subcmd = lc($subcmd);
-
- if ($subcmd eq 'list') {
- write_irssi("Defined connectors");
- foreach (keys(%{$connconf})) {
- write_irssi($_);
- my $schemas = $connconf->{$_}->{'schemas'};
- if (scalar(keys(%{$schemas})) == 0) {
- write_irssi(" No schemas defined");
- } else {
- foreach (keys(%{$schemas})) {
- write_irssi(' %s: %s', $_, $schemas->{$_});
- }
- }
- }
-
- write_irssi();
- write_irssi("Selected connectors: %s", join(", ", @{$conf->{'videosite'}->{'connectorlist'}}));
- } elsif ($subcmd eq 'add') {
- my ($name) = @_;
-
- unless(defined($name)) {
- write_irssi("No name given");
- return;
- }
-
- $name = lc($name);
-
- if (exists($connconf->{$_})) {
- write_irssi("Connector already exists");
- return;
- }
-
- $connconf->{$name} = {'name' => $name, 'schemas' => {}};
- } elsif ($subcmd eq 'del') {
- my ($name) = @_;
-
- unless(defined($name)) {
- write_irssi("No name given");
- return;
- }
-
- $name = lc($name);
-
- unless (exists($connconf->{$name})) {
- write_irssi("Connector does not exist");
- return;
- }
-
- if (exists($connconf->{$name}->{'_immutable'})) {
- write_irssi("Connector cannot be removed");
- return;
- }
-
- delete($connconf->{$name});
-
- # Remove from list of active connectors
- $conf->{'videosite'}->{'connectorlist'} =
- [ grep { $_ ne $name } @{$conf->{'videosite'}->{'connectorlist'}} ];
-
- if (scalar(@{$conf->{'videosite'}->{'connectorlist'}}) == 0) {
- write_irssi("List of selected connectors is empty, resetting to direct");
- $conf->{'videosite'}->{'connectorlist'} = [ 'direct' ];
- }
- } elsif ($subcmd eq 'addschema') {
- my ($conn, $schema, $proxy) = @_;
-
- unless(defined($conn)) {
- write_irssi("No connector name given");
- return;
- }
-
- $conn = lc($conn);
-
- if (exists($connconf->{$conn}->{'_immutable'})) {
- write_irssi("Connector cannot be modified");
- return;
- }
-
- unless(defined($schema)) {
- write_irssi("No schema given");
- return;
- }
-
- $schema = lc($schema);
-
- unless(defined($proxy)) {
- write_irssi("No proxy given");
- return;
- }
-
- unless(exists($connconf->{$conn})) {
- write_irssi("Connector does not exist");
- return;
- }
-
- $connconf->{$conn}->{'schemas'}->{$schema} = $proxy;
- } elsif ($subcmd eq 'delschema') {
- my ($conn, $schema) = @_;
-
- unless(defined($conn)) {
- write_irssi("No connector name given");
- return;
- }
-
- $conn = lc($conn);
-
- if (exists($connconf->{$conn}->{'_immutable'})) {
- write_irssi("Connector cannot be modified");
- return;
- }
-
- unless(defined($schema)) {
- write_irssi("No schema given");
- return;
- }
-
- $schema = lc($schema);
-
- unless(exists($connconf->{$conn})) {
- write_irssi("Connector does not exist");
- return;
- }
-
- delete($connconf->{$conn}->{'schemas'}->{$schema});
- } elsif ($subcmd eq 'select') {
- my @connlist = map { lc } @_;
-
- if (scalar(@connlist) == 0) {
- write_irssi("No connectors given");
- return;
- }
-
- foreach (@connlist) {
- unless(exists($connconf->{$_})) {
- write_irssi("Connector %s does not exist", $_);
- return;
- }
- }
-
- $conf->{'videosite'}->{'connectorlist'} = [ @connlist ];
- } else {
- write_irssi("connector [list|add|del|addschema|delschema|help] <options>");
- write_irssi(" help: Show this help");
- write_irssi(" list: List the defined connectors");
- write_irssi(" add <name>: Add a connector with name <name>");
- write_irssi(" del <name>: Delete the connector with name <name>");
- write_irssi(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
- write_irssi(" delschema <name> <schema>: Remove the schema from the connector");
- write_irssi(" select <name> [<name>...]: Select the connectors to use");
- }
-}
-
-
-
-
-# save on unload
-sub sig_command_script_unload {
- my $script = shift;
- if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
- cmd_save();
- }
-}
-
-sub ploader {
-
- my $dir = shift;
- my $pattern = shift;
- my $type = shift;
- my @list;
- my $p;
- my $g;
- my @g = ();
-
- opendir(D, $dir) || return ();
- @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
- closedir(D);
-
- foreach $p (@list) {
- write_debug("Trying to load $p:");
- $p =~ s/\.pm$//;
- eval qq{ require videosite::$p; };
- if ($@) {
- write_irssi("Failed to load plugin: $@");
- next;
- }
-
- $g = eval qq{ videosite::$p->new(); };
- if ($@) {
- write_irssi("Failed to instanciate: $@");
- delete($INC{$p});
- next;
- }
-
- write_debug("found $g->{'TYPE'} $g->{'NAME'}");
- if ($type eq $g->{'TYPE'}) {
- push(@g, $g);
- $g->setio(\&write_irssi);
- $g->setconn(\&connectorlist);
- } else {
- write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
- delete($INC{$p});
- }
- }
-
- write_debug("Loaded %d plugins", $#g+1);
-
- return @g;
-}
-
-sub _load_modules($) {
-
- my $path = shift;
-
- foreach (keys(%INC)) {
- if ($INC{$_} =~ m|^$path|) {
- write_debug("Removing %s from \$INC", $_);
- delete($INC{$_});
- }
- }
- @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
- @getters = ploader($path, '.*Getter\.pm$', 'getter');
-}
-
-
-sub init_videosite {
-
- my $bindings = shift;
- my $p;
-
- if (-r $conffile) {
- write_debug("Attempting JSON config load from %s", $conffile);
- eval {
- local $/;
- open(CONF, '<', $conffile);
- $conf = JSON->new->utf8->decode(<CONF>);
- close(CONF);
- };
- } elsif (-r $xmlconffile) {
- write_debug("Attempting XML config load from %s", $xmlconffile);
- $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
- }
-
- unless(defined($conf)) {
- # No config, start with an empty one
- write_debug('No config found, using defaults');
- $conf = { 'videosite' => { }};
- }
-
- foreach (keys(%{$PARAMS})) {
- unless (exists($conf->{'videosite'}->{$_})) {
- $conf->{'videosite'}->{$_} = $PARAMS->{$_};
- }
- }
-
- # Make sure there is a connector called 'direct', which defines no
- # proxies
- unless (exists($conf->{'videosite'}->{'connectors'}->{'direct'})) {
- $conf->{'videosite'}->{'connectors'}->{'direct'} = {
- 'name' => 'direct',
- '_immutable' => '1',
- 'schemas' => {},
- };
- }
-
- _load_modules($plugindir);
-
- unless (defined(@grabbers) && defined(@getters)) {
- write_irssi('No grabbers or no getters found, can not proceed.');
- return;
- }
-
- $getter = $getters[0];
- foreach $p (@getters) {
- if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
- $getter = $p;
- }
- }
- write_debug('Selected %s as getter', $getter->{'NAME'});
- $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
-
- # Loop through all plugins and load the config
- foreach $p (@grabbers, @getters) {
- $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
- }
-
- if ($bindings) {
-
- Irssi::signal_add_first('command script load', 'sig_command_script_unload');
- Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
- Irssi::signal_add('setup saved', 'cmd_save');
-
-
- Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
- }
-
- write_irssi('initialized successfully');
-}
-
-sub sig_complete {
- my ($complist, $window, $word, $linestart, $want_space) = @_;
- my @matches;
-
- if ($linestart !~ m|^/videosite\b|) {
- return;
- }
-
- if ('/videosite' eq $linestart) {
- # No command enterd so far. Produce a list of possible follow-ups
- @matches = grep {/^$word/} keys (%{$videosite_commands});
- } elsif ('/videosite set' eq $linestart) {
- # 'set' command entered. Produce a list of modules
- foreach (@grabbers, @getters) {
- push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
- };
- } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
- my $module = $1;
-
- foreach my $p (@getters, @grabbers) {
- if ($p->{'NAME'} eq $module) {
- @matches = $p->getparamlist($word);
- last;
- }
- }
- } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
- my $module = $1;
- my $param = $2;
-
- foreach my $p (@getters, @grabbers) {
- if ($p->{'NAME'} eq $module) {
- @matches = $p->getparamvalues($param, $word);
- last;
- }
- }
- }
-
-
- push(@{$complist}, sort @matches);
- ${$want_space} = 0;
-
- Irssi::signal_stop();
-}
-
-sub cmdhandler {
- my $event = shift;
- my ($cmd, @params) = split(/\s+/, $event->message());
-
- push_output($event->ewpf);
-
- if (exists($videosite_commands->{$cmd})) {
- $videosite_commands->{$cmd}->(@params);
- }
-
- pop_output();
-}
-
-unshift(@INC, $scriptdir);
-init_videosite(1);
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'asyncfilegetter',
+ @_,
+ );
- $self->{'NAME'} = 'asyncfilegetter';
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'asyncwgetfilegetter',
+ @_,
+ );
- $self->{'NAME'} = 'asyncwgetfilegetter';
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = {'_DEBUG' => 0,
- '_OUT' => sub {printf(@_)},
- '_CONNECTORS' => sub { return ({ 'name' => 'direct',
- 'schemas' => {} }) },
- '_CONNECTOR' => undef,
+ my $self = {'_CONNECTOR' => undef,
+ _API => {
+ io => sub { print(@_) },
+ io_debug => sub { print(@_) },
+ connectors => sub { return ({ 'name' => 'direct',
+ 'schemas' => {} }) },
+ },
+ @_,
};
+ # Add the 'enabled' property to all modules
+ $self->{_PARAMS}->{enabled} = [1, 'Whether the module is enabled'];
bless($self, $class);
- $self->_prepare_parameters();
-
return $self;
}
$data[0] = "(" . ref($self) . ") " . $data[0];
- $self->{'_OUT'}(@data);
+ $self->{_API}->{io}->(@data);
}
sub debug {
my $self = shift;
my @data = @_;
- $data[0] = "DEBUG: " . $data[0];
- if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
-}
-
-sub mergeconfig {
- my $self = shift;
- my $c = shift;
- my $o;
-
- return $self->{'_CONFIG'} unless defined($c);
+ $data[0] = "(" . ref($self) . ") " . $data[0];
- foreach $o (keys(%{$c->{'option'}})) {
- if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
- $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
- }
- }
-
- return $self->{'_CONFIG'};
-}
-
-sub _prepare_parameters {
- my $self = shift;
- my $p;
-
- $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
-
- foreach $p (keys(%{$self->{'_PARAMS'}})) {
- $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
- }
+ $self->{_API}->{io_debug}->(@data);
}
sub _getval {
my $self = shift;
my $key = shift;
+ my $path = ['plugin', $self->{NAME}, $key];
my $val;
- $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
- $self->debug('Returning %s=%s', $key, $val);
+ # Try to read from the global config
+ # Fall back to default
+ if ($self->{_API}->{config_has}->($path)) {
+ $val = $self->{_API}->{config_get}->($path);
+ } elsif (exists($self->{_PARAMS}->{$key})) {
+ $val = $self->{_PARAMS}->{$key}->[0];
+ } else {
+ $self->error('Requested unknown config key %s', $key);
+ }
+ $self->debug('Returning %s=%s', $key, $val);
return $val;
}
my $self = shift;
my $key = shift;
my $val = shift;
+ my $path = ['plugin', $self->{NAME}, $key];
- if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
- $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
+ if (exists($self->{'_PARAMS'}->{$key})) {
+ $self->{_API}->{config_set}->($path, $val);
} else {
- $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
+ $self->error('Module does not have a parameter named %s', $self->$key);
}
}
-sub setio {
- my $self = shift;
- my $io = shift;
-
- $self->{'_OUT'} = $io;
-}
-
sub getconfstr {
my $self = shift;
my $s = 'Options for ' . $self->{'NAME'} . ":\n";
my $k;
my $p;
- foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
- $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
- $p =~ s/%/%%/g;
+ foreach $k (keys(%{$self->{'_PARAMS'}})) {
+ $p = $self->_getval($k);
$s .= sprintf(" %s: %s", $k, $p);
- if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
+ if ($p eq $self->{'_PARAMS'}->{$k}->[0]) {
$s .= " (default)\n";
} else {
$s .= "\n";
}
$s .= " Options:\n";
- foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
+ foreach $k (keys(%{$self->{'_PARAMS'}})) {
$p = $self->{'_PARAMS'}->{$k}->[0];
- $p =~ s/%/%%/g;
if (exists($self->{'_PARAMS'}->{$k}->[2])) {
# The parameter has a list of allowed values. Add the keys and their help
$s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
return $s;
}
-sub setdebug {
- my $self = shift;
-
- $self->{'_DEBUG'} = shift;
-}
-
sub ua {
my $self = shift;
my $ua;
sub connectors {
my $self = shift;
- return $self->{'_CONNECTORS'}->();
+ return $self->{_API}->{connectors}->();
}
sub selectconn {
$self->{'_CONNECTOR'} = shift;
}
-sub setconn {
+#
+# This function was used in previous versions of videosite. If it's called
+# we are dealing with an old plugin which probably needs some minor modifications
+# to work properly.
+#
+# Generate a warning message.
+#
+sub _prepare_parameters {
+ my $self = shift;
+
+ $self->error("THIS MODULE IS CALLING _prepare_parameters(). THIS FUNCTION IS DEPRECATED. See readme.txt in the plugin directory.");
+}
+
+#
+# Register a callbacks into the core API to the plugin.
+# Example of those are config getter/setters and IO functions
+# The API is a hash reference containing subroutine references.
+#
+sub register_api {
my $self = shift;
+ my $api = shift;
- $self->{'_CONNECTORS'} = shift;
+ $self->{_API} = $api;
}
1;
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'bliptv';
- $self->{_SELFTESTURL} = 'http://blip.tv/rebelliouspixelscom/buffy-vs-edward-twilight-remixed-2274024';
- $self->{_SELFTESTTITLE} = 'Buffy vs Edward (Twilight Remixed)';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/\S+/\S+)'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'bliptv',
+ _SELFTESTURL => 'http://blip.tv/rebelliouspixelscom/buffy-vs-edward-twilight-remixed-2274024',
+ _SELFTESTTITLE => 'Buffy vs Edward (Twilight Remixed)',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/\S+/\S+)'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'break',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*break.com/index/([-a-zA-Z0-9_]+?)\.html)'],
+ @_,
+ );
- $self->{'NAME'} = 'break';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*break.com/index/([-a-zA-Z0-9_]+?)\.html)'];
-
- bless($self, $class);
-
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'broadcaster',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*broadcaster\.com/clip/(\d+))'],
+ @_,
+ );
- $self->{'NAME'} = 'broadcaster';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*broadcaster\.com/clip/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'collegehumor';
- $self->{_SELFTESTURL} = 'http://www.collegehumor.com/video/5635400/pixar-intro-parody';
- $self->{_SELFTESTTITLE} = 'Pixar Intro Parody';
- $self->{'PATTERNS'} = ['(http://www.collegehumor.com/video:(\d+))',
- '(http://www.collegehumor.com/video/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'collegehumor',
+ _SELFTESTURL => 'http://www.collegehumor.com/video/5635400/pixar-intro-parody',
+ _SELFTESTTITLE => 'Pixar Intro Parody',
+ PATTERNS => ['(http://www.collegehumor.com/video:(\d+))',
+ '(http://www.collegehumor.com/video/(\d+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'dailymotion';
- $self->{_SELFTESTURL} = 'http://www.dailymotion.com/video/xylv6u_moon-duo-sleepwalker_music';
- $self->{_SELFTESTTITLE} = 'Moon Duo - Sleepwalker';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*dailymotion.com/(?:[^/]+/)*video/([-a-zA-Z0-9_]+))'];
-
- bless($self, $class);
-
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'dailymotion',
+ _SELFTESTURL => 'http://www.dailymotion.com/video/xylv6u_moon-duo-sleepwalker_music',
+ _SELFTESTTITLE => 'Moon Duo - Sleepwalker',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*dailymotion.com/(?:[^/]+/)*video/([-a-zA-Z0-9_]+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'doubleviking',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*doubleviking.com/videos/(?:[-a-zA-Z0-9_ %]+/)*page0\.html/(\d+)\.html$)'],
+ @_,
+ );
- $self->{'NAME'} = 'doubleviking';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*doubleviking.com/videos/(?:[-a-zA-Z0-9_ %]+/)*page0\.html/(\d+)\.html$)'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'filegetter';
- $self->{'_PARAMS'} = {'MINFREE' => ['500000', 'The amount of space that needs to be available on the filesystem before the video is downloaded (in kilobytes)'], 'FILEPATTERN', => ['/tmp/%s - %s - %s.flv', "The file name to save the file under. This is a string which is passed to a sprintf call later on. The parameters passed to that sprintf call, in order, are:\n- The site the video is from\n- The ID of the video\n- The title of the video\n- The URL of the video file itself\n- The URL of the site the video was taken from\nAll parameters are encoded (space and / replaced by _)"]};
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'filegetter',
+ _PARAMS => {
+ MINFREE => ['500000', 'The amount of space that needs to be available on the filesystem before the video is downloaded (in kilobytes)'],
+ FILEPATTERN => ['/tmp/%s - %s - %s.flv', "The file name to save the file under. This is a string which is passed to a sprintf call later on. The parameters passed to that sprintf call, in order, are:\n- The site the video is from\n- The ID of the video\n- The title of the video\n- The URL of the video file itself\n- The URL of the site the video was taken from\nAll parameters are encoded (space and / replaced by _)"]
+ },
+ @_,
+ );
+
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self = {%{$self},
+ my $self = $class->SUPER::new(
NAME => 'FlashGetter',
- TYPE => 'getter'
- };
+ TYPE => 'getter',
+ @_,
+ );
return bless($self, $class);
}
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'google';
- $self->{'PATTERNS'} = ['(http://video\.google\.com/videoplay\?docid=([-\d]+))'];
- $self->{'_PARAMS'} = {'QUALITY' => ['normal', 'Quality of the video to download. normal = standard resolution flash video, h264 = high resolution MPEG4 video']};
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'google',
+ PATTERNS => ['(http://video\.google\.com/videoplay\?docid=([-\d]+))'],
+ _PARAMS => {
+ QUALITY => ['normal', 'Quality of the video to download. normal = standard resolution flash video, h264 = high resolution MPEG4 video']
+ },
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self = {%{$self},
+ my $self = $class->SUPER::new(
NAME => 'FlashGrab',
TYPE => 'grabber',
PATTERNS => [],
- };
+ @_,
+ );
return bless($self, $class);
}
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'HTTPJSONGetter';
- $self->{'_PARAMS'} = {'URL' => ['http://www.example.com/getjson.pl', "The URL to call in order to trigger a download. The JSON encoded information will be POSTed to this URL."]};
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'HTTPJSONGetter',
+ _PARAMS => {
+ URL => ['http://www.example.com/getjson.pl', "The URL to call in order to trigger a download. The JSON encoded information will be POSTed to this URL."]
+ },
+ @_,
+ );
+
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'HTTPRPCGetter';
- $self->{'_PARAMS'} = {'URL' => ['http://www.example.com/get.pl?type=%s&vid=%s&title=%s&url=%s', "The URL to call in order to trigger a download. This is a string which is passed to a sprintf call later on. The parameters passed to that sprintf call, in order, are:\n- The site the video is from\n- The ID of the video\n- The title of the video\n- The URL of the video file itself\n- The URL of the site the video was taken from\nAll parameters are hexencoded"]};
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'HTTPRPCGetter',
+ _PARAMS => {
+ URL => ['http://www.example.com/get.pl?type=%s&vid=%s&title=%s&url=%s', "The URL to call in order to trigger a download. This is a string which is passed to a sprintf call later on. The parameters passed to that sprintf call, in order, are:\n- The site the video is from\n- The ID of the video\n- The title of the video\n- The URL of the video file itself\n- The URL of the site the video was taken from\nAll parameters are hexencoded"]
+ },
+ @_,
+ );
+
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'liveleak',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*liveleak.com/view\?i=([^\&]+))'],
+ @_,
+ );
- $self->{'NAME'} = 'liveleak';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*liveleak.com/view\?i=([^\&]+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'mncast',
+ PATTERNS => ['(http://www\.mncast\.com/\?(\d+))'],
+ @_,
+ );
- $self->{'NAME'} = 'mncast';
- $self->{'PATTERNS'} = ['(http://www\.mncast\.com/\?(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'metacafe',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*metacafe.com/watch/(\d+)(?:\S+)?)'],
+ @_,
+ );
- $self->{'NAME'} = 'metacafe';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*metacafe.com/watch/(\d+)(?:\S+)?)'];
-
- bless($self, $class);
-
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'motherless';
- $self->{_SELFTESTURL} = 'http://motherless.com/4976432';
- $self->{_SELFTESTTITLE} = 'Teen masturbation in shower';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*motherless.com/([a-zA-Z0-9]+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'motherless',
+ _SELFTESTURL => 'http://motherless.com/4976432',
+ _SELFTESTTITLE => 'Teen masturbation in shower',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*motherless.com/([a-zA-Z0-9]+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
$p = HTML::TokeParser->new(\$content);
# Look for the title
- while ($tag = $p->get_tag('title', 'script')) {
- if ('title' eq $tag->[0]) {
- my $t = $p->get_text();
- $metadata->{'TITLE'} = $t;
- $metadata->{'TITLE'} =~ s/.* : *//;
+ while ($tag = $p->get_tag('meta', 'script')) {
+ if (('meta' eq $tag->[0]) and ($tag->[1]->{'name'}) and ($tag->[1]->{'name'} eq 'description')) {
+ $metadata->{'TITLE'} = $tag->[1]->{'content'};
} elsif ('script' eq $tag->[0]) {
my $t = $p->get_text();
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'myvideo',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*myvideo.de/watch/(\d+))'],
+ @_,
+ );
- $self->{'NAME'} = 'myvideo';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*myvideo.de/watch/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'nullgetter',
+ @_,
+ );
- $self->{'NAME'} = 'nullgetter';
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub get {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'redtube';
- $self->{_SELFTESTURL} = 'http://www.redtube.com/8269';
- $self->{_SELFTESTTITLE} = 'Porn bloopers with pretty girl';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'redtube',
+ _SELFTESTURL => 'http://www.redtube.com/8269',
+ _SELFTESTTITLE => 'Porn bloopers with pretty girl',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub div($$) {
@ISA = qw(videosite::GrabberBase);
use XML::Simple;
+use HTML::TokeParser;
use Data::Dumper;
use strict;
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'sevenload';
- $self->{_SELFTESTURL} = 'http://de.sevenload.com/videos/uqDvKzh-vilogo-TV-Spot';
- $self->{_SELFTESTTITLE} = 'vilogo TV-Spot';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*sevenload.com/videos/(\w+?)-.*)'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'sevenload',
+ _SELFTESTURL => 'http://www.sevenload.com/videos/twins-one-guitar-5122ed655a1cb35c41003c64',
+ _SELFTESTTITLE => 'Twins one guitar',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*sevenload\.com/videos/.+-([[:alnum:]]+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
my $pattern = shift;
my $content;
my $metadata = {};
- my $p = XML::Simple->new();
+ my $p;
my $t;
$url =~ m|$pattern|;
$metadata->{'TITLE'} = undef;
$metadata->{'DLURL'} = undef;
+ # Get the HTML page for the title
+ unless(defined($content = $self->simple_get($url))) {
+ $self->error('Could not download HTM');
+ return undef;
+ }
+
+ $p = HTML::TokeParser->new(\$content);
+ while ($t = $p->get_tag('meta')) {
+ if ('meta' eq $t->[0]) {
+ if (exists($t->[1]->{'property'}) and ($t->[1]->{'property'} eq 'og:title')) {
+ $metadata->{'TITLE'} = $t->[1]->{'content'};
+ }
+ }
+ }
+
# Get the XML file containing the video metadata
- unless(defined($content = $self->simple_get(sprintf('http://flash.sevenload.com/player?itemId=%s', $2)))) {
+ unless(defined($content = $self->simple_get(sprintf('http://player-services.sevenload.com/p/1/sp/1/playManifest/entryId/%s', $2)))) {
$self->error('Could not download XML metadata');
return undef;
}
+ $p = XML::Simple->new();
unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
$self->error('Could not parse XML metadata');
return undef;
}
# Loop through the video streams
- foreach(@{$t->{'playerconfig'}->{'playlists'}->{'playlist'}->{'items'}->{'item'}->{'videos'}->{'video'}->{'streams'}->{'stream'}}) {
- if ($_->{'quality'} eq 'high') {
- $metadata->{'DLURL'} = $_->{'locations'}->{'location'}->{'content'};
- }
- }
- $metadata->{'TITLE'} = $t->{'playerconfig'}->{'playlists'}->{'playlist'}->{'items'}->{'item'}->{'title'};
+ $metadata->{'DLURL'} = $t->{'manifest'}->{'media'}->{'url'};
unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
$self->error('Could not extract download URL and title');
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'snotr';
- $self->{_SELFTESTURL} = 'http://www.snotr.com/video/1836';
- $self->{_SELFTESTTITLE} = 'Brilliant thief';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*snotr\.com/video/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'snotr',
+ _SELFTESTURL => 'http://www.snotr.com/video/1836',
+ _SELFTESTTITLE => 'Brilliant thief',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*snotr\.com/video/(\d+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'spikedhumor',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*spikedhumor.com/articles/(\d+)(?:/.*)*)'],
+ @_,
+ );
- $self->{'NAME'} = 'spikedhumor';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*spikedhumor.com/articles/(\d+)(?:/.*)*)'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'veoh';
- $self->{_SELFTESTURL} = 'http://www.veoh.com/watch/v18348952fyn2twbe';
- $self->{_SELFTESTTITLE} = '518_2 kureyon shinchan';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*veoh.com/+watch/(\w+)\??)'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'veoh',
+ _SELFTESTURL => 'http://www.veoh.com/watch/v18348952fyn2twbe',
+ _SELFTESTTITLE => '518_2 kureyon shinchan',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*veoh.com/+watch/(\w+)\??)'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'vimeo';
- $self->{_SELFTESTURL} = 'http://vimeo.com/35055590';
- $self->{_SELFTESTTITLE} = 'Hello';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(?:m/)?(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'vimeo',
+ _SELFTESTURL => 'http://vimeo.com/35055590',
+ _SELFTESTTITLE => 'Hello',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(?:m/)?(\d+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'wimp';
- $self->{_SELFTESTURL} = 'http://www.wimp.com/insanebuilding/';
- $self->{_SELFTESTTITLE} = 'Insane building.';
- $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*wimp.com/([^/]+)/?)'];
-
- bless($self, $class);
-
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'wimp',
+ _SELFTESTURL => 'http://www.wimp.com/insanebuilding/',
+ _SELFTESTTITLE => 'Insane building.',
+ PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*wimp.com/([^/]+)/?)'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
+ my $self = $class->SUPER::new(
+ NAME => 'yahoo',
+ PATTERNS => ['(http://video\.yahoo\.com/watch/\d+/(\d+))'],
+ @_,
+ );
- $self->{'NAME'} = 'yahoo';
- $self->{'PATTERNS'} = ['(http://video\.yahoo\.com/watch/\d+/(\d+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'youtube';
- $self->{_SELFTESTURL} = 'http://www.youtube.com/watch?v=dMH0bHeiRNg';
- $self->{_SELFTESTTITLE} = 'Evolution of Dance - By Judson Laipply';
- $self->{'PATTERNS'} = ['(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch(?:_popup)?\?.*?v=([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch\#\!v=([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/embed/([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/[[:alnum:]]+\?v=([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/(?:user/)?[[:alnum:]]+#p/(?:\w+/)+\d+/([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/watch\?v=([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/([-a-zA-Z0-9_]+))',
- '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/\w+\?.*/([-a-zA-Z0-9_]+))'];
- $self->{'_PARAMS'} = {
- 'QUALITY' => ['normal', 'Quality of the video to download.', {
- 'normal' => 'standard resolution flash video',
- 'high' => 'higher resolution flash video',
- 'h264' => 'high resolution MPEG4 video',
- 'hd' => 'HD720 resolution'}],
- 'USERNAME' => ['', 'Username to use for YouTube login'],
- 'PASSWORD' => ['', 'Password to use for YouTube login'],
- 'HTTPS' => [1, 'Whether to use HTTPS (if available) to connect to YouTube']};
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'youtube',
+ _SELFTESTURL => 'http://www.youtube.com/watch?v=dMH0bHeiRNg',
+ _SELFTESTTITLE => 'Evolution of Dance - By Judson Laipply',
+ PATTERNS => ['(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch(?:_popup)?\?.*?v=([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch\#\!v=([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/embed/([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/[[:alnum:]]+\?v=([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/(?:user/)?[[:alnum:]]+#p/(?:\w+/)+\d+/([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/watch\?v=([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/([-a-zA-Z0-9_]+))',
+ '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/\w+\?.*/([-a-zA-Z0-9_]+))'],
+ _PARAMS => {
+ QUALITY => ['normal', 'Quality of the video to download.', {
+ normal => 'standard resolution flash video',
+ high => 'higher resolution flash video',
+ h264 => 'high resolution MPEG4 video',
+ hd => 'HD720 resolution'}],
+ USERNAME => ['', 'Username to use for YouTube login'],
+ PASSWORD => ['', 'Password to use for YouTube login'],
+ HTTPS => [1, 'Whether to use HTTPS (if available) to connect to YouTube']
+ },
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'zeropunctuation';
- $self->{_SELFTESTURL} = 'http://www.escapistmagazine.com/videos/view/zero-punctuation/5346-Amy';
- $self->{_SELFTESTTITLE} = 'Amy';
- $self->{'PATTERNS'} = ['(http://www.escapistmagazine.com/videos/view/zero-punctuation/([-A-Za-z0-9]+))'];
-
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ NAME => 'zeropunctuation',
+ _SELFTESTURL => 'http://www.escapistmagazine.com/videos/view/zero-punctuation/5346-Amy',
+ _SELFTESTTITLE =>'Amy',
+ PATTERNS => ['(http://www.escapistmagazine.com/videos/view/zero-punctuation/([-A-Za-z0-9]+))'],
+ @_,
+ );
+
+ return bless($self, $class);
}
sub _parse {
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'_PARAMS'} = {'FOO' => [42, 'This is the FOO parameter, twiddle it to do stuff']};
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ my $self = $class->SUPER::new(
+ _PARAMS => {'FOO' => [42, 'This is the FOO parameter, twiddle it to do stuff']},
+ @_,
+ );
+ return bless($self, $class);
}
-b) as seen in the example above, after declaring the parameter hash, call the
-method _prepare_parameters() on your class instance. This will convert the
-hash into the internally used data structure and prepare it for automatic
-loading and saving.
-
-c) to access one of the parameters, call the _getval() method, giving the name
+b) to access one of the parameters, call the _getval() method, giving the name
of the parameter as first argument. This will return the current value of that
-parameter (either the default value or the user defuned value).
+parameter (either the default value or the user defined value).