Shorten URL’s with Bitly using YQL

Bitly is a great URL shortener which also provides a very neat API. If you’re as API driven as me and love to automate the tasks around you, you’ll be happy to note that there is a new open data table that can shorten URL’s using YQL.

The API is neat but why remember the query parameters of a service when YQL can do the work for you?

use 'http://nagiworld.net/yqldefs/bit.ly.shorten.xml';
select * from bit.ly.shorten where login='nag...' and apiKey='....'
           and longUrl='http://cnn.com/'

This looks pretty easy but seems like a lot of work for a single URL to be shortened. Isn’t it? You can always go to the bit.ly site and do the same in a single click. right? Yes! Infact the reason for this table is to shorten multiple URLs in a single statement using a YQL sub-select i.e.

select * from bit.ly.shorten where login='nag...' and apiKey='...'
       and longUrl in ('http://cnn.com', 'http://yahoo.com')

The output of the query looks like the following. Only the results element is shown here for brevity. Given this data, I can project the necessary fields and get either the shortened url or other elements.

    <results>
        <bitly>
            <errorCode>0</errorCode>
            <errorMessage/>
            <results>
                <nodeKeyVal>
                    <userHash>xzV1m</userHash>
                    <shortKeywordUrl/>
                    <hash>31IqMl</hash>
                    <nodeKey><![CDATA[http://cnn.com/]]></nodeKey>
                    <shortUrl>http://bit.ly/xzV1m</shortUrl>
                </nodeKeyVal>
            </results>
            <statusCode>OK</statusCode>
        </bitly>
        <bitly>
            <errorCode>0</errorCode>
            <errorMessage/>
            <results>
                <nodeKeyVal>
                    <userHash>h4H0n</userHash>
                    <shortKeywordUrl/>
                    <hash>2deaFR</hash>
                    <nodeKey><![CDATA[http://yahoo.com]]></nodeKey>
                    <shortUrl>http://bit.ly/h4H0n</shortUrl>
                </nodeKeyVal>
            </results>
            <statusCode>OK</statusCode>
        </bitly>
    </results>

Better yet, take an entire html page, rss/atom feed or even a CSV file as an input containing all the URL’s to be shortened and zip it through YQL in a single statement. Now, Insn’t that something cool!

select * from bit.ly.shorten where login = 'nag..' and apiKey='...'
    and longUrl in (select link from rss where url='.....')

Note: Once you register with bit.ly, you can find the apiKey by going to the Account section.

This table is already checked into the YQL Open Table github repository. There are many other open data tables of use ranging from bit.ly to amazon to iplocation contributed by the community.

Lastly, if you’d like to contribute your awesome open data table you can checkout Fork me! YQL Open Tables!. This article gives an excellent overview of how to get your table into github.

Share
  • del.icio.us
  • Facebook
  • Digg
  • description

Related Posts

No related posts.

Viewing 2 Comments

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus