Advertisement
Goldenlion5648

calendarScript

Jan 23rd, 2022 (edited)
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Variables used by Scriptable.
  2. // These must be at the very top of the file. Do not edit.
  3. // icon-color: red; icon-glyph: home;
  4.  
  5. //-----------------------------
  6. const imageURLs = ["https://i.imgur.com/aZRwDxrh.jpg", "https://i.imgur.com/aZRwDxrh.jpg", "https://i.imgur.com/jWv0Bplh.jpg", "https://i.imgur.com/AbMWtweh.jpg", "https://i.imgur.com/7TMPe1kh.jpg", "https://i.imgur.com/MncTHNih.jpg", "https://i.imgur.com/AqGI7Qgh.jpg", "https://i.imgur.com/RKYoNPRh.jpg", "https://i.imgur.com/GsGDFk7h.jpg", "https://i.imgur.com/vNLJXDEh.jpg", "https://i.imgur.com/Fy1V4zCh.jpg", "https://i.imgur.com/JIWymggh.jpg", "https://i.imgur.com/nLGTBvqh.jpg", "https://i.imgur.com/tveWaQSh.jpg", "https://i.imgur.com/e1PwPwRh.jpg", "https://i.imgur.com/GcbZP2ih.jpg", "https://i.imgur.com/tW2V1KYh.jpg", "https://i.imgur.com/0zw4qwJh.jpg", "https://i.imgur.com/D4kEhkWh.jpg", "https://i.imgur.com/QE01Tceh.jpg", "https://i.imgur.com/KQ68C22h.jpg", "https://i.imgur.com/n5SpBcjh.jpg", "https://i.imgur.com/zNeu9xUh.jpg", "https://i.imgur.com/OpeCyWph.jpg", "https://i.imgur.com/7bFIyXah.jpg", "https://i.imgur.com/0qHyEHvh.jpg", "https://i.imgur.com/aPSfyS3h.jpg", "https://i.imgur.com/is6Eq8fh.jpg", "https://i.imgur.com/GGVKkfgh.jpg", "https://i.imgur.com/PtiHrHyh.jpg", "https://i.imgur.com/qVzpePoh.jpg", "https://i.imgur.com/mmqtknrh.jpg", "https://i.imgur.com/0QVDSOGh.jpg", "https://i.imgur.com/V3ybYkQh.jpg", "https://i.imgur.com/xQkiHBBh.jpg", "https://i.imgur.com/iti4l9Ih.jpg", "https://i.imgur.com/oQSaLcHh.jpg", "https://i.imgur.com/R0DVH3Hh.jpg", "https://i.imgur.com/SmxmVwUh.jpg", "https://i.imgur.com/O2SvqeNh.jpg", "https://i.imgur.com/Mjyg6WDh.jpg", "https://i.imgur.com/H4BXvFZh.jpg", "https://i.imgur.com/Ib8KcDCh.jpg", "https://i.imgur.com/ujB5wg1h.jpg"]
  7. //-----------------------------
  8.  
  9. let collection = "932809";
  10.  
  11. const FORCE_IMAGE_UPDATE = false
  12.  
  13. // var TEST_MODE = true;
  14.  
  15. // Store current datetime
  16. const date = new Date();
  17.  
  18. let widget = new ListWidget();
  19.  
  20. setDate(widget);
  21.  
  22. let widgetInputRAW = args.widgetParameter;
  23.  
  24. if (widgetInputRAW) {
  25.     try {
  26.         widgetInputRAW.toString();
  27.  
  28.         if (widgetInputRAW.toString() !== "") {
  29.             collection = widgetInputRAW.toString();
  30.         }
  31.     } catch (e) {
  32.         throw new Error("Please long press the widget and add a parameter.");
  33.     }
  34. }
  35.  
  36. // Add more minimal overlay
  37. let gradient = new LinearGradient();
  38. gradient.colors = [new Color("#000000", 0.5), new Color("#ffffff", 0)];
  39. gradient.locations = [0, 0.5];
  40. widget.backgroundGradient = gradient;
  41.  
  42. widget.addSpacer();
  43.  
  44.  
  45.  
  46. // Look for the image file
  47. await setBackground(widget);
  48.  
  49. setGreeting(widget);
  50. // widget.addImage()
  51.  
  52. // Finalize widget settings
  53. // widget.setPadding(16, 16, 16, 0);
  54. // widget.spacing = -3;
  55.  
  56. // Script.setWidget(widget);
  57. widget.presentLarge();
  58. Script.complete();
  59.  
  60. function setDate(widget) {
  61.     // Format the date info
  62.     let df = new DateFormatter();
  63.     df.dateFormat = "EEEE";
  64.     let weekday = widget.addText(df.string(date).toUpperCase());
  65.     let day = widget.addText(date.getDate().toString());
  66.     weekday.font = Font.semiboldSystemFont(20);
  67.     day.font = Font.lightSystemFont(34);
  68.     weekday.textColor = Color.white();
  69.     day.textColor = Color.white();
  70. }
  71. function choice(greetingArray) {
  72.     return greetingArray[Math.floor(Math.random() * greetingArray.length)];
  73. }
  74.  
  75. function setGreeting(widget) {
  76.     // Date Calculations
  77.     const month = date.getMonth();
  78.     // const hour = date.getHours();
  79.  
  80.     let holidaysByDate = {
  81.         // month,date: greeting
  82.         "1,1": "Happy " + date.getFullYear().toString() + "!",
  83.         "10,31": "Happy Halloween!",
  84.         "3,14": "Happy Pi Day!",
  85.         "12,25": "Merry Christmas!",
  86.         "11,30": "Happy Birthday!",
  87.     };
  88.  
  89.     let greetings = [
  90.         "The super genius solution!",
  91.         "Get your game on!",
  92.         "This is oofy",
  93.         "Hey yo what's up buddy?",
  94.  
  95.     ]
  96.  
  97.     let holidayKeyDate = (month + 1).toString() + "," + date.getDate().toString();
  98.  
  99.     // Support for multiple greetings per time period
  100.  
  101.  
  102.     // Overwrite all greetings if specific holiday
  103.     if (holidaysByDate[holidayKeyDate]) {
  104.         greeting = holidaysByDate[holidayKeyDate];
  105.     }
  106.     else {
  107.         // greeting = choice(greetings);
  108.         greeting = ''
  109.     }
  110.  
  111.     // Greeting label
  112.     let hello = widget.addText(greeting);
  113.     hello.font = Font.semiboldSystemFont(20);
  114.     hello.textColor = Color.white();
  115.     hello.shadowRadius = 4;
  116. }
  117. async function getImage() {
  118.     let files = FileManager.local();
  119.     const path = files.documentsDirectory() + "/calendar_widget.jpg";
  120.     const modificationDate = files.modificationDate(path);
  121.  
  122.  
  123.     // Download image if it doesn't exist, wasn't created today, or update is forced
  124.     if (!modificationDate ||
  125.         !sameDay(modificationDate, date) ||
  126.         FORCE_IMAGE_UPDATE
  127.     ) {
  128.         try {
  129.             let img = await provideImage(collection);
  130.             files.writeImage(path, img);
  131.             // widget.backgroundImage = img;
  132.             return (img);
  133.         } catch (e) {
  134.             console.log("caught here");
  135.             console.log(e);
  136.             // widget.backgroundImage = files.readImage(path);
  137.             return (files.readImage(path));
  138.         }
  139.     } else {
  140.         // widget.backgroundImage = files.readImage(path);
  141.         return (files.readImage(path));
  142.     }
  143. }
  144. async function setBackground(widget) {
  145.  
  146.  
  147.     let img2 = await getImage();
  148.     img2 = cropImage(img2, new Rect(0,0,200, 200))
  149.  
  150.     if (img2.size.width / 2 > img2.size.height ||
  151.         img2.size.height / 2 > img2.size.width ||
  152.         img2.height > 1000 || img2.width > 1000) {
  153.         temp = widget.addImage(img2);
  154.         // temp.centerAlignImage();
  155.         temp.applyFillingContentMode();
  156.         console.log("option 1")
  157.     }
  158.     else {
  159.         widget.backgroundImage = img2;
  160.         console.log("option 2")
  161.  
  162.     }
  163.  
  164. }
  165.  
  166. // Fetch a image from Unsplash by it's collection id
  167. async function provideImage() {
  168.     var chosen = choice(imageURLs);
  169.     console.log(chosen);
  170.     const img = await downloadImage(
  171.         // "https://source.unsplash.com/collection/" + id
  172.         chosen
  173.     );
  174.  
  175.     return img;
  176. }
  177.  
  178. // Helper function to download images
  179. async function downloadImage(url) {
  180.     const req = new Request(url);
  181.     return await req.loadImage();
  182. }
  183.  
  184. // Crop an image into a rect
  185. function cropImage(img, rect) {
  186.     let draw = new DrawContext();
  187.     draw.respectScreenScale = true;
  188.  
  189.     draw.drawImageInRect(img, rect);
  190.     return draw.getImage();
  191. }
  192.  
  193. // Formats the times under each event
  194. function formatTime(date) {
  195.     let df = new DateFormatter();
  196.     df.useNoDateStyle();
  197.     df.useShortTimeStyle();
  198.     return df.string(date);
  199. }
  200.  
  201. // Determines if two dates occur on the same day
  202. function sameDay(d1, d2) {
  203.     return (
  204.         d1.getFullYear() === d2.getFullYear() &&
  205.         d1.getMonth() === d2.getMonth() &&
  206.         d1.getDate() === d2.getDate()
  207.     );
  208. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement