node-unshortener can unshort any url

Twitter Inc HQ

Image via Wikipedia

Lately I’ve been spending my evenings and nights working on a super secret and above awesome project that I will talk about … soon, when it’s a bit more ready.

tl; dr -> github link to source

That project deals mostly with Twitter and harnessing url’s from the stream. As anyone who’s sniffed at Twitter lately can atest, therea re many, many many strange urls floating around, using any number of url shortening schemes or what seems to have become quite fashionable, a custom domains that are shortenered via services nobody could guess at.

But when you’re trying to do something useful with these it’s usually much easier when you have the original url to work with. Especially when it comes to doing things for specific domains.

What’s needed is some sort of library that can unshort pretty much any url you throw at it. Surely someone has already made one right?

Turns out, outside of a few services, no such thing existed … and I didn’t want to use a third party service. Naturally I set out to make my own library for unshortening short links.

You can check out the source on github.

Example

It’s pretty simple to use, only has one function you should care about :)

1
2
3
4
5
6
7
8
9
var unshortener = require('unshortener');
 
// you can pass in a url object or string
unshortener.expand('http://fb.me/UXVqinvO',
                   function (url) {
                       console.log(url.href);
 
		       // prints: http://www.facebook.com/photo.php?pid=514346&l=30b4ab9bbd&id=132853273433650
                   });

Simple logic

The approach is pretty simple:

  • take a url
  • check if known service
  • know it –> use an API
  • wtf –> pretend to be a browser

As you can see, the library is pretty simple, just 68 sloc of JavaScript code. But simple solutions are good solutions I’m told and this little thing does everything I need it to do.

Right now it directly supports only bit.ly and is.gd, so if you know of any more shorteners with an expand API please tell me so I can add them. Following redirects is a bit messy and I’d prefer to do as much as possible through official API’s.

Enhanced by Zemanta

Related Posts

---
Need a freelance developer? Email me!

You should follow me on twitter
 Subscribe to RSS

5 responses so far

  • http://blog.neosavvy.com/wordpress/ Adam Parrish

     RockMelt has it’s own shortener me.lt

  • http://fgribreau.com/ Francois-Guillaume Ribreau

     I’m currently using another one “NodeJS Url-expander” : https://github.com/cakebaker/node-url-expander/

  • http://swizec.com Swizec

    Oh cool, didn’t find that when I was looking for it. Thanks. 

  • James

     http://code.google.com/apis/urlshortener/v1/getting_started.html

    goo.gl unshortener API

  • http://swizec.com Swizec

    Goo.gl is now supported, thanks :)

« 750 words a day keeps the insanity... Implementing a weighed random... »