Forked from juuliaans/proxferiado-scriptable-widget.js
Created
October 5, 2020 08:04
-
-
Save brainno722/24a6f1c0df7f42bfa38166c35de3d153 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
const getURL = year => `https://nolaborables.com.ar/api/v2/feriados/${year}` | |
const months = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']; | |
const days = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado']; | |
const dayOfWeek = (day, month, year) => days[new Date(year, month, day).getDay()]; | |
const getYear = () => (new Date()).getFullYear(); | |
const getNextHoliday = function(holidays){ | |
const now = new Date() | |
const today = { | |
day: now.getDate(), | |
month: now.getMonth() + 1 | |
}; | |
let holiday = holidays.find(h => | |
h.mes === today.month && h.dia > today.day || h.mes > today.month | |
); | |
if (!holiday){ | |
holiday = holidays[0]; | |
} | |
return holiday; | |
}; | |
const diffInDays = (date1, date2) => { | |
const diffTime = Math.abs(date2 - date1); | |
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); | |
return diffDays; | |
}; | |
const holidayUrl = getURL(getYear()); | |
const req = new Request(holidayUrl); | |
const res = await req.loadJSON(); | |
const nxtHolidayData = getNextHoliday(res); | |
if (config.runsInWidget) { | |
const widget = new ListWidget(); | |
widget.backgroundColor = new Color('3399ff'); | |
const title = widget.addText("Próximo feriado: "); | |
title.textColor = Color.white(); | |
title.textOpacity = 0.8; | |
title.font = new Font("Courier", 16); | |
widget.addSpacer(5); | |
const holidayName = widget.addText(`${nxtHolidayData.motivo}`) | |
holidayName.textColor = Color.white(); | |
holidayName.textOpacity = 0.8; | |
holidayName.font = new Font("Courier", 14); | |
widget.addSpacer(5); | |
const dateNameText = widget.addText(`${dayOfWeek(nxtHolidayData.dia, nxtHolidayData.mes-1, getYear())}`); | |
dateNameText.textColor = Color.white(); | |
dateNameText.textOpacity = 0.8; | |
dateNameText.font = new Font("Courier", 10); | |
widget.addSpacer(5); | |
const dateText = widget.addText(`${nxtHolidayData.dia}/${nxtHolidayData.mes}/${getYear()}`); | |
dateText.textColor = Color.white(); | |
dateText.textOpacity = 0.8; | |
dateText.font = new Font("Courier", 10); | |
widget.addSpacer(10); | |
const date1 = new Date(); | |
const date2 = new Date(getYear(), nxtHolidayData.mes-1, nxtHolidayData.dia); | |
const tminusText = widget.addText(`Faltan ${diffInDays(date1, date2)} días`) | |
tminusText.textColor = Color.white(); | |
tminusText.textOpacity = 0.8; | |
tminusText.font = new Font("Courier", 14); | |
Script.setWidget(widget); | |
Script.complete(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment