Created
November 20, 2020 09:00
-
-
Save Leibinger015/b461a3f24bf25a952a8c88076f629b49 to your computer and use it in GitHub Desktop.
This is a Scriptable Widget Script for iOS. A World Air Quality Index Icon for your Homescreen.
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
// Das Skript habe ich auf Deutsch lokalisiert und es stammt aus der Quelle -> https://gist.github.com/SuperC1r0/a8aef6ec73108d03862aa96f278a72d7 | |
// | |
// | |
// | |
// ***Air Quality Widget*** | |
// | |
// Copyright (C) 2020 by SuperC1r0 | |
// | |
// Permission to use, copy, modify, and/or distribute this software is hereby granted. | |
// However you have to respect the Terms of Service of the Air Quality Open Data Platform: https://aqicn.org/api/tos/ | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | |
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
// OF THIS SOFTWARE. | |
// | |
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: deep-blue; icon-glyph: magic; | |
//** Script for scriptable to get the current Air Polution of your city | |
//Thank you World Air Quality Index Project for gathering all the data! | |
function createWidget(keyword,aqi,date){ | |
let widget = new ListWidget() | |
let title = widget.addText([keyword] + "'s Luftqualität") | |
title.font = Font.boldSystemFont(16) | |
title.textColor = Color.white() | |
title.centerAlignText() | |
title.minimumScaleFactor = 0.4 | |
title.lineLimit = 2 | |
widget.addSpacer() | |
let aqiTitle = widget.addText("AQI-Index") | |
aqiTitle.font = Font.regularSystemFont(10) | |
aqiTitle.textColor = Color.black() | |
aqiTitle.centerAlignText() | |
aqiTitle.minimumScaleFactor = 0.8 | |
//Air Quality Levels | |
let aqiText = widget.addText(aqi) | |
aqiText.font = Font.boldSystemFont(30) | |
aqiText.centerAlignText() | |
aqiText.minimumScaleFactor = 1 | |
if (aqi >= 300) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#7e0023") | |
let aqiText1 = widget.addText("GEFÄHRLICH:👎🛑😷") | |
aqiText1.font = Font.boldSystemFont(10) | |
aqiText1.textColor = Color.white() | |
aqiText1.centerAlignText() | |
aqiText1.minimumScaleFactor = 1 | |
} else if (aqi >= 201) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#660099") | |
let aqiText1 = widget.addText("SEHR UNGESUND:✋🛑") | |
aqiText1.font = Font.boldSystemFont(9) | |
aqiText1.textColor = Color.white() | |
aqiText1.centerAlignText() | |
aqiText1.minimumScaleFactor = 0.7 | |
} else if (aqi >= 151) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#cc0033") | |
let aqiText1 = widget.addText("UNGESUND:👎") | |
aqiText1.font = Font.boldSystemFont(10) | |
aqiText1.textColor = Color.white() | |
aqiText1.centerAlignText() | |
aqiText1.minimumScaleFactor = 0.8 | |
} else if (aqi >= 101) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#ff9933") | |
let aqiText1 = widget.addText("BEDENKLICH:✋") | |
aqiText1.font = Font.boldSystemFont(10) | |
aqiText1.textColor = Color.black() | |
aqiText1.centerAlignText() | |
aqiText1.minimumScaleFactor = 0.7 | |
} else if (aqi >= 51) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#66b266") | |
let aqiText1 = widget.addText("MÄSSIG:☝️") | |
aqiText1.font = Font.boldSystemFont(10) | |
aqiText1.textColor = Color.black() | |
aqiText1.centerAlignText() | |
aqiText1.minimumScaleFactor = 0.7 | |
} else if (aqi >=0) { | |
aqiText.textColor = Color.white() | |
widget.backgroundColor = new Color("#006600") | |
let aqiText1 = widget.addText("GUT 👍") | |
aqiText1.font = Font.boldSystemFont(12) | |
aqiText1.textColor = Color.white() | |
aqiText1.centerAlignText() | |
} | |
else { | |
aqiText.textColor = Color.red() | |
widget.backgroundColor = Color.white() | |
} | |
widget.addSpacer() | |
//Last Update of Data | |
let dateText = widget.addText(date + " (Messzeit)") | |
dateText.font = Font.regularSystemFont(7) | |
dateText.centerAlignText() | |
dateText.minimumScaleFactor = 0.2 | |
widget.addSpacer() | |
//Contribution to World Air Quality Project | |
let Text = widget.addText("A World Air Quality Project") | |
Text.font = Font.regularSystemFont(6) | |
Text.textColor = Color.black() | |
Text.centerAlignText() | |
Text.minimumScaleFactor = 0.2 | |
return widget | |
} | |
//Get City from Air Quality Open Data Platform | |
async function getData(keyword) { | |
let req = new Request(`https://api.waqi.info/search/?keyword=${keyword}&token=19b71483eb9ddd3831a5e83312cd860fb00fa03d`) | |
req.method = "GET" | |
let response = await req.loadJSON() | |
if (Object.keys(response.data).length != 0) { | |
var result = '{"aqi" : "'+response.data[0].aqi+'", "date" : "'+response.data[0].time.stime+'"}' | |
} else { | |
var result = '{"aqi" : "N/A", "date" : "k. A."}' | |
} | |
return result | |
} | |
//Search Keyword in Air Quality Open Database | |
if (config.runsInApp) { | |
// Demo in-App visit https://aqicn.org/ for more cities | |
let keyword = "Berlin" | |
let data = await getData(keyword) | |
data = JSON.parse(data) | |
let widget = createWidget(keyword, data.aqi, data.date) | |
widget.presentSmall() | |
} else { | |
// Not in-App - use of fixed Parameter | |
let keyword = args.widgetParameter | |
let data = await getData(keyword) | |
data = JSON.parse(data) | |
let widget = createWidget(keyword, data.aqi, data.date) | |
Script.setWidget(widget) | |
} | |
Script.complete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wirklich cool.. 👍