Skip to content

Commit

Permalink
Add public holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Jan 11, 2025
1 parent 7bbd79a commit efa0abf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
62 changes: 50 additions & 12 deletions extensions/calendrier-scolaire/calendrier-scolaire.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"'
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions extensions/calendrier-scolaire/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit efa0abf

Please sign in to comment.