Python 언어를 이용하여 공공데이터포털에서 제공하는 오픈API 중 음력/양력 정보 조회 테스트 내용을 정리 합니다.
공공데이터포털에서 제공하는 API를 사용하기 위한 사전 준비는 아래 블로그를 참고하면 되겠습니다.
공공데이터포털의 오픈API 가이드는 아래와 같습니다.
테스트 기본 코드
기본 코드 형태는 아래와 같고 클래스를 이용하여 코드를 구성하였습니다.
import requests
from pprint import pprint
from urllib.parse import unquote
from xml_to_dict import XMLtoDict
# data.go.kr
# 양력/음력 정보 조회
class LrsrCldInfoServiceAPI:
def __init__(self):
self.service_key = '서비스 Key'
self.server_url = 'http://apis.data.go.kr/B090041/openapi/service/LrsrCldInfoService'
클래스를 만들고 __init__ 함수로 초기 정보를 셋팅 합니다.
초기 정보는 서비스 Key와 API 호출 기본 URL 입니다.
아래 API 호출 함수 코드를 클래스 내에 작성 및 호출 테스트를 하였습니다.
- import requests
- REST API 호출을 위한 라이브러리
- from pprint import pprint
- JSON 형태를 보기 좋게 출력하기 위한 라이브러리
- from urllib.parse import unquote
- 응답 결과의 인코딩을 변경하기 위한 라이브러리
- from xml_to_dict import XMLtoDict
- XML 응답 결과를 JSON으로 변형하기 위한 라이브러리
음력일 정보 조회
# 음력일 정보 조회
def getLunCalInfo(self):
url = self.server_url + '/getLunCalInfo'
params = {
'serviceKey': self.service_key,
'solYear': '2023',
'solMonth': '01',
'solDay': '20',
}
response = requests.get(url, params=params)
result = unquote(response.content.decode('utf-8'))
xd = XMLtoDict()
result = xd.parse(result)
pprint(result)
return result
실행 코드
api_con = LrsrCldInfoServiceAPI()
api_con.getLunCalInfo()
실행 결과
{'response': {'body': {'items': {'item': {'lunDay': '29',
'lunIljin': '무인(戊寅)',
'lunLeapmonth': '평',
'lunMonth': '12',
'lunNday': '30',
'lunSecha': '임인(壬寅)',
'lunWolgeon': '계축(癸丑)',
'lunYear': '2022',
'solDay': '20',
'solJd': '2459965',
'solLeapyear': '평',
'solMonth': '01',
'solWeek': '금',
'solYear': '2023'}},
'numOfRows': '10',
'pageNo': '1',
'totalCount': '1'},
'header': {'resultCode': '00', 'resultMsg': 'NORMAL SERVICE.'}}}
양력일 정보 조회
# 양력일 정보 조회
def getSolCalInfo(self):
url = self.server_url + '/getSolCalInfo'
params = {
'serviceKey': self.service_key,
'lunYear': '2023',
'lunMonth': '01',
'lunDay': '20',
}
response = requests.get(url, params=params)
result = unquote(response.content.decode('utf-8'))
xd = XMLtoDict()
result = xd.parse(result)
pprint(result)
return result
실행코드
api_con = LrsrCldInfoServiceAPI()
api_con.getSolCalInfo()
실행 결과
{'response': {'body': {'items': {'item': {'lunDay': '20',
'lunIljin': '기해(己亥)',
'lunLeapmonth': '평',
'lunMonth': '01',
'lunNday': '29',
'lunSecha': '계묘(癸卯)',
'lunWolgeon': '갑인(甲寅)',
'lunYear': '2023',
'solDay': '10',
'solJd': '2459986',
'solLeapyear': '평',
'solMonth': '02',
'solWeek': '금',
'solYear': '2023'}},
'numOfRows': '10',
'pageNo': '1',
'totalCount': '1'},
'header': {'resultCode': '00', 'resultMsg': 'NORMAL SERVICE.'}}}
특정 음력일 정보 조회
# 특정 음력일 정보 조회
def getSpcifyLunCalInfo(self):
url = self.server_url + '/getSpcifyLunCalInfo'
params = {
'serviceKey': self.service_key,
'fromSolYear': '2022',
'toSolYear': '2023',
'lunMonth': '01',
'lunDay': '20',
'leapMonth': '평',
}
response = requests.get(url, params=params)
result = unquote(response.content.decode('utf-8'))
xd = XMLtoDict()
result = xd.parse(result)
pprint(result)
return result
실행코드
api_con = LrsrCldInfoServiceAPI()
api_con.getSpcifyLunCalInfo()
실행 결과
{'response': {'body': {'items': {'item': [{'lunDay': '20',
'lunIljin': '갑진(甲辰)',
'lunLeapmonth': '평',
'lunMonth': '01',
'lunNday': '20',
'lunSecha': '임인(壬寅)',
'lunWolgeon': '임인(壬寅)',
'lunYear': '2022',
'solDay': '20',
'solJd': '2459631',
'solLeapyear': '평',
'solMonth': '02',
'solWeek': '일',
'solYear': '2022'},
{'lunDay': '20',
'lunIljin': '기해(己亥)',
'lunLeapmonth': '평',
'lunMonth': '01',
'lunNday': '20',
'lunSecha': '계묘(癸卯)',
'lunWolgeon': '갑인(甲寅)',
'lunYear': '2023',
'solDay': '10',
'solJd': '2459986',
'solLeapyear': '평',
'solMonth': '02',
'solWeek': '금',
'solYear': '2023'}]},
'numOfRows': '10',
'pageNo': '1',
'totalCount': '2'},
'header': {'resultCode': '00', 'resultMsg': 'NORMAL SERVICE.'}}}
율리우스적일 정보 조회
# 율리우스적일 정보 조회
def getJulDayInfo(self):
url = self.server_url + '/getJulDayInfo'
params = {
'serviceKey': self.service_key,
'solJd': '2229156',
}
response = requests.get(url, params=params)
result = unquote(response.content.decode('utf-8'))
xd = XMLtoDict()
result = xd.parse(result)
pprint(result)
return result
실행코드
api_con = LrsrCldInfoServiceAPI()
api_con.getJulDayInfo()
실행 결과
{'response': {'body': {'items': {'item': {'lunDay': '01',
'lunIljin': '기축(己丑)',
'lunLeapmonth': '평',
'lunMonth': '01',
'lunNday': '29',
'lunSecha': '신미(辛未)',
'lunWolgeon': '경인(庚寅)',
'lunYear': '1391',
'solDay': '05',
'solJd': '2229156',
'solLeapyear': '평',
'solMonth': '02',
'solWeek': '일',
'solYear': '1391'}},
'numOfRows': '10',
'pageNo': '1',
'totalCount': '1'},
'header': {'resultCode': '00', 'resultMsg': 'NORMAL SERVICE.'}}}
마치며
위 코드를 이용하여 공공데이터포털에서 제공하는 음력/양력 정보 조회 API를 사용하는 테스트를 수행 하였습니다.