This Irssi script shortens URLs with is.gd, and v.gd.
It replaces links above a certain length in-place so it works in all window items.
Grab the code here, or take a look at the source:
1: # isgd.pl 2: # 3: # URL shortening with is.gd or v.gd 4: # 5: # Copyright (c) 2010 Szymon 'polemon' Bereziak <polemon@polemon.org> 6: # 7: # This script is licensed untder ISC license 8: # 9: 10: use strict; 11: use vars qw($VERSION %IRSSI); 12: 13: use Irssi; 14: use XML::LibXML 1.70; 15: 16: $VERSION = "1.2"; 17: 18: %IRSSI = ( 19: author => "Szymon 'polemon' Bereziak", 20: contact => "polemon\@polemon.org", 21: name => "is.gd", 22: description => "URL shortening with is.gd", 23: license => "ISC", 24: url => "http://polemon.org/isgd.pl", 25: changed => "2011-03-02", 26: ); 27: 28: Irssi::settings_add_int("isgd", "isgd_length", "14"); 29: Irssi::settings_add_str("isdg", "isgd_site", "is.gd"); 30: Irssi::signal_add_first("send text", "modline"); 31: Irssi::signal_add_last("setup changed", "init"); 32: 33: my $ureg = qr{}; 34: my $site = ""; 35: 36: &init; 37: 38: sub init { 39: my $i = Irssi::settings_get_int('isgd_length'); 40: $site = Irssi::settings_get_str('isgd_site'); 41: 42: $ureg = qr{ 43: \b( 44: (?:ht|f)tps?://\S{$i,} 45: )\b 46: }x; 47: } 48: 49: sub modline { 50: my ($line, $server, $witem) = @_; 51: $line =~ s/$ureg/shorturl($1)/ige; 52: 53: Irssi::signal_continue($line, $server, $witem); 54: } 55: 56: sub shorturl { 57: local $SIG{'__WARN__'} = sub { }; 58: my ($url) = @_; 59: $url =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; 60: 61: my $parser = XML::LibXML->new({"recover" => 2}); 62: my $doc = $parser->parse_html_file("http://$site/create.php?longurl=$url"); 63: 64: if($doc->getElementById("short_url")) { 65: return $doc->getElementById("short_url")->getAttribute("value"); 66: } 67: 68: return "@_"; 69: }
Features include:
When a link is posted, it sometimes takes a second or two until the shortened link appears. It's not really a bug, but causes some people to frown.
To change the minimal length for a link to be shortened, just set isgd_length appropriately:
/set isgd_length 14
The length describes the actual link-name part, without http://, ftps:// or the like.
To change the service, set isgd_site approptiately:
/set isgd_site v.gd
Right now, I only support is.gd and v.gd, as they use the same API.