Guest User

UV Index Widget for Scriptable

a guest
Oct 15th, 2023
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const locationFile = FileManager.local().joinPath(FileManager.local().temporaryDirectory(), 'location.txt');
  2. let location = null;
  3.  
  4. // Attempt to retrieve current location
  5. try {
  6.   location = await Location.current();
  7. } catch (error) {
  8.   console.error('Error retrieving location:', error);
  9. }
  10.  
  11. // Fallback to stored location data if current location retrieval fails
  12. if (!location) {
  13.   try {
  14.     const storedLocationData = FileManager.local().readString(locationFile);
  15.     if (storedLocationData) {
  16.       location = JSON.parse(storedLocationData);
  17.       console.log('Using stored location data as a fallback:', location);
  18.     } else {
  19.       console.error('No location data available.');
  20.     }
  21.   } catch (error) {
  22.     console.error('Error reading stored location data:', error);
  23.   }
  24. }
  25.  
  26. // Update stored location data with the current location (if retrieved)
  27. if (location) {
  28.   FileManager.local().writeString(locationFile, JSON.stringify(location));
  29. }
  30.  
  31. if (location) {
  32.   const lat = location.latitude;
  33.   const lon = location.longitude;
  34.  
  35.   const uvIndexRequest = new Request(`https://api.openweathermap.org/data/3.0/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely,alerts&appid=REPLACEWITHAPIKEY`);
  36.   const uvIndexResponse = await uvIndexRequest.loadJSON();
  37.  
  38.   const currentUVIndex = uvIndexResponse.current.uvi.toFixed(1);
  39.   const todayMaxUVIndex = uvIndexResponse.daily[0].uvi.toFixed(1);
  40.   const tomorrowMaxUVIndex = uvIndexResponse.daily[1].uvi.toFixed(1);
  41.   const todayMaxUVIndexTime = new Date(uvIndexResponse.daily[0].dt * 1000).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
  42.   const tomorrowMaxUVIndexTime = new Date(uvIndexResponse.daily[1].dt * 1000).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
  43.  
  44.   // Create widget
  45.   let widget = new ListWidget();
  46.   widget.setPadding(8, 16, 16, 0);
  47.  
  48.   // Add title
  49.   let titleText = widget.addText('UV Index β˜€οΈ');
  50.   titleText.font = Font.boldSystemFont(16);
  51.   titleText.textColor = Color.white();
  52.  
  53.   widget.addSpacer(0);
  54.  
  55.   // Add current UV index
  56.   let currentUVIndexText = widget.addText(currentUVIndex);
  57.   currentUVIndexText.font = Font.systemFont(36);
  58.   currentUVIndexText.textColor = Color.white();
  59.  
  60.   widget.addSpacer(30);
  61.  
  62.   // Determine the current date and tomorrow's date
  63.   const now = new Date();
  64.   const today = now.toLocaleDateString('en-US', { day: 'numeric', month: 'long' });
  65.   const tomorrow = new Date(now);
  66.   tomorrow.setDate(tomorrow.getDate() + 1);
  67.   const tomorrowFormatted = tomorrow.toLocaleDateString('en-US', { day: 'numeric', month: 'long' });
  68.  
  69.   // Add maximum UV index for today or tomorrow
  70.   let maxUVIndexText;
  71.   let maxUVIndexTimeText;
  72.   if (now.getHours() >= 20) {
  73.     maxUVIndexText = widget.addText(`Tomorrow's Max: ${tomorrowMaxUVIndex}`);
  74.    maxUVIndexTimeText = widget.addText(`(around ${tomorrowMaxUVIndexTime})`);
  75.  } else {
  76.    maxUVIndexText = widget.addText(`Today's Max: ${todayMaxUVIndex}`);
  77.     maxUVIndexTimeText = widget.addText(`(around ${todayMaxUVIndexTime})`);
  78.   }
  79.   maxUVIndexText.font = Font.systemFont(14);
  80.   maxUVIndexText.textColor = Color.white();
  81.   maxUVIndexTimeText.font = Font.systemFont(12);
  82.   maxUVIndexTimeText.textColor = Color.white();
  83.  
  84.   // Set widget background color
  85.   widget.backgroundColor = new Color("#B2675E");
  86.  
  87.   // Present widget
  88.   if (config.runsInWidget) {
  89.     // Display widget in the widget area
  90.     Script.setWidget(widget);
  91.   } else {
  92.     // Display widget in the app
  93.     widget.presentMedium();
  94.   }
  95.  
  96.   Script.complete();
  97. } else {
  98.   console.error('Location data not available.');
  99. }
Advertisement
Add Comment
Please, Sign In to add comment