Created
October 22, 2011 14:10
-
-
Save muddydixon/1306044 to your computer and use it in GitHub Desktop.
Node t.co expander
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** | |
* TCO expander | |
* tweet中のt.coが展開できない、って言われて作ってから試して見てたら、 | |
* tweetオブジェクトの中にtweet.entities.urlsってあって、そんなかに入ってたのでいらなかった。 | |
* なんかの役に立つかもと思い残しておくことにした。 | |
*****/ | |
/***** | |
* USAGE: | |
* var tco = require('./tco.js'); | |
* tco.expand('http://t.co/RYYZuPvw', function(err, url){console.log(url)}); | |
* | |
*****/ | |
var http = require('http') | |
, url = require('url'); | |
var TCO = {}; | |
TCO.expand = function(tco, cb){ | |
var parsedUrl = url.parse(tco), | |
err; | |
if(parsedUrl.host !== 't.co'){ | |
return cb(new Error('url not t.co'), null); | |
} | |
try{ | |
http.get({ | |
host: parsedUrl.host | |
, port: parsedUrl.protocol === 'https:' ? 443 : 80 | |
, path: parsedUrl.pathname | |
}, function(res){ | |
cb(null, res.headers.location); | |
}); | |
} catch (x) { | |
cb(x, null); | |
} | |
}; | |
if(module.parent){ | |
module.exports = TCO; | |
}else{ | |
var tco; | |
if(process.argv && (tco = process.argv[2])){ | |
TCO.expand(tco, function(err, url){ return url}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment