Advertisement
Duckslaps

Home Screen weather widget code

Feb 4th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function createWidget() {
  2.     let listwidget = new ListWidget();
  3.    
  4.     let weatherData;
  5.     const apiKey = "82c29fdbgd6aebbb595d402f8a65fabf";
  6.     var cityID = "4998830";
  7.    
  8.     let fileManager = FileManager.iCloud();
  9.     let bgPath = fileManager.documentsDirectory() + "/Widgetbg.PNG"; //Replace with your background crop  
  10.     } if (background.type == "image") {
  11.       const extension = (this.darkMode && background.dark && !this.settings.widget.instantDark ? " (Dark)" : "") + ".jpg"
  12.       const imagePath = this.fm.joinPath(this.fm.joinPath(this.fm.documentsDirectory(), "Weather Cal"), name + extension)
  13.  
  14.       if (this.fm.fileExists(imagePath)) {
  15.         if (this.fm.isFileStoredIniCloud(imagePath)) { await this.fm.downloadFileFromiCloud(imagePath) }
  16.         this.widget.backgroundImage = this.fm.readImage(imagePath)
  17.  
  18.       } else if (config.runsInWidget) {
  19.         this.widget.backgroundColor = Color.gray()
  20.  
  21.     const months = ["Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"];
  22.  
  23.     const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  24.  
  25.     const d = new Date();
  26.    
  27.     let month = months[d.getMonth()];
  28.     let day = days[d.getDay()];
  29.     let numDay = d.getDate();
  30.     let hour = d.getHours();
  31.    
  32.   try {
  33.   weatherData = await new Request("https://api.openweathermap.org/data/2.5/weather?id="+ cityID +"&appid=" + apiKey).loadJSON();
  34. console.log(weatherData);
  35. }catch(e){
  36.   console.log("nothing");
  37. }
  38.  
  39. var farenheit = Math.round(((parseFloat(weatherData.main.temp)-273.15)*1.8)+32);;
  40.    
  41.     function dateOrdinal(int) {
  42.     if (int == 31 || int == 21 || int == 1) return int + "st";
  43.     else if (int == 22 || int == 2) return int + "nd";
  44.     else if (int == 23 || int == 3) return int + "rd";
  45.     else return int + "th";
  46. };
  47.  
  48.     function getTime() {
  49.     if (hour < 12) return "Morning Blake";
  50.     else if (hour < 18) return "Afternoon Blake";
  51.     else return "Evening Blake";
  52.   };
  53. //
  54. // console.log(getTime());
  55.  
  56. function weatherMessage() {
  57.   if (farenheit <= 40) return "Grab a scarf, Its freezing";
  58.   else if (farenheit <= 50) return "Might want a coat";
  59.   else if (farenheit <= 60) return "Not too bad outside";
  60.   else if (celcius <= 68) return "Getting a bit warm outside";
  61.   else if (celcius <= 75) return "Summertime sucks, drink water";
  62.   else return "Nope, way too hot out there";
  63. }
  64.  
  65.  
  66. await fileManager.downloadFileFromiCloud(bgPath);
  67.  
  68. listwidget.backgroundImage = Image.fromFile(bgPath);
  69.  
  70. let heading = listwidget.addText("Good " + getTime());
  71.   heading.leftAlignText();
  72.   heading.font = Font.lightSystemFont(18);
  73.   heading.textColor = new Color("#000");
  74.  
  75.   listwidget.addSpacer(5);
  76.  
  77.   let dateText = listwidget.addText("It's " + day);
  78.   dateText.leftAlignText();
  79.   dateText.font = Font.regularRoundedSystemFont(30);
  80.   dateText.textColor = new Color("#000");
  81.  
  82.     let dateMonth = listwidget.addText(month + " the " + dateOrdinal(numDay));
  83.   dateMonth.leftAlignText();
  84.   dateMonth.font = Font.regularRoundedSystemFont(30);
  85.   dateMonth.textColor = new Color("#000");
  86.   listwidget.addSpacer(5);
  87.  
  88.   let weatherText = listwidget.addText("It's " + farenheit + "°F with "+ weatherData.weather[0].description + ".");
  89.   weatherText.leftAlignText();
  90.   weatherText.font = Font.lightSystemFont(12)
  91.   weatherText.textColor = new Color("#000");
  92.  
  93.   let weatherTexttwo = listwidget.addText(weatherMessage());
  94.   weatherTexttwo.leftAlignText();
  95.   weatherTexttwo.font = Font.lightSystemFont(12)
  96.   weatherTexttwo.textColor = new Color("#000");
  97.  
  98.   listwidget.addSpacer(80);
  99.  
  100.   return listwidget;
  101. }
  102.  
  103. let widget = await createWidget();
  104.  
  105. if (config.runsInWidget) {
  106.   Script.setWidget(widget);
  107. } else {(str)
  108.   widget.presentLarge();
  109. }    
  110.   Script.complete();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement