Created
June 6, 2012 16:04
-
-
Save liquidz/2882910 to your computer and use it in GitHub Desktop.
Notify me today's weather every day the first time I unlock my phone after 5 AM
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
// Initializing variables | |
var dayBaseTime = 5; // (day - 1) if before 5 AM | |
var weatherOption = { | |
location: 'Tokyo' | |
, locationtype: 'city' | |
, days: 0 | |
}; | |
// End of variables initializing | |
var trim = function(str){ | |
return str.replace(/(^\s*|\s*$)/, ''); | |
}; | |
var isNull = function(x){ | |
return (x === null || x === undefined); | |
}; | |
var getToday = function(date){ | |
var today = new Date() | |
, year = today.getFullYear() | |
, month = today.getMonth() + 1 | |
, day = today.getDate() | |
, hour = today.getHours() | |
; | |
if(hour < dayBaseTime){ --day; } | |
return('' + year + month + day); | |
}; | |
console.log("Started script: Notify me today's weather every day the first time I unlock my phone before " + dayBaseTime + "AM"); | |
device.screen.on('unlock', function(){ | |
var lastUnlocked = device.localStorage.getItem('lastUnlocked'); | |
var today = getToday(); | |
// check last unlocked date | |
if(isNull(lastUnlocked) || lastUnlocked !== today){ | |
// show weather notification | |
feeds.weather.get(weatherOption, | |
function(weather, status, response){ | |
var now = weather.now | |
, alerts = trim('' + now.alerts) | |
; | |
var notify = device.notifications.createNotification("Today's Weather"); | |
notify.content = now.sky+ '(rain: ' + now.rain + ')'; | |
if(!isNull(alerts) && alerts !== ''){ | |
notify.content += ' !! ' + alerts + ' !!'; | |
} | |
notify.on('click', function(){ | |
device.browser.launch(now.forecastUrl); | |
}); | |
notify.show(); | |
}, function(response, status){ | |
console.error('Weather error from script'); | |
}); | |
// set last unlocked date | |
device.localStorage.setItem('lastUnlocked', today); | |
} | |
}); | |
console.log("Completed script: Notify me today's weather every day the first time I unlock my phone before " + dayBaseTime + "AM"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this possible in IOS?