openfree commited on
Commit
d1f2bdd
Β·
verified Β·
1 Parent(s): e5802a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -54
app.py CHANGED
@@ -48,19 +48,24 @@ TIMEZONE_CITIES = {
48
  "베이컀 섬 (UTC-12)": -12,
49
  }
50
 
51
- def convert_to_seoul_time(year, month, day, hour, minute, timezone_name):
52
  """
53
  μ„ νƒλœ μ‹œκ°„λŒ€μ˜ μ‹œκ°„μ„ μ„œμšΈ μ‹œκ°„μœΌλ‘œ λ³€ν™˜
54
  """
55
  try:
56
- # μ‹œκ°„λŒ€ μ˜€ν”„μ…‹ κ°€μ Έμ˜€κΈ°
57
- timezone_offset = TIMEZONE_CITIES[timezone_name]
 
 
 
 
 
58
 
59
  # μ„œμšΈκ³Όμ˜ μ‹œμ°¨ 계산 (μ„œμšΈμ€ UTC+9)
60
  time_difference = 9 - timezone_offset
61
 
62
  # μž…λ ₯ μ‹œκ°„ 생성
63
- local_time = datetime(int(year), int(month), int(day), int(hour), int(minute))
64
 
65
  # μ‹œμ°¨ 적용
66
  seoul_time = local_time + timedelta(hours=time_difference)
@@ -72,7 +77,59 @@ def convert_to_seoul_time(year, month, day, hour, minute, timezone_name):
72
  'hour': f"{seoul_time.hour:02d}{seoul_time.minute:02d}"
73
  }
74
  except Exception as e:
75
- raise Exception(f"μ‹œκ°„ λ³€ν™˜ 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
 
78
  def exception_handler(exception_type, exception, traceback):
@@ -170,55 +227,7 @@ def calculate_siju(birth_hour, lunIljin):
170
  return siju
171
 
172
 
173
- def get_lunar_info(solYear, solMonth, solDay, solHour, timezone_name):
174
- try:
175
- # μž…λ ₯κ°’ 검증
176
- if not all([solYear, solMonth, solDay, solHour, timezone_name]):
177
- raise ValueError("λͺ¨λ“  ν•„λ“œλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.")
178
-
179
- # μ‹œκ°„ νŒŒμ‹±
180
- hour = int(solHour[:2])
181
- minute = int(solHour[2:]) if len(solHour) > 2 else 0
182
-
183
- # μ‹œκ°„ λ²”μœ„ 검증
184
- if not (0 <= hour <= 23) or not (0 <= minute <= 59):
185
- raise ValueError("μ˜¬λ°”λ₯Έ μ‹œκ°„ ν˜•μ‹μ΄ μ•„λ‹™λ‹ˆλ‹€.")
186
-
187
- # μ„ νƒλœ μ‹œκ°„λŒ€μ—μ„œ μ„œμšΈ μ‹œκ°„μœΌλ‘œ λ³€ν™˜
188
- seoul_time = convert_to_seoul_time(solYear, solMonth, solDay, hour, minute, timezone_name)
189
-
190
-
191
- base_url = "http://apis.data.go.kr/B090041/openapi/service/LrsrCldInfoService/getLunCalInfo"
192
- service_key = "EuBjt%2B2t0KzYt5Ri%2Bzs56oymt9kHJ8uPIOV7m6UMKnOV8cpwj02EiRqMEudX%2F%2FVTKdMnwaLH0igRwY57YUplEQ%3D%3D"
193
-
194
- url = f"{base_url}?solYear={seoul_time['year']}&solMonth={seoul_time['month']}&solDay={seoul_time['day']}&ServiceKey={service_key}"
195
-
196
- response = requests.get(url)
197
- if response.status_code == 200:
198
- root = ElementTree.fromstring(response.content)
199
-
200
- lunIljin = root.find(".//lunIljin").text if root.find(".//lunIljin") is not None else "정보 μ—†μŒ"
201
- lunWolgeon = root.find(".//lunWolgeon").text if root.find(".//lunWolgeon") is not None else "정보 μ—†μŒ"
202
- lunSecha = root.find(".//lunSecha").text if root.find(".//lunSecha") is not None else "정보 μ—†μŒ"
203
-
204
- siju = calculate_siju(seoul_time['hour'], lunIljin)
205
- analysis_html = 사주_뢄석(lunSecha, lunWolgeon, lunIljin, siju)
206
-
207
- # 원본 μ‹œκ°„κ³Ό λ³€ν™˜λœ μ„œμšΈ μ‹œκ°„ 정보λ₯Ό ν¬ν•¨ν•œ κ²°κ³Ό
208
- result = {
209
- "μž…λ ₯ μ‹œκ°„": f"{timezone_name}: {solYear}λ…„ {solMonth}μ›” {solDay}일 {hour:02d}μ‹œ {minute:02d}λΆ„",
210
- "λ³€ν™˜λœ μ„œμšΈ μ‹œκ°„": f"{seoul_time['year']}λ…„ {seoul_time['month']}μ›” {seoul_time['day']}일 {seoul_time['hour'][:2]}μ‹œ {seoul_time['hour'][2:]}λΆ„",
211
- "λ…„μ£Ό": lunSecha,
212
- "μ›”μ£Ό": lunWolgeon,
213
- "일주": lunIljin,
214
- "μ‹œμ£Ό": siju
215
- }
216
-
217
- return result, lunIljin, analysis_html
218
- else:
219
- return {"Error": "API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."}, "정보 μ—†μŒ", "<p>API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</p>"
220
- except Exception as e:
221
- return {"Error": f"처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"}, "정보 μ—†μŒ", "<p>처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.</p>"
222
 
223
  def analyze_character(lunIljin):
224
  API_URL = "https://api.openai.com/v1/chat/completions"
 
48
  "베이컀 섬 (UTC-12)": -12,
49
  }
