[1+1=2]

OneAndOneIs2

« DecorationsAvada kedavra »

Tue, Dec 06, 2011

[Icon][Icon]Shrinking URLs

• Post categories: Omni, FOSS, My Life, Programming, Helpful

If you spend much time chatting online, you'll have encountered URLs that are far too long to be nice in a conversation. It's nice to have an alternative, and that's what tinyurl.com and other such services do - give them a long URL, and they give you a short one that redirects to the original.

Very nice.. but a bit tedious. Instead of

  • copy
  • paste
  • done

You have to mess around with

  • copy
  • go to tinyurl.com - manually or via bookmark...
  • select text box
  • paste
  • submit
  • select new url
  • copy
  • paste

Meh.

What you REALLY want is to press a button and it magically just does everything for you. And since the motto of Perl is "Making the easy things easy and the hard things possible" I figured it must be do-able, and in fact somebody else must have already done almost all the work for me.

I was right. The Perl module "Clipboard" and the 'curl' command-line app were all I needed to use Google's API to shorten URLs using the http://goo.gl service.

So now I have a keyboard shortcut to run a script. The script gets the URL from the clipboard, submits it to goo.gl, gets back the new short redirect, and copies it into the clipboard so I can paste it.

In other words, the new flow is:

  • copy
  • shortcut
  • paste

Job done!

It's useful enough to me that I thought I'd shunt it online in case anyone else wanted it. So here you go!

Follow up:


#!/usr/bin/perl
# Take URL from clipboard & copy smaller redirect URL to clipboard

use strict;
use warnings;
use Clipboard;

# Ugly fixify of cliboard order
if ('Clipboard::Xclip' eq $Clipboard::driver) {
no warnings 'redefine'; *Clipboard::Xclip::all_selections = sub {
qw(clipboard primary buffer secondary)
};
}

# Get the long URL from cliboard
my $url = Clipboard->paste;

# Get short URL via curl
my $short_url = qx#curl -s -d'&url=$url' http://goo.gl/api/url#;

# Returns in the format {"short_url":"http://goo.gl/xxxx","added_to_history":false}
# Crop out everything but the URL
$short_url =~ s#^.*goo.gl([^"]+).*#http://goo.gl$1#;

# Put the short URL onto the clipboard so we can paste it
Clipboard->copy($short_url);

10 comments

Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
URL shorteners are evil. They steal whatever link juice might be intended for the original site, taking away valuable online link sources and making the destination reliant on the URL service forever.

In an already fragile world of www URLs, shorteners are a crazy idea, intended purely for those who "do" twitter and other similar social media.
07/12/11 @ 10:48
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
..or who post them in conversations, like I said right at the top of the post.

Twitter shortens URLs itself these days, by the way - you don't need a seperate service
07/12/11 @ 19:14
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
yes, I agree short URLs have uses in purely temporary situations. What I objected to was that for non-temporary purposes, it is best to link direct.
08/12/11 @ 03:19
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
People shorten URLs on web pages? What's the point of that?!? Nobody cares how long a link is if it's just a Click Here.
08/12/11 @ 08:48
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
I've seen people use third-party linking services to maintain a blog roll. What irritates me is that these links usually start out by being "Free" and then the third party service adds its own adverts or frame to the linked page. Very annoying.
08/12/11 @ 12:53
Åke
Comment from: Åke [Visitor] · http://akeiexil.wordpress.com
Which clipboard is that? is it the X-middle-click-clipboard or is it the Ctrl+C-clipboard used in Gnome-KDE (GTK+/QT I guess) and other Apps?

12/12/11 @ 10:59
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
This is set to use the Ctrl-C / Ctrl-V clipboard, which works better with my settings.

By default (without the bit labelled "# Ugly fixify of cliboard order") it would use the middle-click clipboard instead.
12/12/11 @ 11:42
Åke
Comment from: Åke [Visitor] · http://akeiexil.wordpress.com
It's a pretty nifty script. It makes me sad that I can't understand a single line of perl. I tried to do this in python for fun and it works pretty well too, the drawback is that I used gtk for clipboard handling which creates an ugly dependency but makes it cross-platform.

The curse of open source is that it's always more fun to try doing something from scratch than reuse someone else's work... I promise I'll be better in the future.
21/12/11 @ 08:01
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Don't let it get you down: I couldn't understand a single non-code line of your blog post without Google Translate ;)
21/12/11 @ 09:03
Åke
Comment from: Åke [Visitor] · http://akeinexile.wordpress.com
Haha, yeah sorry about that...I'm not comfortable enough with English to dare use it as language in my blog.
21/12/11 @ 17:38

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
22/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

blogging soft