forked from ariya/phantomjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
37 lines (34 loc) · 1.17 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var page = require('webpage').create(),
system = require('system'),
city,
url;
city = 'Mountain View, California'; // default
if (system.args.length > 1) {
city = Array.prototype.slice.call(system.args, 1).join(' ');
}
url = encodeURI('http://api.openweathermap.org/data/2.1/find/name?q=' + city);
console.log('Checking weather condition for', city, '...');
page.open(url, function(status) {
var result, data;
if (status !== 'success') {
console.log('Error: Unable to access network!');
} else {
result = page.evaluate(function () {
return document.body.innerText;
});
try {
data = JSON.parse(result);
data = data.list[0];
console.log('');
console.log('City:', data.name);
console.log('Condition:', data.weather.map(function(entry) {
return entry.main;
}).join(', '));
console.log('Temperature:', Math.round(data.main.temp - 273.15), 'C');
console.log('Humidity:', Math.round(data.main.humidity), '%');
} catch (e) {
console.log('Error:', e.toString());
}
}
phantom.exit();
});