Scheduler holidays from API

Automatically load public holidays into schedulers #

Create a scheduled script that runs once per day.

Modify category and country variables as needed.
List of supported countries.

Script will delete all existing holidays belonging to the specified category before loading new data.

category = 'auto'
country = 'IT'

function loadholidays(year)
  local http = require('socket.http')
  local json = require('json')
  local webrequest = require('webrequest')
  local baseurl = 'https://date.nager.at/api/v3/PublicHolidays/%d/%s'
  local url = string.format(baseurl, year, country)
  local resp, err = http.request(url)

  local data = json.pdecode(resp)
  if type(data) ~= 'table' then
    log('failed to fetch data', err)
    return nil
  end

  for _, item in ipairs(data) do
    local y, m, d = item.date:match('(%d+)%-(%d+)%-(%d+)')

    webrequest('schedulers', 'holidays-save', {
      data = {
        name = item.localName or item.name or 'Holiday',
        type = '',
        category = category,
        day = d,
        month = m,
        year = y,
      }
    })
  end
end

now = os.date('*t')

db:query('DELETE FROM scheduler_holidays WHERE category=?', category)

loadholidays(now.year)
loadholidays(now.year + 1)

Uses data from Nager.Date