Skip to content

Instantly share code, notes, and snippets.

@jxson
Forked from joemccann/bitly-node.md
Created May 9, 2011 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxson/963339 to your computer and use it in GitHub Desktop.
Save jxson/963339 to your computer and use it in GitHub Desktop.
Bitly shortening from the command line with node, motherfuckers.

This is for Mac OS X and requires npm.

Usage:

  1. Open a terminal
  2. Type bitly http://www.somewebsite.com/needs/to/be/shortened/

The shortened link is now in your clipboard. Paste the shortened link wherever you need to!

Installation:

  1. Open a terminal
  2. Type npm install request (if you don't already have the request module).
  3. Type cd /usr/local/lib/node
  4. Type vim bitly.js
  5. Now paste in the following code:
var request = require('request'),
		sys = require('sys'),
		link = process.ARGV[2],
		bitly = 'http://api.bit.ly/v3/shorten';
		
function urlencode(str) { 
return escape(str)
    .replace('+', '%2B')
    .replace('%20', '+')
    .replace('*', '%2A')
    .replace('/', '%2F').replace('@', '%40');
}

// prepend http if it does not exist.
if (!/^https?:\/\//i.test(link)) {
    link = 'http://' + link;
}

// urlencode the link and add to params.
var params = 'login=YOUR_LOGIN_ID&apiKey=YOUR_API_KEY&longUrl='+urlencode(link)+'%2F&format=json';

request({uri:bitly, body:params, method:'POST'},function(error,response,body){
	if(!error && response.statusCode === 200 && body)
	{
		var json = JSON.parse(body);
        console.log(json.data.url);
	}
});

NOTE: You will need to supply your own login and apiKey values.

  1. Type ESC:wq to save the file and close it.
  2. Back in your terminal type vim ~/.bash_profile
  3. Paste in the following block of code:
function bitly()
{
bitlyPath='/usr/local/lib/node/bitly.js'
node $bitlyPath $1 | pbcopy
}
  1. Type ESC:wq to save the file and close it.
  2. Restart your terminal.
  3. Type bitly www.google.com.

The bitly link for "www.google.com" is now in copied into your clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment