Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Created January 15, 2022 14:15
Show Gist options
  • Save Avi-E-Koenig/95216a068b5ad4ca8bcb47fb4ed2dae5 to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/95216a068b5ad4ca8bcb47fb4ed2dae5 to your computer and use it in GitHub Desktop.
Get hebrew holidays list from API www.hebcal.com
const main = async () => {
try {
const resultSet = fetchYearRange(2022, 2052)
console.log("πŸš€ ~ file: main.js ~ line 24 ~ merged ~ resultSet", JSON.stringify(resultSet))
// copy and do stuff.
console.log("πŸš€ ~ file: main.js ~ line 7 ~ main ~ resultSet", resultSet)
// save this to file if you're less lazy then me :)
} catch (error) {
console.error("πŸš€ ~ file: main.js ~ line 11 ~ main ~ error", error)
}
}
const fetchYearData = async (year) => {
const apiUrl = `https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&min=on&year=${year}&month=x&geo=geoname&geonameid=3448439&M=on&s=on&D=on`
const apiCallRes = await fetch(apiUrl)
const json = await apiCallRes.json()
let { items } = json
items = items.map(item => {
delete item.leyning
return item
})
return items;
}
const fetchYearRange = async (startYear, endYear) => {
let eventList = []
const yearAmount = endYear - startYear
for (let i = 0; i < yearAmount + 1; i++) {
const targetYear = startYear + i
const yearlyEvents = await fetchYearData(targetYear)
eventList = eventList.concat(yearlyEvents)
}
return eventList.reduce((acc, item) => {
let target = acc.find(entry => entry.date === item.date)
if (!target) {
acc.push(item)
return acc
}
const targetIdx = acc.findIndex(entry => entry.date === item.date)
acc[targetIdx] = { ...target, ...item }
return acc
}, [])
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment