Skip to content

Instantly share code, notes, and snippets.

@jxson
Forked from joemccann/bitly-node.md
Created May 9, 2011 20:38

Revisions

  1. @joemccann joemccann revised this gist Oct 26, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bitly-node.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ request({uri:bitly, body:params, method:'POST'},function(error,response,body){
    });
    </pre>

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

    6. Type <code>ESC:wq</code> to save the file and close it.
    7. Back in your terminal type <code>vim ~/.bash_profile</code>
  2. @joemccann joemccann revised this gist Oct 26, 2010. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions bitly-node.md
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,9 @@ request({uri:bitly, body:params, method:'POST'},function(error,response,body){
    }
    });
    </pre>

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

    6. Type <code>ESC:wq</code> to save the file and close it.
    7. Back in your terminal type <code>vim ~/.bash_profile</code>
    8. Paste in the following block of code:
  3. @joemccann joemccann created this gist Oct 26, 2010.
    61 changes: 61 additions & 0 deletions bitly-node.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    This is for Mac OS X and requires [npm](http://github.com/isaacs/npm).

    Usage:
    ============
    1. Open a terminal
    2. Type <code>bitly http://www.somewebsite.com/needs/to/be/shortened/</code>

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

    Installation:
    =============
    1. Open a terminal
    2. Type <code>npm install request</code> (if you don't already have the request module).
    3. Type <code>cd /usr/local/lib/node</code>
    4. Type <code>vim bitly.js</code>
    5. Now paste in the following code:
    <pre>
    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);
    }
    });
    </pre>
    6. Type <code>ESC:wq</code> to save the file and close it.
    7. Back in your terminal type <code>vim ~/.bash_profile</code>
    8. Paste in the following block of code:
    <pre>
    function bitly()
    {
    bitlyPath='/usr/local/lib/node/bitly.js'
    node $bitlyPath $1 | pbcopy
    }
    </pre>
    9. Type <code>ESC:wq</code> to save the file and close it.
    10. Restart your terminal.
    11. Type <code>bitly www.google.com</code>.

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