Skip to content

Commit

Permalink
handle t.co properly
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Aug 28, 2017
1 parent c07b4ba commit 7c2e413
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ describe('unshrtn', function() {
})
);

it('handles t.co', done =>
u('https://t.co/r2mIeyyY7t', function(err, long) {
assert.equal(long, 'http://www.newshub.co.nz/home/election/2017/08/patrick-gower-bill-english-in-shutdown-mode-over-todd-barclay-texts.html');
return done();
})
);

return it('handles canonical link', done =>
u('https://www.nytimes.com/2017/05/23/opinion/mitch-landrieu-new-orleans-mayor-speech.html?smprod=nytcore-iphone&smid=nytcore-iphone-share&_r=0', function(err, long) {
assert.equal(long, 'https://www.nytimes.com/2017/05/23/opinion/mitch-landrieu-new-orleans-mayor-speech.html');
return done();
})
);

});

11 changes: 6 additions & 5 deletions unshrtn.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let URL = require('url')
let jsdom = require('jsdom');
let level = require('level');
let stream = require('stream');
Expand Down Expand Up @@ -117,14 +118,14 @@ var unshorten = (short, next) => {
}
};


var userAgent = (url) => {

// most of the time we pretend to be a browser but fw.to doesn't give
// most of the time we pretend to be a browser but fw.to & t.co don't give
// browsers a Location header and instead rely on META refresh and JavaScript
// to redirect browsers (sigh)

if (url.match(/https?:\/\/fw\.to/)) {
// to redirect browsers (sigh) when they are told we are not a browser
// they give an HTTP redirect, which is nice

if (['t.co', 'fw.to'].indexOf(URL.parse(url).hostname) != -1) {
return "ushrtn (https://github.com/edsu/unshrtn)";
} else {
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
Expand Down

0 comments on commit 7c2e413

Please sign in to comment.