X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=videosite.pl;h=d87b5984fff28b5e01e19f961dc19a13d263c9c5;hb=552ef86edaa5d6e39f1a9d63d743948eb9353d3c;hp=3f3b086a6ab32d7e3c56db9c31ddec30e6e3a074;hpb=8067b7d719813f025726ba956041d980d088a713;p=videosite.git diff --git a/videosite.pl b/videosite.pl index 3f3b086..d87b598 100644 --- a/videosite.pl +++ b/videosite.pl @@ -6,7 +6,15 @@ # Based on youtube.pl by Christian Garbs # which in turn is # based on trigger.pl by Wouter Coekaerts -# This is helena + + +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); @@ -14,8 +22,9 @@ use vars qw($VERSION %IRSSI); use XML::Simple; use Data::Dumper; use File::Spec; -delete($INC{'BettIrssi.pm'}); -use BettIrssi qw(_bcb _bcs); +use File::Temp qw(tempfile); +use BettIrssi 101 qw(_bcb _bcs); +use LWP::UserAgent; my @grabbers; my @getters; @@ -51,16 +60,17 @@ signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)})); 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(@putputstack, shift); + unshift(@outputstack, shift); } sub pop_output { shift(@outputstack); - unless(@outputstack) @outputstack = (undef); + @outputstack = (undef) unless (@outputstack); } my $videosite_commands = { @@ -140,6 +150,67 @@ sub write_debug { } } +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); + 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 check_for_link { my $event = shift; my $message = $event->message(); @@ -154,10 +225,13 @@ sub check_for_link { return; } + push_output($event->ewpf); + $message = expand_url_shortener($message); + study($message); # Offer the message to all Grabbers in turn - foreach $g (@grabbers) { + GRABBER: foreach $g (@grabbers) { ($m, $p) = $g->get($message); while (defined($m)) { write_debug('Metadata: %s', Dumper($m)); @@ -176,18 +250,23 @@ sub check_for_link { # more!) $message =~ s/$p//; study($message); + last GRABBER if ($message =~ /^\s*$/); ($m, $p) = $g->get($message); } } + + pop_output(); } sub cmd_save { + eval { - open(CONF, '>'.$conffile) or die 'Could not open config file'; - print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}); - close(CONF); + my ($tempfile, $tempfn) = tempfile("videosite.xml.XXXXXX", dir => Irssi::get_irssi_dir()); + print $tempfile XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}); + close($tempfile); + rename($tempfn, $conffile); }; if ($@) { write_irssi('Could not save config to %s: %s', ($conffile, $@)); @@ -306,6 +385,7 @@ sub cmd_getter { if ($p->{'NAME'} eq $target) { $getter = $p; $conf->{'videosite'}->{'getter'} = $target; + write_irssi("Getter changed to %s", $target); return; } } @@ -322,6 +402,7 @@ sub cmd_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); } @@ -372,7 +453,7 @@ sub ploader { write_debug("found $g->{'TYPE'} $g->{'NAME'}"); if ($type eq $g->{'TYPE'}) { push(@g, $g); - $g->setio(sub {Irssi::print(shift)}); + $g->setio(\&write_irssi); } else { write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type); delete($INC{$p}); @@ -446,7 +527,7 @@ sub init_videosite { Irssi::command_bind(_bcb('videosite' => \&cmdhandler)); } - write_irssi('videosite initialized'); + write_irssi('initialized successfully'); } sub sig_complete { @@ -497,9 +578,13 @@ 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);