From efa0abfd336ec91125b276606a7efc2193cf7a7e Mon Sep 17 00:00:00 2001 From: javalikescript Date: Sat, 11 Jan 2025 22:21:44 +0100 Subject: [PATCH] Add public holidays --- .../calendrier-scolaire.lua | 62 +++++++++++++++---- extensions/calendrier-scolaire/manifest.json | 18 ++++++ 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/extensions/calendrier-scolaire/calendrier-scolaire.lua b/extensions/calendrier-scolaire/calendrier-scolaire.lua index aa818ed..0ac931d 100644 --- a/extensions/calendrier-scolaire/calendrier-scolaire.lua +++ b/extensions/calendrier-scolaire/calendrier-scolaire.lua @@ -20,18 +20,40 @@ local function createThing() ['@type'] = 'BooleanProperty', title = 'Holiday', type = 'boolean', - description = 'Holiday', + description = 'Whether or not today is a school holiday', readOnly = true - }, '') + }, false):addProperty('publicHoliday', { + ['@type'] = 'BooleanProperty', + title = 'Public Holiday', + type = 'boolean', + description = 'Whether or not today is a public holiday', + readOnly = true + }, false) end local function whereIn(name, year) return string.format("%s > date'%s-01-01' and %s < date'%s-12-31'", name, year, name, year) end +local function getPublicHolidays(year) + local configuration = extension:getConfiguration() + local url = Url:new(configuration.publicHoliday.apiUrl) + local resource = concatPath(url:getPath(), string.format('%s/%s.json', configuration.publicHoliday.zone, year)) + logger:fine('fetching %s', resource) + local client = HttpClient:new(url) + return client:fetch(resource):next(function(response) + if response:getStatusCode() == 200 then + return response:json() + end + return Promise.reject('Response status is '..response:getStatusCode()) + end):finally(function() + client:close() + end) +end + local function getRecords(year) local configuration = extension:getConfiguration() - local url = Url:new(configuration.apiUrl) -- https://data.opendatasoft.com/api/explore/v2.1/ + local url = Url:new(configuration.apiUrl) local path = concatPath(url:getPath(), 'catalog/datasets/'..configuration.datasetId..'/records') local resource = path..'?'..Url.mapToQuery({limit = 20, refine = { 'location:"'..configuration.location..'"', 'population:"-"' @@ -53,32 +75,44 @@ local function getRecords(year) end) end -local function findRecord(records, time) +local function findRecord(time, records) for _, record in ipairs(records) do local startTime = Date.fromISOString(record.start_date, true) local endTime = Date.fromISOString(record.end_date, true) if time >= startTime and time < endTime then - return record + return record.description end end end -local function updateThing(thing, records) - local holiday = findRecord(records, Date.now()) ~= nil - --logger:info('updateThing() christmas: %s, holiday: %s', findRecord(records, Date.fromISOString('2025-12-25', true)) ~= nil, holiday) - logger:fine('updateThing() holiday: %s', holiday) - thing:updatePropertyValue('holiday', holiday) +local function findPublicHolidays(time, publicHolidays) + for day, description in pairs(publicHolidays) do + local startTime = Date.fromISOString(day) + if time >= startTime and time < startTime + 86400000 then + return description + end + end +end + +local function updateThing(thing, records, publicHolidays) + local time = Date.now() + --logger:info('updateThing() christmas: %s 11/11: %s', findRecord(Date.fromISOString('2025-12-27T10:00:00'), records), findPublicHolidays(Date.fromISOString('2025-11-11T10:00:00'), publicHolidays)) + thing:updatePropertyValue('holiday', findRecord(time, records) ~= nil) + if publicHolidays then + thing:updatePropertyValue('publicHoliday', findPublicHolidays(time, publicHolidays) ~= nil) + end end local year local records +local publicHolidays local thing local function update() if thing then local y = os.date('%Y') if records and y == year then - updateThing(thing, records) + updateThing(thing, records, publicHolidays) else getRecords(y):next(function(r) logger:fine('records %T', r) @@ -89,7 +123,11 @@ local function update() logger:info('%s %s %s', record.start_date, record.end_date, record.description) end end - updateThing(thing, records) + return getPublicHolidays(year) + end):next(function(h) + logger:info('public holidays %T', h) + publicHolidays = h + updateThing(thing, records, publicHolidays) end):catch(function(reason) logger:warn('Fail to fetch records due to %s', reason) end) diff --git a/extensions/calendrier-scolaire/manifest.json b/extensions/calendrier-scolaire/manifest.json index ea1505c..7c432ed 100644 --- a/extensions/calendrier-scolaire/manifest.json +++ b/extensions/calendrier-scolaire/manifest.json @@ -6,6 +6,24 @@ "schema": { "type": "object", "properties": { + "publicHoliday": { + "type": "object", + "properties": { + "apiUrl": { + "title": "API URL", + "type": "string", + "pattern": "^https?://.+$", + "default": "https://calendrier.api.gouv.fr/jours-feries/" + }, + "zone": { + "title": "Zone", + "type": "string", + "pattern": "^[%w%-@_]+$", + "required": true, + "default": "metropole" + } + } + }, "apiUrl": { "title": "API URL", "type": "string",