Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A Perl script to create Tiny URLs UNIX
Usually, I make TinyURLs with this Dashboard widget. However, I get a bit frustrated due to the delay when a widget is launched for the first time. I love Tiny URLs for two reasons: (1) they fit in any mail or forum list with restricted line width support; and (2) they hide the address and then make it more surprising for the reader to open the URL.

So instead, here's a small Perl program that takes the contents of the clipboard (if no arguments are provided) or converts each argument into a Tiny URL, and then prints the Tiny URL version to the standard output.
#!/usr/bin/env perl
#
# testtinyurl.pl
#
# juanfc 2006-06-30
#
# 
use WWW::Shorten::TinyURL;
use WWW::Shorten 'TinyURL';

if (!@ARGV) {
    $small = makeashorterlink(`pbpaste`);
    system("echo -n $small | pbcopy" );
    print "$smalln";
} else {
    foreach $a (@ARGV) {
        print makeashorterlink($a), "n";
    }
}
As you can see you need WWW::Shorten::TinyURL, and this was the first problem for me since...
sudo perl -MCPAN -e 'install WWW::Shorten::TinyURL'
...failed in tests and couldn't install it. Finally, I found one to download on Dave Cross' CPAN page. When you install it, be careful with the place it wants to install. It will try to install into /Library -> Perl -> 5.8.6 -> lib -> perl5 -> site_perl -> WWW. That is wrong; you must move it to /Library/Perl/5.8.6/WWW/.

[robg adds: I haven't tested this one...]
    •    
  • Currently 2.57 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)
 
[23,989 views]  

A Perl script to create Tiny URLs | 12 comments | Create New Account
Click here to return to the 'A Perl script to create Tiny URLs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A Perl script to create Tiny URLs
Authored by: eaganj on Jul 05, '06 08:28:49AM
It might be easier to just use this bookmarklet:

javascript:void(location.href='http://tinyurl.com/create.php?url='+location.href)

I have TinyURL! bookmarked to that location and sitting on my Safari shortcuts bar. Then all I have to do is click that bookmarklet to get a TinyURL version of the current page.

