우리 어플에서 현재 모임을 만들 때 달력에 날을 체크하여 모임 일정을 생성한다.
현재는 그냥 달력에 아무런 표시가 되어 있지 않다.
이때 달력에 해당 날짜의 날씨가 적혀있다면 날을 선정하는데 도움이 되지 않을까 싶어 생각하게 되었다.
기상청 API를 활용하여 장소를 고르면 10일까지의 날씨를 알 수 있는 중기육상예보 API를 활용하여 나타낸다.
여기까지의 아이디어는 아래 게시글을 참고하면 된다.
파이썬으로 기상청 일기예보와 날씨 API 사용하기
기상청에서 제공하는 날씨 API를 사용해보려고 한다. 현재 내가 다니고 있는 회사는 모임플래폼을 운영하고 있다. 어플에서는 모임을 만들 수 있는데 날짜와 시간, 장소를 적는다. 그러면 모임을
beomcoder.tistory.com
그리고 모임날짜가 되면 모임이 시작되기 6시간 전에 미리 알림으로 해당 모임인원들에게 알림을 보내주는 것이다.
아래 사진은 아직 도입되지 않아서 내가 임의로 만들었다.
먼저 데이터포털에 들어가서 API키를 받는다. 키를 받는 방법은 위에 올려둔 게시글에서 확인 가능하다.
기상청_단기예보 ((구)_동네예보) 조회서비스
초단기실황, 초단기예보, 단기((구)동네)예보, 예보버전 정보를 조회하는 서비스입니다. 초단기실황정보는 예보 구역에 대한 대표 AWS 관측값을, 초단기예보는 예보시점부터 6시간까지의 예보를,
www.data.go.kr
단기예보 조회서비스는 Decoding key가 적용되는 듯 하다. 일반 인증키를 복사해서 적어놓는다.
!pip install requests
!pip install beautifulsoup4
!pip install json
def weather(target):
def now_weather_info():
url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst'
params = {
'serviceKey' : '아까 적어놓은 본인의 키값',
'pageNo' : '1',
'numOfRows' : '1000',
'dataType' : 'JSON',
'base_date' : '20230220',
'base_time' : '1500',
'nx' : '58',
'ny' : '75'
}
r = requests.get(url, params=params)
response = json.loads(r.content)
print(response)
def today_gathering():
url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtFcst'
params = {
'serviceKey' : '아까 적어놓은 본인의 키값',
'pageNo' : '1',
'numOfRows' : '1000',
'dataType' : 'JSON',
'base_date' : '20230220',
'base_time' : '0900',
'nx' : '58',
'ny' : '75'
}
r = requests.get(url, params=params)
response = json.loads(r.content)
print(response)
_function = {'now_weather_info': now_weather_info, 'today_gathering': today_gathering}
_function[target]()
# gwangju('festival')
weather('today_gathering')
날씨 api를 사용하려고 하기 때문에 함수로 기능들을 묶어주었다.
내가 사용은 하지 않을 거지만 우선 확인해보려고 함수를 만들었다.
now_weather_info()는 예측정보라기보다는 현재의 정보들을 보여준다.
2023년 02월 20일의 날씨 중, 현재 시간이 16시라면 base_date는 20230220, base_time에 1500을 적어준다.
nx, ny는 아래에서 한꺼번에 알려주려고 한다.
params = {
'serviceKey' : '아까 적어놓은 본인의 키값',
'pageNo' : '1',
'numOfRows' : '1000',
'dataType' : 'JSON',
'base_date' : '20230220',
'base_time' : '0900',
'nx' : '58',
'ny' : '75'
}
today_gathering()함수 내 api 호출 파라미터에 대해 알려주려한다.
먼저 함수는 자유롭게 이름을 지으면 되는데
나는 어플의 오늘 모임 알림이므로 함수이름을 today_gathering으로 지었다.
url은 호출주소이고, params는 어떤 정보를 요구할지 적는 곳이다.
'serviceKey'는 본인의 키값을 적으면 된다. 인코딩, 디코딩 키를 둘다 넣어보고 되는 값을 적으면 된다.
'pageNo' , 'numOfRows'는 잘 모르겠다. 값을 바꿔봐도 같은 값이 나온다.
'dataType'은 리턴값을 'JSON'형식으로 받을지 'XML'형식으로 받을지 선택이 가능하다.
'base_date'는 날짜를 적으면 되는데 최근 3일간의 자료만 제공가능하다.
{'response': {'header': {'resultCode': '10', 'resultMsg': '최근 3일 간의 자료만 제공합니다.'}}}
{'response': {'header': {'resultCode': '03', 'resultMsg': 'NO_DATA'}}}
너무 예전의 날짜나 미래의 시간이나 날짜를 적으면 위의 응답을 받게 된다.
'base_time'은 현재 시간보다 약 30분에서 1시간정도 전의 시간을 적는게 좋다.
'rx', 'ry'는 그 지역의 x값, y값을 적어주면 된다.
파일을 다운로드 받으면 본인이 확인하고 싶은 지역을 찾아서 격자x, 격자y값을 입력해주면 된다.
아래는 공식문서의 표이니 참고하면 된다.
{'response': {'header': {'resultCode': '00', 'resultMsg': 'NORMAL_SERVICE'}, 'body': {'dataType': 'JSON', 'items': {'item': [{'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '강수없음', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '3', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '65', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '55', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '50', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0.5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '2.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '2.9', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '3.1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '3.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0.6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0.2', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '-1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '-3.5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '-4.8', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '-5.1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '219', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '263', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '293', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '321', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '328', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '327', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '3', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '6', 'nx': 58, 'ny': 75}]}, 'pageNo': 1, 'numOfRows': 1000, 'totalCount': 60}}} |
제대로 입력하여 실행시키면 위와 같은 호출값을 받게 되는데 정신없다.
나도 처음에 이 데이터를 받고 물음표가 되었다.
먼저 하나씩 하면 내가 적은 시간(base_time) 이후 6시간까지의 예보를 알려준다.
그리고 XX:30으로 통일되는 듯 하다.
base_time 을 1000으로 적어도 1030으로 전송된다.
이 값을 조금 보기 좋게 바꿔보겠다.
1시간 후의 광주 동림동 일기예보입니다. 낙뢰 : 0 강수형태 : 없음 1시간 강수량 : 강수없음 하늘상태 : 맑음 기온 : 6 습도 : 55 동서바람성분 : 1.2 남북바람성분 : -0.1 풍향 : 277 풍속 : 1 2시간 후의 광주 동림동 일기예보입니다. 낙뢰 : 0 강수형태 : 없음 1시간 강수량 : 강수없음 하늘상태 : 구름많음 기온 : 7 습도 : 50 동서바람성분 : 2.3 남북바람성분 : -0.6 풍향 : 287 풍속 : 2 3시간 후의 광주 동림동 일기예보입니다. 낙뢰 : 0 강수형태 : 없음 1시간 강수량 : 강수없음 하늘상태 : 맑음 기온 : 7 습도 : 40 동서바람성분 : 2.8 남북바람성분 : -2.5 풍향 : 313 풍속 : 4 (중략) |
왜 풍속이 1,2,3,4 ... 로 가는지는 우연인지 api가 잘못된건지 모르겠다.
def weather(target):
def today_gathering():
url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtFcst'
params = {
'serviceKey' : '본인의 키값',
'pageNo' : '1',
'numOfRows' : '1000',
'dataType' : 'JSON',
'base_date' : '20230220',
'base_time' : '1000',
'nx' : '58',
'ny' : '75'
}
r = requests.get(url, params=params)
response = json.loads(r.content)
if response['response']['header']['resultCode'] != '00':
return response['response']['header']['resultMsg']
else:
code_name = {'T1H': '기온', 'RN1': '1시간 강수량', 'SKY': '하늘상태', 'UUU': '동서바람성분', 'VVV': '남북바람성분',
'REH': '습도', 'PTY': '강수형태', 'LGT': '낙뢰', 'VEC': '풍향', 'WSD': '풍속'}
sky_value = {'1': '맑음', '3': '구름많음', '4': '흐림'}
pty_value = {'0': '없음', '1': '비', '2': '비/눈', '3': '눈', '5': '빗방울', '6': '빗방울/눈날림', '7': '눈날림'}
result = {'1': {}, '2': {}, '3': {}, '4': {}, '5': {}, '6': {}}
for i, v in enumerate(response['response']['body']['items']['item']):
if v['category'] == 'SKY':
result[str(i%6 + 1)][code_name[v['category']]] = sky_value[v['fcstValue']]
elif v['category'] == 'PTY':
result[str(i%6 + 1)][code_name[v['category']]] = pty_value[v['fcstValue']]
else:
result[str(i%6 + 1)][code_name[v['category']]] = v['fcstValue']
return result
_function = {'today_gathering': today_gathering}
return _function[target]()
# gwangju('festival')
result = weather('today_gathering')
for i in result.keys():
print(i+'시간 후의 광주 동림동 일기예보입니다.')
for k, v in result[i].items():
print(k, ':', v)
print()
일단은 이렇게 바꿔보았는데 풍속, 풍향, 습도 등에 기호를 알맞게 추가로 넣으면 될듯하다.
아래 표를 참고하여도 된다.
오픈 api 가이드를 보고 만들어보아도 된다.
내가 만든 코드는 제안을 하기 위해 잠깐 만든 코드라 현재 엉성하다.
대표님께서 진행하라고 하시면 FE팀과 상의하여 코드를 다시 짜야 한다.
'개발 > 파이썬 API' 카테고리의 다른 글
[디스코드 봇] 롤 전적검색기능 만들기 - 1 (0) | 2024.03.17 |
---|---|
파이썬 각종 OCR 사용해보기 및 네이버 클라우드 AI OCR 사용법 (1) (0) | 2023.03.30 |
파이썬으로 기상청 일기예보와 날씨 API 사용하기 (0) | 2023.02.18 |
koGPT, openAI GPT3 사용해보기 (chat gpt3, dall-e) (0) | 2023.02.13 |
카카오 API 'Karlo', stable diffusion 사용해보기 (0) | 2023.02.12 |
댓글