Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,33 +203,37 @@ def check_network_status():
|
|
| 203 |
def get_weather_data():
|
| 204 |
url = "http://openapi.seoul.go.kr:8088/77544e69764a414d363647424a655a/xml/citydata/1/5/μ λ¦Όμ"
|
| 205 |
try:
|
| 206 |
-
response = requests.get(url, timeout=10)
|
| 207 |
response.raise_for_status()
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
response_text = response.text.strip()
|
| 210 |
if not response_text:
|
| 211 |
-
st.session_state.api_failed = True
|
| 212 |
st.error("μλ΅μ΄ λΉμ΄ μμ΅λλ€.")
|
| 213 |
return None
|
| 214 |
|
|
|
|
|
|
|
|
|
|
| 215 |
data = xmltodict.parse(response_text)
|
| 216 |
result = data['SeoulRtd.citydata']['CITYDATA']['WEATHER_STTS']['WEATHER_STTS']
|
| 217 |
|
| 218 |
-
|
|
|
|
|
|
|
| 219 |
return result
|
| 220 |
|
| 221 |
except requests.exceptions.Timeout:
|
| 222 |
-
st.session_state.api_failed = True
|
| 223 |
st.error("API μμ² μκ°μ΄ μ΄κ³Όλμμ΅λλ€.")
|
| 224 |
except requests.exceptions.RequestException as e:
|
| 225 |
-
st.session_state.api_failed = True
|
| 226 |
st.error(f"API νΈμΆ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 227 |
except Exception as e:
|
| 228 |
-
st.session_state.api_failed = True
|
| 229 |
st.error(f"λ°μ΄ν° μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 230 |
return None
|
| 231 |
|
| 232 |
-
|
| 233 |
def get_background_color(pm10_value):
|
| 234 |
try:
|
| 235 |
pm10 = float(pm10_value)
|
|
@@ -302,22 +306,12 @@ def show_weather_info(data):
|
|
| 302 |
st.markdown('<div class="section-container">', unsafe_allow_html=True)
|
| 303 |
|
| 304 |
# Add update time display using the last API call timestamp (already in KST)
|
| 305 |
-
# show_weather_info ν¨μ λ΄μ refresh time νμ λΆλΆμ μμ
|
| 306 |
refresh_time = datetime.fromtimestamp(st.session_state.last_api_call) if st.session_state.last_api_call else (datetime.utcnow() + timedelta(hours=9))
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
</div>
|
| 313 |
-
''', unsafe_allow_html=True)
|
| 314 |
-
else:
|
| 315 |
-
st.markdown(f'''
|
| 316 |
-
<div style="text-align: center; font-size: 0.8em; color: gray;">
|
| 317 |
-
Data refreshed at: {refresh_time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 318 |
-
</div>
|
| 319 |
-
''', unsafe_allow_html=True)
|
| 320 |
-
|
| 321 |
|
| 322 |
# Add this code to define formatted_date
|
| 323 |
current_time = datetime.utcnow() + timedelta(hours=9)
|
|
@@ -761,9 +755,6 @@ def main():
|
|
| 761 |
st.session_state.current_section = 'weather'
|
| 762 |
st.session_state.last_api_call = 0
|
| 763 |
st.session_state.weather_data = None
|
| 764 |
-
st.session_state.api_failed = False # μ¬κΈ°μ api_failed μ΄κΈ°ν μΆκ°
|
| 765 |
-
|
| 766 |
-
|
| 767 |
|
| 768 |
# νμ¬ μκ°μ μμΈ μκ°μΌλ‘ κ°μ Έμ΅λλ€.
|
| 769 |
current_time = datetime.utcnow() + timedelta(hours=9)
|
|
@@ -786,16 +777,11 @@ def main():
|
|
| 786 |
if new_data: # μ λ°μ΄ν°λ₯Ό μ±κ³΅μ μΌλ‘ λ°μμμ λλ§ μ
λ°μ΄νΈ
|
| 787 |
st.session_state.weather_data = new_data
|
| 788 |
st.session_state.last_api_call = current_timestamp
|
| 789 |
-
st.
|
| 790 |
-
st.rerun()
|
| 791 |
except Exception as e:
|
| 792 |
st.error(f"Failed to refresh data: {str(e)}")
|
| 793 |
-
st.session_state.api_failed = True
|
| 794 |
else:
|
| 795 |
st.warning("νμ¬ λ€νΈμν¬μ λ¬Έμ κ° λ°μνμ΅λλ€. λ°μ΄ν° κ°±μ μ΄ λΆκ°λ₯ν©λλ€.")
|
| 796 |
-
st.session_state.api_failed = True
|
| 797 |
-
if st.session_state.weather_data: # μ΄μ λ°μ΄ν°κ° μμΌλ©΄ κ³μ μ¬μ©
|
| 798 |
-
data = st.session_state.weather_data
|
| 799 |
|
| 800 |
data = st.session_state.weather_data
|
| 801 |
if data:
|
|
|
|
| 203 |
def get_weather_data():
|
| 204 |
url = "http://openapi.seoul.go.kr:8088/77544e69764a414d363647424a655a/xml/citydata/1/5/μ λ¦Όμ"
|
| 205 |
try:
|
| 206 |
+
response = requests.get(url, timeout=10) # timeout μΆκ°
|
| 207 |
response.raise_for_status()
|
| 208 |
|
| 209 |
+
# μλ΅ λ΄μ© λλ²κΉ
|
| 210 |
+
st.write("Status Code:", response.status_code)
|
| 211 |
+
st.write("Response Headers:", dict(response.headers))
|
| 212 |
+
|
| 213 |
response_text = response.text.strip()
|
| 214 |
if not response_text:
|
|
|
|
| 215 |
st.error("μλ΅μ΄ λΉμ΄ μμ΅λλ€.")
|
| 216 |
return None
|
| 217 |
|
| 218 |
+
# XML νμ± μ μλ΅ νμΈ
|
| 219 |
+
st.write("Raw Response:", response_text[:200]) # μ²μ 200μλ§ μΆλ ₯
|
| 220 |
+
|
| 221 |
data = xmltodict.parse(response_text)
|
| 222 |
result = data['SeoulRtd.citydata']['CITYDATA']['WEATHER_STTS']['WEATHER_STTS']
|
| 223 |
|
| 224 |
+
# νμ±λ λ°μ΄ν° νμΈ
|
| 225 |
+
st.write("Parsed Data:", result)
|
| 226 |
+
|
| 227 |
return result
|
| 228 |
|
| 229 |
except requests.exceptions.Timeout:
|
|
|
|
| 230 |
st.error("API μμ² μκ°μ΄ μ΄κ³Όλμμ΅λλ€.")
|
| 231 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 232 |
st.error(f"API νΈμΆ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 233 |
except Exception as e:
|
|
|
|
| 234 |
st.error(f"λ°μ΄ν° μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 235 |
return None
|
| 236 |
|
|
|
|
| 237 |
def get_background_color(pm10_value):
|
| 238 |
try:
|
| 239 |
pm10 = float(pm10_value)
|
|
|
|
| 306 |
st.markdown('<div class="section-container">', unsafe_allow_html=True)
|
| 307 |
|
| 308 |
# Add update time display using the last API call timestamp (already in KST)
|
|
|
|
| 309 |
refresh_time = datetime.fromtimestamp(st.session_state.last_api_call) if st.session_state.last_api_call else (datetime.utcnow() + timedelta(hours=9))
|
| 310 |
+
st.markdown(f'''
|
| 311 |
+
<div style="text-align: center; font-size: 0.8em; color: gray;">
|
| 312 |
+
Data refreshed at: {refresh_time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 313 |
+
</div>
|
| 314 |
+
''', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
# Add this code to define formatted_date
|
| 317 |
current_time = datetime.utcnow() + timedelta(hours=9)
|
|
|
|
| 755 |
st.session_state.current_section = 'weather'
|
| 756 |
st.session_state.last_api_call = 0
|
| 757 |
st.session_state.weather_data = None
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
# νμ¬ μκ°μ μμΈ μκ°μΌλ‘ κ°μ Έμ΅λλ€.
|
| 760 |
current_time = datetime.utcnow() + timedelta(hours=9)
|
|
|
|
| 777 |
if new_data: # μ λ°μ΄ν°λ₯Ό μ±κ³΅μ μΌλ‘ λ°μμμ λλ§ μ
λ°μ΄νΈ
|
| 778 |
st.session_state.weather_data = new_data
|
| 779 |
st.session_state.last_api_call = current_timestamp
|
| 780 |
+
st.rerun() # νμ΄μ§ μλ‘κ³ μΉ¨
|
|
|
|
| 781 |
except Exception as e:
|
| 782 |
st.error(f"Failed to refresh data: {str(e)}")
|
|
|
|
| 783 |
else:
|
| 784 |
st.warning("νμ¬ λ€νΈμν¬μ λ¬Έμ κ° λ°μνμ΅λλ€. λ°μ΄ν° κ°±μ μ΄ λΆκ°λ₯ν©λλ€.")
|
|
|
|
|
|
|
|
|
|
| 785 |
|
| 786 |
data = st.session_state.weather_data
|
| 787 |
if data:
|