[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: billbarstad on Jul 05, '06 08:45:07AM

Very nice! Thanks.



[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: jaysoffian on Jul 05, '06 09:34:54AM
http://www.macosxhints.com/article.php?story=200502042003188

(I need to update the TinyURLService - it doesn't work reliably in 10.4 - at least in Mail.app)

[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: scotty321 on Jul 05, '06 09:48:13AM
And remember that, as mentioned above, not only can you use this bookmarklet in Safari:
javascript:void(location.href='http://tinyurl.com/create.php?url='+location.href)

But also remember that any bookmarks that you add to Safari's toolbar are automatically assigned keyboard shortcuts of command-1, command-2, command-3, etc.!

So when you add that bookmarklet to your toolbar, all that you really need to press is command-1 (if it's the first one in your toolbar).

[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: whodisbe on Jul 05, '06 12:36:57PM
you beat me to it :)

like the last poster said, if you add a link to the bookmarks bar, it is just an option - # keystroke away. I just bookmarked "javascript:void(location.href='http://tinyurl.com/create.php?url='+location.href)" in my bookmarks bar, and voila. No need for extra scripts or anything.

[ Reply to This | # ]
A Perl^h^h^h^hRuby script to create Tiny URLs
Authored by: jeff_lamarche on Jul 05, '06 11:58:54AM
And for those of us who can't stand PERL and CPAN, here's a Ruby script that does the same thing, but with no reliance on libraries that have to be installed:

#!/usr/bin/env ruby
require 'net/http'
require 'uri'

in_url = ARGV[0]
url = URI.parse('http://tinyurl.com')
res = Net::HTTP.start(url.host, url.port) {|http|
  http.get("/create.php?url=#{in_url}")
}
search_string = "<input type=hidden name=tinyurl value="
body = res.body

url_index = body.index(search_string) + search_string.length + 1

search_end = body.index("\">", url_index)
puts body[url_index, search_end - url_index]


[ Reply to This | # ]
A Perl^h^h^h^hRuby script to create Tiny URLs
Authored by: pkishor on Jul 05, '06 04:08:43PM
now, now... that is not nice... to disparage Perl and CPAN...

#!/usr/bin/perl

use LWP;
my $a = LWP::UserAgent->new;

my $q = HTTP::Request->new(POST => 'http://tinyurl.com/create.php');
$q->content_type('application/x-www-form-urlencoded');
$q->content("url=$ARGV[0]");

my $r = $a->request($q);

if ($r->is_success) {
  my $h = $r->decoded_content;
  $h =~ /<blockquote><b>http:\/\/tinyurl.com\/(.*?)<\/b>/;
  print "http://tinyurl.com/$1";
}

>perl tinyurl.pl http://www.macosxhints.com
http://tinyurl.com/4oh69
>


[ Reply to This | # ]
A Perl^h^h^h^hRuby script to create Tiny URLs
Authored by: luomat on Aug 24, '06 01:14:08PM

AH!

Thank God for freedom from the damned CPAN!

This one "Just Worked"!



[ Reply to This | # ]
A Pox on TinyURL™ links
Authored by: Phosphor on Jul 05, '06 03:30:15PM
"I love Tiny URLs for two reasons:...(2) they hide the address and then make it more surprising for the reader to open the URL."
Reason number 2 is the exact reason loathe TinyURL links, specifically when they aren't accompanied by some bit of descriptive text about where the link will be taking me.

I don't like the fact that the true destination is obfuscated in both the pasted link and in your browser window's status bar. The majority of the time if I'm on a forum or site where I don't really know or trust the person dropping a TinyURL link (sans description) I won't even bother clicking through at all.

For my money a much better choice is urlx.org. The shortened bookmark it creates at least has some identifying information about where the casual clicker will end up.

For example, is there much of a question about where the following url(x) link is going to take you?

    http://urlx.org/macosxhints.com/c8b48
Go check out urlx.org. They also have a little bookmarklet for your browser toolbar.

[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: boredzo on Jul 05, '06 05:37:05PM

Another option is notlong.com, which gives you the option of picking a custom nickname for the URL (for example: http://macosxhints.notlong.com/. And a similar Perl API exists for notlong.



[ Reply to This | # ]
A Perl script to create Tiny URLs
Authored by: juanfal on Jul 06, '06 09:23:54AM
Thanks a lot for these so many ideas...

I have improved a little bit the Perl code (that I consider more usable and flexible than specific AppleScripts or long specific curl command lines with many scaped chars!)

#!/usr/bin/env perl
#
# testtinyurl.pl
#
# juanfc 2006-06-30
#
# 
use strict;
use warnings;

# use WWW::Shorten 'BabyURL';
# use WWW::Shorten 'Linkz';
# use WWW::Shorten 'MakeAShorterLink';
# use WWW::Shorten 'Metamark';
# use WWW::Shorten 'NotLong';
# use WWW::Shorten 'OneShortLink';
# use WWW::Shorten 'Shorl';
# use WWW::Shorten 'SnipURL';
# use WWW::Shorten 'TinyClick';
# use WWW::Shorten 'TinyLink';
use WWW::Shorten 'TinyURL';

use Data::Validate::URI qw(is_uri);

if (!@ARGV) {
    my $a = `pbpaste`;
    if ( !is_uri($a) ) {
        $a = "http://$a";
    }    
    my $small = makeashorterlink($a);
    system("echo -n $small | pbcopy" );
    print "$smalln";
} else {
    foreach my $a (@ARGV) {
        if ( !is_uri($a) ) {
            $a = "http://$a";
        }
        print makeashorterlink("$a"), "n";
    }
}
If you want to use NotLong (very interesting), you can set the preferred nickname easily:
makeashorterlink($long_url,nickname=>$nickname);
but the philosophy of NotLong seems to be more to have a real nickname for a stable site.

It's pity WWW::Shorten doesn't have support for urlx.org.

[ Reply to This | # ]

A Perl script to create Tiny URLs
Authored by: natebeaty on Feb 22, '07 10:39:47AM

Here's a modification of the Ruby script above for urlx (formatted as a TextMate command, replace ENV["TM_SELECTED_TEXT"] with ARGV[0] and print with puts for a shell command..)


#!/usr/bin/env ruby
require 'net/http'
require 'uri'

in_url = ENV["TM_SELECTED_TEXT"]
url = URI.parse('http://urlx.org')
res = Net::HTTP.start(url.host, url.port) {|http|
  http.get("/api.php?x=#{in_url}")
}
print res.body


[ Reply to This | # ]