From 6a4c97ccada442bf5fd1b860ee10fb7a706d289e Mon Sep 17 00:00:00 2001 From: efuce Date: Wed, 15 Jun 2016 22:52:36 +0200 Subject: Updated tinyurl.pl to make use of HTTP::Tiny instead of LWP::UserAgent. Slightly adapted url detection regex to make it work with the current iteration of tinyurl.com. --- scripts/tinyurl.pl | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'scripts') diff --git a/scripts/tinyurl.pl b/scripts/tinyurl.pl index 31a097b..d1ecf84 100644 --- a/scripts/tinyurl.pl +++ b/scripts/tinyurl.pl @@ -3,13 +3,12 @@ # by Atoms use strict; -use IO::Socket; -use LWP::UserAgent; +use HTTP::Tiny; use vars qw($VERSION %IRSSI); use Irssi qw(command_bind active_win); -$VERSION = '1.0'; +$VERSION = '1.1'; %IRSSI = ( authors => 'Atoms', contact => 'atoms@tups.lv', @@ -33,24 +32,32 @@ command_bind( ); sub tinyurl { - my $url = shift; + my $url = shift; + my $content = "url=$url"; - #added to fix URLs containing a '&' - $url=url_encode($url); + #added to fix URLs containing a '&' + $url=url_encode($url); - my $ua = LWP::UserAgent->new; - $ua->agent("tinyurl for irssi/1.0 "); - my $req = HTTP::Request->new(POST => 'http://tinyurl.com/create.php'); - $req->content_type('application/x-www-form-urlencoded'); - $req->content("url=$url"); - my $res = $ua->request($req); + my %options = ( + agent => "tinyurl for irssi/1.0 " + ); - if ($res->is_success) { - return get_tiny_url($res->content); + my %form_params = ( + url => $url + ); + + my $ua = HTTP::Tiny->new(%options); + my $res = $ua->request('POST', 'http://tinyurl.com/create.php', { + content => $content, + headers => { 'content-type' => 'application/x-www-form-urlencoded' }, + }); + + if ($res->{success}) { + return get_tiny_url($res->{content}); } else { print CLIENTCRAP "ERROR: tinyurl: tinyurl is down or not pingable"; - return ""; - } + return ""; + } } #added because the URL was not being url_encoded. This would cause only @@ -64,7 +71,7 @@ sub url_encode { sub get_tiny_url($) { my $tiny_url_body = shift; - $tiny_url_body =~ /(.*)(tinyurl\svalue=\")(.*)(\")(.*)/; + $tiny_url_body =~ /(.*)(data\-clipboard\-text=\")(.*)(\")(.*)/; return $3; } -- cgit v1.2.3 From d3acf0e40a9d9b092a308e2a1e224980556631d0 Mon Sep 17 00:00:00 2001 From: efuce Date: Thu, 16 Jun 2016 10:03:50 +0200 Subject: Switched over to WWW::Shorten::TinyURL. Massively simplified the code. Consolidated code indentation style. --- scripts/tinyurl.pl | 67 ++++++++++++++++-------------------------------------- 1 file changed, 19 insertions(+), 48 deletions(-) mode change 100644 => 100755 scripts/tinyurl.pl (limited to 'scripts') diff --git a/scripts/tinyurl.pl b/scripts/tinyurl.pl old mode 100644 new mode 100755 index d1ecf84..829ee74 --- a/scripts/tinyurl.pl +++ b/scripts/tinyurl.pl @@ -3,7 +3,8 @@ # by Atoms use strict; -use HTTP::Tiny; +use WWW::Shorten::TinyURL; +use WWW::Shorten 'TinyURL'; use vars qw($VERSION %IRSSI); @@ -20,58 +21,28 @@ $VERSION = '1.1'; command_bind( tinyurl => sub { - my ($msg, $server, $witem) = @_; - my $answer = tinyurl($msg); - if ($answer) { - print CLIENTCRAP "$answer"; - if ($witem && ($witem->{type} eq 'CHANNEL' || $witem->{type} eq 'QUERY')) { - $witem->command("MSG " . $witem->{name} ." ". $answer); + my ($msg, $server, $witem) = @_; + my $answer = tinyurl($msg); + + if ($answer) { + print CLIENTCRAP "$answer"; + + if ($witem && ($witem->{type} eq 'CHANNEL' || $witem->{type} eq 'QUERY')) { + $witem->command("MSG " . $witem->{name} ." ". $answer); + } } - } } ); sub tinyurl { - my $url = shift; - my $content = "url=$url"; - - #added to fix URLs containing a '&' - $url=url_encode($url); - - my %options = ( - agent => "tinyurl for irssi/1.0 " - ); - - my %form_params = ( - url => $url - ); - - my $ua = HTTP::Tiny->new(%options); - my $res = $ua->request('POST', 'http://tinyurl.com/create.php', { - content => $content, - headers => { 'content-type' => 'application/x-www-form-urlencoded' }, - }); + my $url = shift; - if ($res->{success}) { - return get_tiny_url($res->{content}); - } else { - print CLIENTCRAP "ERROR: tinyurl: tinyurl is down or not pingable"; - return ""; - } -} - -#added because the URL was not being url_encoded. This would cause only -#the portion of the URL before the first "&" to be properly sent to tinyurl. -sub url_encode { - my $url = shift; - $url =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg; - return $url; -} + my $res = makeashorterlink($url); -sub get_tiny_url($) { - - my $tiny_url_body = shift; - $tiny_url_body =~ /(.*)(data\-clipboard\-text=\")(.*)(\")(.*)/; - - return $3; + if (defined $res) { + return $res; + } else { + print CLIENTCRAP "ERROR: tinyurl: tinyurl is down or not pingable"; + return ""; + } } -- cgit v1.2.3 From 7444951ba2199f2c78ace960899d0a73e630c53f Mon Sep 17 00:00:00 2001 From: efuce Date: Thu, 16 Jun 2016 10:07:39 +0200 Subject: Fixed an instance of indentation being different. --- scripts/tinyurl.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts') diff --git a/scripts/tinyurl.pl b/scripts/tinyurl.pl index 829ee74..6e6c1c9 100755 --- a/scripts/tinyurl.pl +++ b/scripts/tinyurl.pl @@ -13,7 +13,7 @@ $VERSION = '1.1'; %IRSSI = ( authors => 'Atoms', contact => 'atoms@tups.lv', - patch => 'spowers@dimins.com', + patch => 'spowers@dimins.com', name => 'tinyurl', description => 'create a tinyurl from a long one', license => 'GPL', -- cgit v1.2.3