50
 
51
+ def convert_to_seoul_time(year, month, day, hour, minute, timezone_offset):
52
  """
53
  μ„ νƒλœ μ‹œκ°„λŒ€μ˜ μ‹œκ°„μ„ μ„œμšΈ μ‹œκ°„μœΌλ‘œ λ³€ν™˜
54
  """
55
  try:
56
+ # μž…λ ₯값을 μ •μˆ˜λ‘œ λ³€ν™˜
57
+ year = int(year)
58
+ month = int(month)
59
+ day = int(day)
60
+ hour = int(hour)
61
+ minute = int(minute)
62
+ timezone_offset = float(timezone_offset) # μŠ¬λΌμ΄λ” 값은 float둜 처리
63
 
64
  # μ„œμšΈκ³Όμ˜ μ‹œμ°¨ 계산 (μ„œμšΈμ€ UTC+9)
65
  time_difference = 9 - timezone_offset
66
 
67
  # μž…λ ₯ μ‹œκ°„ 생성
68
+ local_time = datetime(year, month, day, hour, minute)
69
 
70
  # μ‹œμ°¨ 적용
71
  seoul_time = local_time + timedelta(hours=time_difference)
 
77
  'hour': f"{seoul_time.hour:02d}{seoul_time.minute:02d}"
78
  }
79
  except Exception as e:
