Skip to content

Instantly share code, notes, and snippets.

@zlargon
Created April 26, 2016 03:03
Show Gist options
  • Save zlargon/bde6bfd1c4454a5ccddb73f44f3db766 to your computer and use it in GitHub Desktop.
Save zlargon/bde6bfd1c4454a5ccddb73f44f3db766 to your computer and use it in GitHub Desktop.
fetch short url of "goo.gl"
#!/usr/bin/env node
'use strict';
const fetch = require('node-fetch');
const exec = require('child_process').execSync;
const ptt_url = 'https://www.ptt.cc/bbs/NBA/M.1461627008.A.3D4.html';
fetch(ptt_url)
.then(res => {
if (res.status !== 200) {
throw new Error(`request to ${ptt_url} failed, status code = ${res.status} (${res.statusText})`);
}
return res.text();
})
.then(html => {
let map = {};
html.match(/http[s]?:\/\/goo\.gl\/\w+/ig)
.forEach(url => {
// filter reproduced url
map[url] = true;
});
return Object.keys(map);
})
.then(list => {
list.forEach(url => {
// exec('open ' + url);
console.log(url);
});
})
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment