스타일 수정, 응답 오류 시 재시도 1회 추가
This commit is contained in:
@ -18,7 +18,7 @@ def parse_precip(value):
|
|||||||
else:
|
else:
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
def get_precipitation_summary():
|
def get_precipitation_summary(retry=True):
|
||||||
url = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst"
|
url = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst"
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
@ -37,9 +37,29 @@ def get_precipitation_summary():
|
|||||||
try:
|
try:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
total_rainfall = 0.0
|
total_rainfall = 0.0
|
||||||
lines = [f'<div style="text-align:center;"><h3 style="font-size:2em; text-align:center; margin: 20px 0;">[시간대별 강수량]</h3>']
|
|
||||||
lines.append(f'<table style="display: inline-table; min-width:400px; text-align:center; ">')
|
lines = [
|
||||||
lines.append(f'<tr><th style="border: 1px #333 solid; padding:0;">시간</th><th style="border: 1px #333 solid; padding:0;">강수량</th></tr>')
|
'<style>',
|
||||||
|
'.weatherinfo table {',
|
||||||
|
' border-collapse: collapse;',
|
||||||
|
' border-spacing: 0;',
|
||||||
|
' display: inline-table;',
|
||||||
|
'}',
|
||||||
|
'.weatherinfo td, .weatherinfo th {',
|
||||||
|
' padding: 0;',
|
||||||
|
' border: 1px solid #333;',
|
||||||
|
'}',
|
||||||
|
'.weatherinfo h3 {',
|
||||||
|
' font-size: 2em;',
|
||||||
|
' text-align: center;',
|
||||||
|
' margin: 20px 0;',
|
||||||
|
'}',
|
||||||
|
'</style>',
|
||||||
|
'<div class="weatherinfo">',
|
||||||
|
'<h3>[시간대별 강수량]</h3>',
|
||||||
|
'<table>',
|
||||||
|
'<tr><th>시간</th><th>강수량</th></tr>'
|
||||||
|
]
|
||||||
|
|
||||||
for item in data['response']['body']['items']['item']:
|
for item in data['response']['body']['items']['item']:
|
||||||
if item['category'] == 'PCP' and item['fcstDate'] == TODAY:
|
if item['category'] == 'PCP' and item['fcstDate'] == TODAY:
|
||||||
@ -47,17 +67,20 @@ def get_precipitation_summary():
|
|||||||
if 900 < int(time) < 2300:
|
if 900 < int(time) < 2300:
|
||||||
mm = parse_precip(item['fcstValue'])
|
mm = parse_precip(item['fcstValue'])
|
||||||
time_str = f"{time[:2]}:{time[2:]}" # '11:00'
|
time_str = f"{time[:2]}:{time[2:]}" # '11:00'
|
||||||
lines.append(f'<tr><td style="border: 1px #333 solid; padding:0;">{time_str}</td><td style="border: 1px #333 solid; padding:0;">{mm}mm</td></tr>')
|
lines.append(f'<tr><td>{time_str}</td><td>{mm}mm</td></tr>')
|
||||||
total_rainfall += mm
|
total_rainfall += mm
|
||||||
|
|
||||||
|
lines.append('</table></div>')
|
||||||
lines.append("</table></div>")
|
lines.append(f'<p style="text-align:center;">영업시간 중 총 예상 강수량: <strong>{total_rainfall:.1f}mm</strong></p>')
|
||||||
lines.append(f"<p>영업시간 중 총 예상 강수량: <strong>{total_rainfall:.1f}mm</strong></p>")
|
|
||||||
|
|
||||||
return ''.join(lines)
|
return ''.join(lines)
|
||||||
|
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return "⚠️ 응답이 JSON 형식이 아닙니다."
|
if retry:
|
||||||
|
print("⚠️ JSON 디코드 오류 발생, 재시도 중...")
|
||||||
|
return get_precipitation_summary(retry=False)
|
||||||
|
else:
|
||||||
|
return "⚠️ 응답이 JSON 형식이 아닙니다."
|
||||||
|
|
||||||
# 테스트용
|
# 테스트용
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user