Skip to content

Instantly share code, notes, and snippets.

@xdg
Created November 27, 2015 22:33
Show Gist options
  • Save xdg/aba9959f859a83668983 to your computer and use it in GitHub Desktop.
Save xdg/aba9959f859a83668983 to your computer and use it in GitHub Desktop.
#!/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