Displays titles of URLs in IRSSI [ENG]

Topp

Okay, so I got really annoyed when URLs were posted and I actually had to click the URL to see what it was. I'm kind of lazy. So I pulled out my beer and perl-face and started forking an old Spotify script(which is now discontinued) and came up with this! What it does is quite simple, when a link/URL is posted on IRC, it looks up the titles of the webpage, fetches everything in between the title tags and displays it above the URL just posted(this includes Spotify links, it will then show the artist and title of the song linked). Like this:

1859.21 -!- [URL Title] Now they're imprinting ads on women's legs - The Raw Feed
1859.21 < varjen> http://therawfeed.com/now-theyre-imprinting-ads-on-womens-legs <3

This script is certainly not perfect, it still has some issues, such as with encoding. But it's no biggie, I've been using to for a while, and when I've patched it I'll publish the patch here as well. The encoding issues is mostly that it convert non-english letters such as "åäö" to ISO-8859-1 instead of UTF-8. So if you're using UTF-8 in your terminal, or on your server, you might have issues with those letters. But you'll hardly notice it, it won't fuck anything up, at the most those letters will be displayed quite weird. If you find any other issues, just comment below and I'll see to it. You can download the script here and you can find the source below.

uri.pl source

1. #####################################################################
2. # This script fetches the <title> of a URL posted and prints it
3. # in irssi.
4. #
5. # This script is basically forked from Toni Viemerö spotifyuri
6. # script, only slightly modified. You can find his website on 
7. # http://spotify.url.fi/
8. #
9. #
10. # You will need the following packages:
11. # LWP::UserAgent (You can install this using cpan -i LWP::UserAgent)
12. # Crypt::SSLeay  (You can install this using cpan -i Crypt::SSLeay)
13. # 
14. # Or if you're using Debian GNU/Linux:
15. # apt-get update;apt-get install libwww-perl libcrypt-ssleay-perl
16. #
17. #
18. # eth0 will prevail. || irc.eth0.info
19. #
20. #####################################################################
21. 
22. 
23. 
24. use strict;
25. use Irssi;
26. use Irssi::Irc;
27. use LWP::UserAgent;
28. use vars qw($VERSION %IRSSI);
29. use HTML::Entities;
30. 
31. $VERSION = '0.5';
32. %IRSSI = (
33.     authors     => 'Caesar "sniker" Ahlenhed',
34.     contact     => 'sniker@se.linux.org',
35.     name        => 'uri',
36.     description => 'Show titles of URLs posted',
37.     license     => 'BSD',
38.     url         => 'http://sniker.codebase.nu/',
39. );
40. 
41. sub uri_public {
42.     my ($server, $data, $nick, $mask, $target) = @_;
43.     my $retval = uri_get($data);
44.     my $win    = $server->window_item_find($target);
45.     if($retval =~ /<title>(.*?)<\/title>/s){
46.         $retval = $1;
47.         $retval =~ s/\n//g;
48.         $retval = decode_entities($retval);
49.         if ($win) {
50.             $win->print("[URL Title] $retval", MSGLEVEL_CRAP) if $retval;
51.         } else {
52.             Irssi::print("[URL Title] $retval") if $retval;
53.         }
54.     }
55. }
56. sub uri_private {
57.     my ($server, $data, $nick, $mask) = @_;
58.     my $retval = uri_get($data);
59.     my $win    = $server->window_item_find($nick);
60.     if($retval =~ /<title>(.*?)<\/title>/s){
61.         $retval = $1;
62.         $retval =~ s/\n//g;
63.         $retval = decode_entities($retval);
64.         if ($win) {
65.             $win->print("[URL Title] $retval", MSGLEVEL_CRAP) if $retval;
66.         } else {
67.             Irssi::print("[URL Title] $retval") if $retval;
68.         }
69.     }
70. }
71. 
72. sub uri_parse {
73.      my ($url) = @_;
74.      if ($url =~ /(https?:\/\/([^ ]+))/) {
75. #	 Irssi::print("[Debug] $1");
76.           return $1;
77.      }
78.      return 0;
79. }
80. 
81. sub uri_get {
82.     my ($data) = @_;
83. 
84.     my $url = uri_parse($data);
85. 
86.     my $ua = LWP::UserAgent->new(env_proxy=>1, keep_alive=>1, timeout=>5);
87.     $ua->agent("irssi/$VERSION " . $ua->agent());
88.     my $req = HTTP::Request->new('GET', $url);
89.     my $res = $ua->request($req);
90.     if ($res->is_success()) {
91.         return $res->content();
92.     }
93.     return 0;
94. }
95. 
96. Irssi::signal_add_last('message public', 'uri_public');
97. Irssi::signal_add_last('message private', 'uri_private');

Author

sniker[at]codebase[dot]nu

Updated Sun, 27 Feb 2011 21:39:44 +0100

Topp

blog comments powered by Disqus