Created
October 30, 2015 16:34
-
-
Save japanetfutan/4299bfe7f56bf40d01ed to your computer and use it in GitHub Desktop.
This file contains 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
var UI = require('ui'); | |
var ajax = require('ajax'); | |
var myAPIKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
// 初期表示 | |
var Card = new UI.Card({ | |
title: "Please Wait", | |
body: "Downloading..." | |
}); | |
Card.fullscreen(true); //WatchFaceにメニューバーは不要なのでフルスクリーンにする | |
Card.show(); | |
// Construct URL | |
var cityName = 'Osaka'; | |
var URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&appid=' + myAPIKey; | |
// Make the request | |
ajax( | |
{ | |
url: URL, | |
type: 'json' | |
}, | |
function(data) { | |
// Success! | |
console.log('Successfully fetched weather data!'); | |
// Extract data | |
var location = data.name; | |
var temperature = Math.round(data.main.temp - 273.15) + 'C'; | |
// Always upper-case first letter of description | |
var description = data.weather[0].description; | |
description = description.charAt(0).toUpperCase() + description.substring(1); | |
// Show to user | |
Card.title(''); | |
Card.subtitle(location + ', ' + temperature); | |
Card.body(description); | |
}, | |
function(error) { | |
// Failure! | |
console.log('Failed fetching weather data: ' + error); | |
} | |
); | |
function padZero(s){ | |
return s < 10?'0'+s:s; | |
} | |
function itsTime(){ | |
var date = new Date(); | |
var y = date.getFullYear(); | |
var m = date.getMonth()+1; | |
var d = date.getDate(); | |
var hh = date.getHours(); | |
var mm = date.getMinutes(); | |
var ss = date.getSeconds(); | |
m = padZero(m); | |
d = padZero(d); | |
hh = padZero(hh); | |
mm = padZero(mm); | |
ss = padZero(ss); | |
return y + '/' + m + '/' + d + ' ' + hh + ':' + mm + ':' + ss; | |
} | |
setInterval(function(){ | |
Card.title(itsTime()); | |
}, 1000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment