Skip to content

@jxson /bitly-node.md forked from joemccann/bitly-node.md
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
    }
    
  4. Type ESC:wq to save the file and close it.
  5. Restart your terminal.
  6. 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
Something went wrong with that request. Please try again.