From cb1a98d940136b513d855f601c994ae97e8d75c5 Mon Sep 17 00:00:00 2001 From: KWON Date: Fri, 27 Jun 2025 13:32:19 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=9C=EA=B0=84=EB=8C=80=EB=B3=84=20?= =?UTF-8?q?=EA=B0=95=EC=88=98=EB=9F=89=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20htm?= =?UTF-8?q?l=ED=98=95=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/weather.py | 74 ++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/data/weather.py b/data/weather.py index 4d0986d..fec8ca1 100644 --- a/data/weather.py +++ b/data/weather.py @@ -1,25 +1,10 @@ +# weather.py + import requests import json import re from datetime import datetime - -base_date = datetime.now().strftime('%Y%m%d') # 오늘 날짜 -serviceKey = 'mHrZoSnzVc+2S4dpCe3A1CgI9cAu1BRttqRdoEy9RGbnKAKyQT4sqcESDqqY3grgBGQMuLeEgWIS3Qxi8rcDVA==' - -url = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst" - -params = { - 'serviceKey': serviceKey, - 'numOfRows': '1000', - 'pageNo': '1', - 'dataType': 'JSON', - 'base_date': base_date, - 'base_time': '0800', # 02:00 부터 3시간 단위 발표 - 'nx': '57', - 'ny': '130' -} - -response = requests.get(url, params=params) +from config import serviceKey, TODAY def parse_precip(value): if value == '강수없음': @@ -27,30 +12,49 @@ def parse_precip(value): elif '1mm 미만' in value: return 0.5 else: - # 숫자만 추출 (예: '1.0mm' → 1.0) match = re.search(r"[\d.]+", value) if match: return float(match.group()) else: return 0.0 -try: - data = response.json() - total_rainfall = 0.0 +def get_precipitation_summary(): + url = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst" - print("📅 시간대별 강수량 (단기예보 기준):") + params = { + 'serviceKey': serviceKey, + 'numOfRows': '1000', + 'pageNo': '1', + 'dataType': 'JSON', + 'base_date': TODAY, + 'base_time': '0800', + 'nx': '57', + 'ny': '130' + } - for item in data['response']['body']['items']['item']: - if item['category'] == 'PCP' and item['fcstDate'] == base_date: - time = item['fcstTime'] - if 900 < int(time) < 2300: - value = item['fcstValue'] - mm = parse_precip(value) - print(f" {time}시 → {mm}mm") - total_rainfall += mm + response = requests.get(url, params=params) - print(f"\n🌧️ 총 예상 강수량: {total_rainfall:.1f}mm") + try: + data = response.json() + total_rainfall = 0.0 + lines = [f"

📅 시간대별 강수량 :

") + lines.append(f"

🌧️ 영업시간 중 총 예상 강수량: {total_rainfall:.1f}mm

") + + return ''.join(lines) + + except json.decoder.JSONDecodeError: + return "⚠️ 응답이 JSON 형식이 아닙니다." + +# 테스트용 +if __name__ == "__main__": + print(get_precipitation_summary())