General: Add support for URL shortening services:
authorRalf Ertzinger <ralf@skytale.net>
Mon, 12 Jul 2010 14:01:19 +0000 (16:01 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Mon, 12 Jul 2010 14:01:19 +0000 (16:01 +0200)
- bit.ly
- j.mp
- tinyurl.com

videosite.pl

index 00af09f..2926b0b 100644 (file)
@@ -23,6 +23,7 @@ use XML::Simple;
 use Data::Dumper;
 use File::Spec;
 use BettIrssi 101 qw(_bcb _bcs);
+use LWP::UserAgent;
 
 my @grabbers;
 my @getters;
@@ -148,6 +149,54 @@ sub write_debug {
     }
 }
 
+sub expand_url_shortener {
+    my $s = shift;
+    my $os = '';
+    my @urlshortener = (
+        'j\.mp/[[:alnum:]]+',
+        'bit\.ly/[[:alnum:]]+',
+        'tinyurl\.com/[[:alnum:]]+',
+        );
+    my $ua = LWP::UserAgent->new(agent => 'Mozilla');
+    my $i = 100;
+
+    OUTER: while (($os ne $s) and ($i > 0)) {
+        $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_success()) {
+                    my $new = $res->request()->uri();
+
+                    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,6 +212,7 @@ sub check_for_link {
     }
 
     push_output($event->ewpf);
+    $message = expand_url_shortener($message);
 
     study($message);