80
+ raise Exception(f"μ‹œκ°„ λ³€ν™˜ 쀑 였λ₯˜ λ°œμƒ: {str(e)}\nμž…λ ₯κ°’: year={year}, month={month}, day={day}, hour={hour}, minute={minute}, offset={timezone_offset}")
81
+
82
+ def get_lunar_info(solYear, solMonth, solDay, solHour, timezone_offset):
83
+ try:
84
+ # μž…λ ₯κ°’ 검증
85
+ if not all([solYear, solMonth, solDay, solHour]):
86
+ raise ValueError("λͺ¨λ“  ν•„λ“œλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.")
87
+
88
+ # μ‹œκ°„ νŒŒμ‹±
89
+ try:
90
+ hour = int(solHour[:2])
91
+ minute = int(solHour[2:]) if len(solHour) > 2 else 0
92
+ except ValueError:
93
+ raise ValueError("μ˜¬λ°”λ₯Έ μ‹œκ°„ ν˜•μ‹μ΄ μ•„λ‹™λ‹ˆλ‹€. (예: 1430)")
94
+
95
+ # μ‹œκ°„ λ²”μœ„ 검증
96
+ if not (0 <= hour <= 23) or not (0 <= minute <= 59):
97
+ raise ValueError("μ˜¬λ°”λ₯Έ μ‹œκ°„ λ²”μœ„κ°€ μ•„λ‹™λ‹ˆλ‹€.")
98
+
99
+ # μ„ νƒλœ μ‹œκ°„λŒ€μ—μ„œ μ„œμšΈ μ‹œκ°„μœΌλ‘œ λ³€ν™˜
100
+ seoul_time = convert_to_seoul_time(solYear, solMonth, solDay, hour, minute, timezone_offset)
101
+
102
+ base_url = "http://apis.data.go.kr/B090041/openapi/service/LrsrCldInfoService/getLunCalInfo"
103
+ service_key = "EuBjt%2B2t0KzYt5Ri%2Bzs56oymt9kHJ8uPIOV7m6UMKnOV8cpwj02EiRqMEudX%2F%2FVTKdMnwaLH0igRwY57YUplEQ%3D%3D"
104
+
105
+ url = f"{base_url}?solYear={seoul_time['year']}&solMonth={seoul_time['month']}&solDay={seoul_time['day']}&ServiceKey={service_key}"
106
+
107
+ response = requests.get(url)
108
+ if response.status_code == 200:
109
+ root = ElementTree.fromstring(response.content)
110
+
111
+ lunIljin = root.find(".//lunIljin").text if root.find(".//lunIljin") is not None else "정보 μ—†μŒ"
112
+ lunWolgeon = root.find(".//lunWolgeon").text if root.find(".//lunWolgeon") is not None else "정보 μ—†μŒ"
113
+ lunSecha = root.find(".//lunSecha").text if root.find(".//lunSecha") is not None else "정보 μ—†μŒ"
114
+
115
+ siju = calculate_siju(seoul_time['hour'], lunIljin)
116
+ analysis_html = 사주_뢄석(lunSecha, lunWolgeon, lunIljin, siju)
117
+
118
+ # 원본 μ‹œκ°„κ³Ό λ³€ν™˜λœ μ„œμšΈ μ‹œκ°„ 정보λ₯Ό ν¬ν•¨ν•œ κ²°κ³Ό
119
+ result = {
120
+ "μž…λ ₯ μ‹œκ°„": f"UTC{'+' if timezone_offset > 0 else ''}{timezone_offset}: {solYear}λ…„ {solMonth}μ›” {solDay}일 {hour:02d}μ‹œ {minute:02d}λΆ„",
121
+ "λ³€ν™˜λœ μ„œμšΈ μ‹œκ°„": f"{seoul_time['year']}λ…„ {seoul_time['month']}μ›” {seoul_time['day']}일 {seoul_time['hour'][:2]}μ‹œ {seoul_time['hour'][2:]}λΆ„",
122
+ "λ…„μ£Ό": lunSecha,
123
+ "μ›”μ£Ό": lunWolgeon,
124
+ "일주": lunIljin,
125
+ "μ‹œμ£Ό": siju
126
+ }
127
+
128
+ return result, lunIljin, analysis_html
129
+ else:
130
+ return {"Error": "API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."}, "정보 μ—†μŒ", "<p>API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</p>"
131
+ except Exception as e:
132
+ return {"Error": f"처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"}, "정보 μ—†μŒ", "<p>처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.</p>"
133
 
134
 
135
  def exception_handler(exception_type, exception, traceback):
 
227
  return siju
228
 
229
 
230
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  def analyze_character(lunIljin):
233
  API_URL = "https://api.openai.com/v1/chat/completions"