holiday_api_checker.py
· 3.6 KiB · Python
Raw
import requests
class HolidayAPI:
def __init__(self, base_url):
"""
Initialize the HolidayAPI class.
:param base_url: The base URL for the API endpoint.
"""
self.base_url = base_url
def get_holidays(self, page=0, size=10):
"""
Fetch holiday data from the API.
:param page: The page number to fetch.
:param size: The number of records per page.
:return: A list of holiday data dictionaries.
"""
url = f"{self.base_url}?page={page}&size={size}"
headers = {
'accept': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
def get_holidays_by_year(self, year):
"""
Fetch holiday data for a specific year.
:param year: The year for which to fetch holidays.
:return: A list of holiday data dictionaries for the given year.
"""
all_holidays = []
page = 0
while True:
data = self.get_holidays(page=page, size=100)
if not data:
break
all_holidays.extend(data)
page += 1
return [holiday for holiday in all_holidays if holiday.get("year") == str(year)]
def is_holiday(self, date):
"""
Check if a specific date is a holiday.
:param date: The date to check in YYYYMMDD format.
:return: True if the date is a holiday, False otherwise.
"""
page = 0
while True:
data = self.get_holidays(page=page, size=100)
if not data:
break
for holiday in data:
if holiday.get("date") == date and holiday.get("isholiday") == "是":
return True
page += 1
return False
class Holiday:
def __init__(self, date, year, name, isholiday, holidaycategory, description):
"""
Represent a single holiday entry.
:param date: The date of the holiday in YYYYMMDD format.
:param year: The year of the holiday.
:param name: The name of the holiday (if any).
:param isholiday: Whether it is a holiday ('是' or '否').
:param holidaycategory: The category of the holiday.
:param description: The description of the holiday.
"""
self.date = date
self.year = year
self.name = name
self.isholiday = isholiday
self.holidaycategory = holidaycategory
self.description = description
def __repr__(self):
return f"Holiday(date={self.date}, year={self.year}, name={self.name}, " \
f"isholiday={self.isholiday}, holidaycategory={self.holidaycategory}, " \
f"description={self.description})"
# Example usage
if __name__ == "__main__":
api = HolidayAPI("https://data.ntpc.gov.tw/api/datasets/308dcd75-6434-45bc-a95f-584da4fed251/json")
holidays_2025 = api.get_holidays_by_year(2025)
holidays = []
for entry in holidays_2025:
holiday = Holiday(
date=entry.get("date"),
year=entry.get("year"),
name=entry.get("name"),
isholiday=entry.get("isholiday"),
holidaycategory=entry.get("holidaycategory"),
description=entry.get("description")
)
holidays.append(holiday)
for holiday in holidays:
print(holiday)
# Check if a specific date is a holiday
# specific_date = "20250101"
specific_date = "20250102"
print(f"Is {specific_date} a holiday? {api.is_holiday(specific_date)}")
| 1 | import requests |
| 2 | |
| 3 | class HolidayAPI: |
| 4 | def __init__(self, base_url): |
| 5 | """ |
| 6 | Initialize the HolidayAPI class. |
| 7 | :param base_url: The base URL for the API endpoint. |
| 8 | """ |
| 9 | self.base_url = base_url |
| 10 | |
| 11 | def get_holidays(self, page=0, size=10): |
| 12 | """ |
| 13 | Fetch holiday data from the API. |
| 14 | :param page: The page number to fetch. |
| 15 | :param size: The number of records per page. |
| 16 | :return: A list of holiday data dictionaries. |
| 17 | """ |
| 18 | url = f"{self.base_url}?page={page}&size={size}" |
| 19 | headers = { |
| 20 | 'accept': 'application/json' |
| 21 | } |
| 22 | response = requests.get(url, headers=headers) |
| 23 | |
| 24 | if response.status_code == 200: |
| 25 | return response.json() |
| 26 | else: |
| 27 | response.raise_for_status() |
| 28 | |
| 29 | def get_holidays_by_year(self, year): |
| 30 | """ |
| 31 | Fetch holiday data for a specific year. |
| 32 | :param year: The year for which to fetch holidays. |
| 33 | :return: A list of holiday data dictionaries for the given year. |
| 34 | """ |
| 35 | all_holidays = [] |
| 36 | page = 0 |
| 37 | while True: |
| 38 | data = self.get_holidays(page=page, size=100) |
| 39 | if not data: |
| 40 | break |
| 41 | all_holidays.extend(data) |
| 42 | page += 1 |
| 43 | |
| 44 | return [holiday for holiday in all_holidays if holiday.get("year") == str(year)] |
| 45 | |
| 46 | def is_holiday(self, date): |
| 47 | """ |
| 48 | Check if a specific date is a holiday. |
| 49 | :param date: The date to check in YYYYMMDD format. |
| 50 | :return: True if the date is a holiday, False otherwise. |
| 51 | """ |
| 52 | page = 0 |
| 53 | while True: |
| 54 | data = self.get_holidays(page=page, size=100) |
| 55 | if not data: |
| 56 | break |
| 57 | for holiday in data: |
| 58 | if holiday.get("date") == date and holiday.get("isholiday") == "是": |
| 59 | return True |
| 60 | page += 1 |
| 61 | return False |
| 62 | |
| 63 | class Holiday: |
| 64 | def __init__(self, date, year, name, isholiday, holidaycategory, description): |
| 65 | """ |
| 66 | Represent a single holiday entry. |
| 67 | :param date: The date of the holiday in YYYYMMDD format. |
| 68 | :param year: The year of the holiday. |
| 69 | :param name: The name of the holiday (if any). |
| 70 | :param isholiday: Whether it is a holiday ('是' or '否'). |
| 71 | :param holidaycategory: The category of the holiday. |
| 72 | :param description: The description of the holiday. |
| 73 | """ |
| 74 | self.date = date |
| 75 | self.year = year |
| 76 | self.name = name |
| 77 | self.isholiday = isholiday |
| 78 | self.holidaycategory = holidaycategory |
| 79 | self.description = description |
| 80 | |
| 81 | def __repr__(self): |
| 82 | return f"Holiday(date={self.date}, year={self.year}, name={self.name}, " \ |
| 83 | f"isholiday={self.isholiday}, holidaycategory={self.holidaycategory}, " \ |
| 84 | f"description={self.description})" |
| 85 | |
| 86 | # Example usage |
| 87 | if __name__ == "__main__": |
| 88 | api = HolidayAPI("https://data.ntpc.gov.tw/api/datasets/308dcd75-6434-45bc-a95f-584da4fed251/json") |
| 89 | holidays_2025 = api.get_holidays_by_year(2025) |
| 90 | |
| 91 | holidays = [] |
| 92 | for entry in holidays_2025: |
| 93 | holiday = Holiday( |
| 94 | date=entry.get("date"), |
| 95 | year=entry.get("year"), |
| 96 | name=entry.get("name"), |
| 97 | isholiday=entry.get("isholiday"), |
| 98 | holidaycategory=entry.get("holidaycategory"), |
| 99 | description=entry.get("description") |
| 100 | ) |
| 101 | holidays.append(holiday) |
| 102 | |
| 103 | for holiday in holidays: |
| 104 | print(holiday) |
| 105 | |
| 106 | # Check if a specific date is a holiday |
| 107 | # specific_date = "20250101" |
| 108 | specific_date = "20250102" |
| 109 | print(f"Is {specific_date} a holiday? {api.is_holiday(specific_date)}") |
| 110 | |
| 111 |