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