Skip to content

Commit

Permalink
chore: Add json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Oct 16, 2024
1 parent 2f64974 commit 7bb62d1
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

| Updated Date | Data Hash |
| --- | --- |
| <!--Date:Begin-->Oct 16, 2024<!--Date:End--> | <!--Version:Begin-->f15bb198ac023f24f1c5e2b57d7acc62<!--Version:End--> |
| <!--Date:Begin-->Oct 16, 2024<!--Date:End--> | <!--Version:Begin-->baf5f078ed2465f9c63ad9078de43ffa<!--Version:End--> |

## JSON Schema

See [schema](./schemas/json-schema-data.json)
130 changes: 130 additions & 0 deletions schemas/json-schema-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"$id": "https://raw.githubusercontent.com/chonla/thai-financial-instiutions-holiday/refs/heads/main/data.schema.json",
"type": "array",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Holiday",
"description": "Thai Financial Institutions Holiday",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "object",
"required": [
"yearCe",
"yearBe",
"data"
],
"properties": {
"yearCe": {
"description": "Year in Christian era",
"type": "number"
},
"yearBe": {
"description": "Year in Buddhist era",
"type": "number"
},
"data": {
"description": "Holiday detail",
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"required": [
"dateStamp",
"dayOfWeekIndex",
"dayOfWeekTh",
"dayOfWeekEn",
"dayOfWeekAbbrEn",
"dayOfWeekAbbrTh",
"day",
"monthIndex",
"monthTh",
"monthAbbrTh",
"monthEn",
"monthAbbrEn",
"yearCe",
"yearBe",
"descriptionTh",
"descriptionEn"
],
"properties": {
"dateStamp": {
"description": "Date stamp in YYYYMMDD format",
"type": "string",
"minLength": 1
},
"dayOfWeekIndex": {
"description": "Day of week index, start from zero",
"type": "number"
},
"dayOfWeekTh": {
"description": "Name of day of week in Thai",
"type": "string",
"minLength": 1
},
"dayOfWeekEn": {
"description": "Name of day of week in English",
"type": "string",
"minLength": 1
},
"dayOfWeekAbbrEn": {
"description": "Abbreviated Name of day of week in English",
"type": "string",
"minLength": 1
},
"dayOfWeekAbbrTh": {
"description": "Abbreviated Name of day of week in Thai",
"type": "string",
"minLength": 1
},
"day": {
"description": "Day of month",
"type": "number"
},
"monthIndex": {
"description": "Month index, start from zero",
"type": "number"
},
"monthTh": {
"description": "Name of month in Thai",
"type": "string",
"minLength": 1
},
"monthAbbrTh": {
"description": "Abbreviated name of month in Thai",
"type": "string",
"minLength": 1
},
"monthEn": {
"description": "Name of month in English",
"type": "string",
"minLength": 1
},
"monthAbbrEn": {
"description": "Abbreviated name of month in English",
"type": "string",
"minLength": 1
},
"yearCe": {
"description": "Year in Christian era",
"type": "number"
},
"yearBe": {
"description": "Year in Buddhist era",
"type": "number"
},
"descriptionTh": {
"description": "Description of holiday in Thai",
"type": "string",
"minLength": 1
},
"descriptionEn": {
"description": "Description of holiday in English",
"type": "string",
"minLength": 1
}
}
}
}
}
}
}
38 changes: 27 additions & 11 deletions updater/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Name(StrEnum):


class Holiday(NamedTuple):
date_stamp: str # YYYYMMDD
day_of_week_index: int # Sun=0, ..., Sat=6
day_of_week_th: str
day_of_week_en: str
Expand All @@ -41,6 +42,12 @@ class Holiday(NamedTuple):
description_en: str


class HolidayInYear(NamedTuple):
year_ce: int
year_be: int
data: List[Holiday]


class HolidayDetail(NamedTuple):
day_of_week_index: int # Sun=0, ..., Sat=6
day_of_week_th: str
Expand Down Expand Up @@ -74,13 +81,14 @@ def get_holiday_url(lang: Lang = Lang.EN, year: int = 2024):
return f"{base_url}{lang_suffix}.model.{year_model}.json"


def get_holiday(year: int = 2024) -> List[Dict[str, Dict]]:
def get_holiday(year: int = 2024) -> List[Dict]:
data_th = get_holiday_data(Lang.TH, year)
data_en = get_holiday_data(Lang.EN, year)
holiday = {}
holiday = []
for (data_key, entry) in data_en.items():
data_key = f"{entry.year_ce}{entry.month_index+1:02d}{entry.day:02d}"
holiday[data_key] = Holiday(
date_stamp = f"{entry.year_ce}{entry.month_index+1:02d}{entry.day:02d}"
holiday.append(Holiday(
date_stamp = date_stamp,
day_of_week_index = entry.day_of_week_index,
day_of_week_th = entry.day_of_week_th,
day_of_week_en = entry.day_of_week_en,
Expand All @@ -96,7 +104,7 @@ def get_holiday(year: int = 2024) -> List[Dict[str, Dict]]:
year_be = entry.year_be,
description_th = data_th[data_key].description,
description_en = entry.description
)._asdict()
)._asdict())

return holiday

Expand Down Expand Up @@ -244,12 +252,20 @@ def dump_json_to_string(data: Any) -> str:
now = datetime.now()
current_year = now.year
next_year = current_year + 1
current_year_holiday = get_holiday(current_year)
next_year_holiday = get_holiday(next_year)
holiday = {
current_year: current_year_holiday,
next_year: next_year_holiday
}
current_year_holiday = HolidayInYear(
year_be = current_year + 543,
year_ce = current_year,
data = get_holiday(current_year)
)._asdict()
next_year_holiday = HolidayInYear(
year_be = next_year + 543,
year_ce = next_year,
data = get_holiday(next_year)
)._asdict()
holiday = [
current_year_holiday,
next_year_holiday
]
holiday_json = dump_json_to_string(holiday)
digest = create_hash(holiday_json)
try:
Expand Down

0 comments on commit 7bb62d1

Please sign in to comment.