Last active
October 12, 2021 22:23
-
-
Save sharn25/4b10c7170c7019501e42773fd5d02f8b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: deep-blue; icon-glyph: magic; | |
// Create your ID on openweathermap.org | |
// Get your api from there and set that in API_WEATHER | |
// To get CITY_WEATHER means city ID get the longitude and latitute from google maps for your location | |
// Replace the longitude, latitute and YOUR_API_KEY in the given link http://api.openweathermap.org/data/2.5/weather?lat=Latitude&lon=Longitude&appid=YOUR_API_KEY&units=metric and look for your city ID in the resultant text. | |
// Set that ID to CITY_WEATHER | |
//API_KEY | |
let API_WEATHER = "YOUR_API_HERE";//Load Your api here | |
let CITY_WEATHER = "YOUR_CITY_ID_HERE";//add your city ID | |
//create Data | |
var today = new Date(); | |
//Initlize Widget | |
let widget = new ListWidget(); | |
//Get storage | |
var base_path = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/weather/"; | |
var fm = FileManager.iCloud(); | |
//color | |
var textcolor = new Color("#ffffff"); | |
// Fetch Image from Url | |
async function fetchimageurl(url) { | |
const request = new Request(url) | |
var res = await request.loadImage(); | |
return res; | |
} | |
// Get formatted Date | |
function getformatteddate(){ | |
var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; | |
return months[today.getMonth()] + " " + today.getDate() | |
} | |
// Load image from local drive | |
async function fetchimagelocal(path){ | |
var finalPath = base_path + path + ".png"; | |
if(fm.fileExists(finalPath)==true){ | |
console.log("file exists: " + finalPath); | |
return finalPath; | |
}else{ | |
//throw new Error("Error file not found: " + path); | |
if(fm.fileExists(base_path)==false){ | |
console.log("Directry not exist creating one."); | |
fm.createDirectory(base_path); | |
} | |
console.log("Downloading file: " + finalPath); | |
await downloadimg(path); | |
if(fm.fileExists(finalPath)==true){ | |
console.log("file exists after download: " + finalPath); | |
return finalPath; | |
}else{ | |
throw new Error("Error file not found: " + path); | |
} | |
} | |
} | |
async function downloadimg(path){ | |
const url = "https://github.com/sharn25/weather_ico_frog/raw/main/weathers25_2.json"; | |
const data = await fetchWeatherData(url); | |
var dataimg = null; | |
var name = null; | |
if(path.includes("bg")){ | |
dataimg = data.background; | |
name = path.replace("_bg",""); | |
}else{ | |
dataimg = data.icon; | |
name = path.replace("_ico",""); | |
} | |
var imgurl=null; | |
switch (name){ | |
case "01d": | |
imgurl = dataimg._01d; | |
break; | |
case "01n": | |
imgurl = dataimg._01n; | |
break; | |
case "02d": | |
imgurl = dataimg._02d; | |
break; | |
case "02n": | |
imgurl = dataimg._02n; | |
break; | |
case "03d": | |
imgurl = dataimg._03d; | |
break; | |
case "03n": | |
imgurl = dataimg._03n; | |
break; | |
case "04d": | |
imgurl = dataimg._04d; | |
break; | |
case "04n": | |
imgurl = dataimg._04n; | |
break; | |
case "09d": | |
imgurl = dataimg._09d; | |
break; | |
case "09n": | |
imgurl = dataimg._09n; | |
break; | |
case "10d": | |
imgurl = dataimg._10d; | |
break; | |
case "10n": | |
imgurl = dataimg._10n; | |
break; | |
case "11d": | |
imgurl = dataimg._11d; | |
break; | |
case "11n": | |
imgurl = dataimg._11n; | |
break; | |
case "13d": | |
imgurl = dataimg._13d; | |
break; | |
case "13n": | |
imgurl = dataimg._13n; | |
break; | |
case "50d": | |
imgurl = dataimg._50d; | |
break; | |
case "50n": | |
imgurl = dataimg._50n; | |
break; | |
} | |
const image = await fetchimageurl(imgurl); | |
console.log("Downloaded Image"); | |
fm.writeImage(base_path+path+".png",image); | |
} | |
//get Json weather | |
async function fetchWeatherData(url) { | |
const request = new Request(url); | |
const res = await request.loadJSON(); | |
return res; | |
} | |
//start Programming | |
// Get Location | |
/*Location.setAccuracyToBest(); | |
let curLocation = await Location.current(); | |
console.log(curLocation.latitude); | |
console.log(curLocation.longitude);*/ | |
let wetherurl = "http://api.openweathermap.org/data/2.5/weather?id=" + CITY_WEATHER + "&APPID=" + API_WEATHER + "&units=metric"; | |
//"http://api.openweathermap.org/data/2.5/weather?lat=" + curLocation.latitude + "&lon=" + curLocation.longitude + "&appid=" + API_WEATHER + "&units=metric"; | |
//"http://api.openweathermap.org/data/2.5/weather?id=" + CITY_WEATHER + "&APPID=" + API_WEATHER + "&units=metric" | |
const weatherJSON = await fetchWeatherData(wetherurl); | |
const cityName = weatherJSON.name; | |
const weatherarry = weatherJSON.weather; | |
const iconData = weatherarry[0].icon; | |
const weathername = weatherarry[0].main; | |
const curTempObj = weatherJSON.main; | |
const curTemp = curTempObj.temp; | |
const highTemp = curTempObj.temp_max; | |
const lowTemp = curTempObj.temp_min; | |
const feel_like = curTempObj.feels_like; | |
//Completed loading weather data | |
// Widget Background Image | |
widget.backgroundImage = Image.fromFile(await fetchimagelocal(iconData+"_bg")); | |
//Start Spacing | |
widget.addSpacer(0); | |
//Widget weather Image | |
var img = Image.fromFile(await fetchimagelocal(iconData+"_ico")); | |
var widgetimg = widget.addImage(img); | |
widgetimg.imageSize = new Size(74,74); | |
widgetimg.rightAlignImage(); | |
// Widget Date | |
var dateText = widget.addText(getformatteddate()); | |
dateText.textColor = textcolor; | |
dateText.font = Font.regularSystemFont(15); | |
// Widget Weather Temp | |
var tempText = widget.addText(Math.round(curTemp)+"\u2103"); | |
tempText.textColor = textcolor; | |
tempText.font = Font.boldSystemFont(35); | |
// Widget feel temp | |
let feel = "Feels like " + Math.round(feel_like) + "\u2103";//"H:"+highTemp+"\u2103"+" L:"+lowTemp+"\u2103" | |
var hltempText = widget.addText(feel); | |
hltempText.textColor = textcolor; | |
hltempText.font = Font.regularSystemFont(15); | |
// Widget city Name | |
/* | |
var citynameText = widget.addText(cityName); | |
citynameText.textColor = textcolor; | |
citynameText.font = Font.regularSystemFont(10); | |
*/ | |
// Bottom Spacer | |
widget.addSpacer(); | |
//widget.setPadding(5, 15, 0, 15) | |
Script.setWidget(widget); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Thanks for great widget. Is there a possibility for me to change the text to alignment on top, not on bottom?

Like it is now its interfering with most pictures and barely readable.