Created
February 21, 2016 13:30
-
-
Save houkanshan/8232dfdf3daa1e44db83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env /usr/local/bin/node | |
var http = require('http') | |
var icon = '♒︎' | |
function get(url) { | |
return new Promise((resolve, reject) => { | |
return http.get(url, res => { | |
var body = '' | |
res.on('data', chunk => { | |
body += chunk | |
}) | |
res.on('end', _ => { | |
resolve(body) | |
}) | |
}).on('error', reject) | |
}) | |
} | |
get('http://aqicn.org/city/beijing/us-embassy/') | |
.then(html => { | |
var ma = html.match(/Beijing Air Quality Index: (\d+)\b/) | |
if (ma) { | |
console.log(`${icon} ${ma[1]}`) | |
console.log('---') | |
console.log('Refresh | refresh=true') | |
} else { | |
console.log(icon) | |
console.log('---') | |
console.log('Can`t parse') | |
} | |
}) | |
.catch(e => { | |
console.log(icon) | |
console.log('---') | |
console.log(e.message) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment