X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=videosite.pl;h=d87b5984fff28b5e01e19f961dc19a13d263c9c5;hb=faaacb2741009b8df6e855d74f38ef72197d6889;hp=cf47cc6f1a4c3e8c30a0dfc280ad00fea102fe71;hpb=ceaa51ce71b9531ca0f088d106f4c62b99638bda;p=videosite.git diff --git a/videosite.pl b/videosite.pl index cf47cc6..d87b598 100644 --- a/videosite.pl +++ b/videosite.pl @@ -22,7 +22,9 @@ 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; my @grabbers; my @getters; @@ -148,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(); @@ -163,11 +226,12 @@ sub check_for_link { } 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)); @@ -186,6 +250,7 @@ sub check_for_link { # more!) $message =~ s/$p//; study($message); + last GRABBER if ($message =~ /^\s*$/); ($m, $p) = $g->get($message); } @@ -196,10 +261,12 @@ sub check_for_link { 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, $@)); @@ -318,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; } } @@ -334,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); }