Created
November 27, 2015 22:33
-
-
Save xdg/aba9959f859a83668983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use v5.10; | |
use strict; | |
use warnings; | |
use HTTP::Tiny; | |
my $url_in = shift || <> | |
or exit; | |
my $ht = HTTP::Tiny->new( max_redirect => 0 ); | |
( my $url_raw = $url_in ) =~ s{\?.*$}{?raw=1}; | |
my $res = $ht->head($url_raw); | |
if ( $res->{status} == 302 ) { | |
my $url_out = $res->{headers}{location}; | |
my $res2 = | |
$ht->post_form( "http://tinyurl.com/api-create.php", { url => $url_out } ); | |
if ( $res2->{success} ) { | |
say $res2->{content}; | |
} | |
else { | |
say $url_out; | |
} | |
} | |
else { | |
say $url_in; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment