Skip to content

Instantly share code, notes, and snippets.

@kramanathan01
Last active October 5, 2018 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kramanathan01/3adef9e25218c732de53d14e52367af4 to your computer and use it in GitHub Desktop.
Save kramanathan01/3adef9e25218c732de53d14e52367af4 to your computer and use it in GitHub Desktop.
Scriptable JS for displaying 7-day forecast from DarkSky
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: magic;
let place = await Location.current() // Use Current Location
let json = await getWeather(place)
// Deserialize
let currentTemp = json.currently.apparentTemperature
let summary = json.minutely.summary
let currentMessage = "It's currently " + currentTemp + " degrees. " + summary
let dailies = json.daily.data
let table = new UITable()
// Add one row per day by iterating over the array of daily data
dailies.forEach(function (day, index) {
let row = new UITableRow()
if (index === 0) {
var date = "Today"
} else if (index === 1) {
var date = "Tomorrow"
} else {
var date = localDay(day.time)
}
let max = Math.round(day.apparentTemperatureMax).toString()
let min = Math.round(day.apparentTemperatureMin).toString()
let probability = Math.round(day.precipProbability * 100)
let icon = day.icon
// Table layout and data binding
let dateCell = row.addText(date)
let tempCell = row.addText(max + " | " + min)
let iconCell = row.addImage(iconImage(icon))
if (probability > 0) {
iconCell = row.addText(probability.toString() + "%")
} else {
iconCell = row.addText(" ")
}
dateCell.widthWeight = 30
tempCell.widthWeight = 15
iconCell.widthWeight = 20
row.height = 60
row.cellSpacing = 5
table.addRow(row)
})
// Display or Speak results
if (config.runsWithSiri) {
QuickLook.present(table)
Speech.speak(currentMessage)
} else {
QuickLook.present(table)
}
async function getWeather(place) {
let keys = devKey()
const site = "https://api.darksky.net/forecast/"
let url = site + keys + "/" + place.latitude + "," + place.longitude
let req = new Request(url)
let json = await req.loadJSON()
return json
}
function devKey() {
let fileManager = FileManager.iCloud()
let dir = fileManager.documentsDirectory()
let file = fileManager.joinPath(dir, "DarkSky_Key.txt")
return fileManager.readString(file)
}
function localDay(unixTime) {
const weekday = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
let d = new Date(0); // Date(0) sets d to Unix Epoch
d.setUTCSeconds(unixTime)
return weekday[d.getDay()]
}
// Hat Tip to Adam Whitcroft of Climacons for providing svg versions of the PNG files
// http://adamwhitcroft.com/climacons/
function iconImage(icon) {
let fileManager = FileManager.iCloud()
let dir = fileManager.documentsDirectory()
let path = "Icons/" + icon + ".png"
let file = fileManager.joinPath(dir, path)
return fileManager.readImage(file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment