Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Variables used by Scriptable.
- // These must be at the very top of the file. Do not edit.
- // icon-color: red; icon-glyph: home;
- //-----------------------------
- 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"]
- //-----------------------------
- let collection = "932809";
- const FORCE_IMAGE_UPDATE = false
- // var TEST_MODE = true;
- // Store current datetime
- const date = new Date();
- let widget = new ListWidget();
- setDate(widget);
- let widgetInputRAW = args.widgetParameter;
- if (widgetInputRAW) {
- try {
- widgetInputRAW.toString();
- if (widgetInputRAW.toString() !== "") {
- collection = widgetInputRAW.toString();
- }
- } catch (e) {
- throw new Error("Please long press the widget and add a parameter.");
- }
- }
- // Add more minimal overlay
- let gradient = new LinearGradient();
- gradient.colors = [new Color("#000000", 0.5), new Color("#ffffff", 0)];
- gradient.locations = [0, 0.5];
- widget.backgroundGradient = gradient;
- widget.addSpacer();
- // Look for the image file
- await setBackground(widget);
- setGreeting(widget);
- // widget.addImage()
- // Finalize widget settings
- // widget.setPadding(16, 16, 16, 0);
- // widget.spacing = -3;
- // Script.setWidget(widget);
- widget.presentLarge();
- Script.complete();
- function setDate(widget) {
- // Format the date info
- let df = new DateFormatter();
- df.dateFormat = "EEEE";
- let weekday = widget.addText(df.string(date).toUpperCase());
- let day = widget.addText(date.getDate().toString());
- weekday.font = Font.semiboldSystemFont(20);
- day.font = Font.lightSystemFont(34);
- weekday.textColor = Color.white();
- day.textColor = Color.white();
- }
- function choice(greetingArray) {
- return greetingArray[Math.floor(Math.random() * greetingArray.length)];
- }
- function setGreeting(widget) {
- // Date Calculations
- const month = date.getMonth();
- // const hour = date.getHours();
- let holidaysByDate = {
- // month,date: greeting
- "1,1": "Happy " + date.getFullYear().toString() + "!",
- "10,31": "Happy Halloween!",
- "3,14": "Happy Pi Day!",
- "12,25": "Merry Christmas!",
- "11,30": "Happy Birthday!",
- };
- let greetings = [
- "The super genius solution!",
- "Get your game on!",
- "This is oofy",
- "Hey yo what's up buddy?",
- ]
- let holidayKeyDate = (month + 1).toString() + "," + date.getDate().toString();
- // Support for multiple greetings per time period
- // Overwrite all greetings if specific holiday
- if (holidaysByDate[holidayKeyDate]) {
- greeting = holidaysByDate[holidayKeyDate];
- }
- else {
- // greeting = choice(greetings);
- greeting = ''
- }
- // Greeting label
- let hello = widget.addText(greeting);
- hello.font = Font.semiboldSystemFont(20);
- hello.textColor = Color.white();
- hello.shadowRadius = 4;
- }
- async function getImage() {
- let files = FileManager.local();
- const path = files.documentsDirectory() + "/calendar_widget.jpg";
- const modificationDate = files.modificationDate(path);
- // Download image if it doesn't exist, wasn't created today, or update is forced
- if (!modificationDate ||
- !sameDay(modificationDate, date) ||
- FORCE_IMAGE_UPDATE
- ) {
- try {
- let img = await provideImage(collection);
- files.writeImage(path, img);
- // widget.backgroundImage = img;
- return (img);
- } catch (e) {
- console.log("caught here");
- console.log(e);
- // widget.backgroundImage = files.readImage(path);
- return (files.readImage(path));
- }
- } else {
- // widget.backgroundImage = files.readImage(path);
- return (files.readImage(path));
- }
- }
- async function setBackground(widget) {
- let img2 = await getImage();
- img2 = cropImage(img2, new Rect(0,0,200, 200))
- if (img2.size.width / 2 > img2.size.height ||
- img2.size.height / 2 > img2.size.width ||
- img2.height > 1000 || img2.width > 1000) {
- temp = widget.addImage(img2);
- // temp.centerAlignImage();
- temp.applyFillingContentMode();
- console.log("option 1")
- }
- else {
- widget.backgroundImage = img2;
- console.log("option 2")
- }
- }
- // Fetch a image from Unsplash by it's collection id
- async function provideImage() {
- var chosen = choice(imageURLs);
- console.log(chosen);
- const img = await downloadImage(
- // "https://source.unsplash.com/collection/" + id
- chosen
- );
- return img;
- }
- // Helper function to download images
- async function downloadImage(url) {
- const req = new Request(url);
- return await req.loadImage();
- }
- // Crop an image into a rect
- function cropImage(img, rect) {
- let draw = new DrawContext();
- draw.respectScreenScale = true;
- draw.drawImageInRect(img, rect);
- return draw.getImage();
- }
- // Formats the times under each event
- function formatTime(date) {
- let df = new DateFormatter();
- df.useNoDateStyle();
- df.useShortTimeStyle();
- return df.string(date);
- }
- // Determines if two dates occur on the same day
- function sameDay(d1, d2) {
- return (
- d1.getFullYear() === d2.getFullYear() &&
- d1.getMonth() === d2.getMonth() &&
- d1.getDate() === d2.getDate()
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement