tmp
#1
by suil0109 - opened
app.py
CHANGED
|
@@ -25,6 +25,10 @@ last_update_time = None
|
|
| 25 |
update_thread = None
|
| 26 |
is_updating = False
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def get_claude_client():
|
| 29 |
"""Claude API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν"""
|
| 30 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
|
@@ -48,52 +52,88 @@ def install_browsers():
|
|
| 48 |
print(f"λΈλΌμ°μ μ€μΉ μ€ν¨: {e}")
|
| 49 |
return False
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
"""
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
-
print(f"
|
| 83 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def capture_google_trends() -> Optional[Image.Image]:
|
| 87 |
-
"""Google Trends μ€ν¬λ¦°μ· μΊ‘μ² (
|
| 88 |
-
playwright = None
|
| 89 |
-
browser = None
|
| 90 |
page = None
|
| 91 |
|
| 92 |
try:
|
| 93 |
print("Google Trends μ€ν¬λ¦°μ· μμ...")
|
| 94 |
-
|
| 95 |
-
# μ€λ λλ³ λ
립 λΈλΌμ°μ μμ±
|
| 96 |
-
playwright, browser = create_browser_for_thread()
|
| 97 |
if not browser:
|
| 98 |
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
|
| 99 |
return None
|
|
@@ -166,34 +206,19 @@ def capture_google_trends() -> Optional[Image.Image]:
|
|
| 166 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 167 |
return None
|
| 168 |
finally:
|
| 169 |
-
# 리μμ€ μ 리
|
| 170 |
if page:
|
| 171 |
try:
|
| 172 |
page.close()
|
| 173 |
except:
|
| 174 |
pass
|
| 175 |
-
if browser:
|
| 176 |
-
try:
|
| 177 |
-
browser.close()
|
| 178 |
-
except:
|
| 179 |
-
pass
|
| 180 |
-
if playwright:
|
| 181 |
-
try:
|
| 182 |
-
playwright.stop()
|
| 183 |
-
except:
|
| 184 |
-
pass
|
| 185 |
|
| 186 |
def capture_ezme_trends() -> Optional[Image.Image]:
|
| 187 |
-
"""rank.ezme.net μ€ν¬λ¦°μ· μΊ‘μ² (
|
| 188 |
-
playwright = None
|
| 189 |
-
browser = None
|
| 190 |
page = None
|
| 191 |
|
| 192 |
try:
|
| 193 |
print("rank.ezme.net μ€ν¬λ¦°μ· μμ...")
|
| 194 |
-
|
| 195 |
-
# μ€λ λλ³ λ
립 λΈλΌμ°μ μμ±
|
| 196 |
-
playwright, browser = create_browser_for_thread()
|
| 197 |
if not browser:
|
| 198 |
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
|
| 199 |
return None
|
|
@@ -250,22 +275,11 @@ def capture_ezme_trends() -> Optional[Image.Image]:
|
|
| 250 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 251 |
return None
|
| 252 |
finally:
|
| 253 |
-
# 리μμ€ μ 리
|
| 254 |
if page:
|
| 255 |
try:
|
| 256 |
page.close()
|
| 257 |
except:
|
| 258 |
pass
|
| 259 |
-
if browser:
|
| 260 |
-
try:
|
| 261 |
-
browser.close()
|
| 262 |
-
except:
|
| 263 |
-
pass
|
| 264 |
-
if playwright:
|
| 265 |
-
try:
|
| 266 |
-
playwright.stop()
|
| 267 |
-
except:
|
| 268 |
-
pass
|
| 269 |
|
| 270 |
def analyze_google_trends_with_claude(image: Image.Image) -> str:
|
| 271 |
"""Google Trends μ΄λ―Έμ§ Claude λΆμ"""
|
|
@@ -449,6 +463,21 @@ def combine_analysis_results(google_analysis: str, ezme_analysis: str) -> str:
|
|
| 449 |
|
| 450 |
{ezme_analysis}
|
| 451 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
return combined_result
|
| 454 |
|
|
@@ -723,6 +752,9 @@ def main():
|
|
| 723 |
else:
|
| 724 |
print("Claude API ν€ νμΈλ¨")
|
| 725 |
|
|
|
|
|
|
|
|
|
|
| 726 |
# μ΄κΈ° ν΅ν© λΆμ μν
|
| 727 |
print("μ΄κΈ° ν΅ν© λΆμ μμ...")
|
| 728 |
initial_success, _ = perform_parallel_analysis()
|
|
@@ -754,6 +786,8 @@ def main():
|
|
| 754 |
except Exception as e:
|
| 755 |
print(f"μΈν°νμ΄μ€ μμ μ€ν¨: {e}")
|
| 756 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
|
|
|
|
|
|
| 757 |
|
| 758 |
if __name__ == "__main__":
|
| 759 |
main()
|
|
|
|
| 25 |
update_thread = None
|
| 26 |
is_updating = False
|
| 27 |
|
| 28 |
+
# μ μ λΈλΌμ°μ μΈμ€ν΄μ€
|
| 29 |
+
global_playwright = None
|
| 30 |
+
global_browser = None
|
| 31 |
+
|
| 32 |
def get_claude_client():
|
| 33 |
"""Claude API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν"""
|
| 34 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
|
|
|
| 52 |
print(f"λΈλΌμ°μ μ€μΉ μ€ν¨: {e}")
|
| 53 |
return False
|
| 54 |
|
| 55 |
+
def get_shared_browser():
|
| 56 |
+
"""곡μ λΈλΌμ°μ μΈμ€ν΄μ€ λ°ν (μ¬μ¬μ© μ΅μ ν + μμ μ± κ°ν)"""
|
| 57 |
+
global global_playwright, global_browser
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
+
# Playwright μΈμ€ν΄μ€ μ΄κΈ°ν
|
| 61 |
+
if global_playwright is None:
|
| 62 |
+
print("Playwright μΈμ€ν΄μ€ μ΄κΈ°ν...")
|
| 63 |
+
global_playwright = sync_playwright().start()
|
| 64 |
+
print("Playwright μ΄κΈ°ν μλ£")
|
| 65 |
+
|
| 66 |
+
# λΈλΌμ°μ μΈμ€ν΄μ€ νμΈ λ° μμ±
|
| 67 |
+
if global_browser is None or not global_browser.is_connected():
|
| 68 |
+
print("λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μ€...")
|
| 69 |
+
|
| 70 |
+
# λΈλΌμ°μ μ€ν μ΅μ
|
| 71 |
+
browser_args = [
|
| 72 |
+
'--no-sandbox',
|
| 73 |
+
'--disable-dev-shm-usage',
|
| 74 |
+
'--disable-gpu',
|
| 75 |
+
'--disable-extensions',
|
| 76 |
+
'--disable-web-security',
|
| 77 |
+
'--disable-blink-features=AutomationControlled',
|
| 78 |
+
'--disable-background-timer-throttling',
|
| 79 |
+
'--disable-backgrounding-occluded-windows',
|
| 80 |
+
'--disable-renderer-backgrounding',
|
| 81 |
+
'--disable-features=TranslateUI',
|
| 82 |
+
'--disable-ipc-flooding-protection'
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
global_browser = global_playwright.chromium.launch(
|
| 86 |
+
headless=True,
|
| 87 |
+
args=browser_args
|
| 88 |
+
)
|
| 89 |
+
print("λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μλ£")
|
| 90 |
+
|
| 91 |
+
# μ°κ²° μν μ¬νμΈ
|
| 92 |
+
if global_browser.is_connected():
|
| 93 |
+
print("λΈλΌμ°μ μ°κ²° μν μνΈ")
|
| 94 |
+
return global_browser
|
| 95 |
+
else:
|
| 96 |
+
print("λΈλΌμ°μ μ°κ²° μν λΆλ, μ¬μμ± μλ")
|
| 97 |
+
global_browser = None
|
| 98 |
+
return get_shared_browser() # μ¬κ· νΈμΆλ‘ μ¬μμ±
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
+
print(f"λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μ€ν¨: {str(e)}")
|
| 102 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 103 |
+
|
| 104 |
+
# μ€ν¨μ μ 리νκ³ None λ°ν
|
| 105 |
+
try:
|
| 106 |
+
if global_browser:
|
| 107 |
+
global_browser.close()
|
| 108 |
+
if global_playwright:
|
| 109 |
+
global_playwright.stop()
|
| 110 |
+
except:
|
| 111 |
+
pass
|
| 112 |
+
|
| 113 |
+
global_browser = None
|
| 114 |
+
global_playwright = None
|
| 115 |
+
return None
|
| 116 |
+
|
| 117 |
+
def cleanup_browser():
|
| 118 |
+
"""λΈλΌμ°μ μ 리"""
|
| 119 |
+
global global_playwright, global_browser
|
| 120 |
+
try:
|
| 121 |
+
if global_browser:
|
| 122 |
+
global_browser.close()
|
| 123 |
+
global_browser = None
|
| 124 |
+
if global_playwright:
|
| 125 |
+
global_playwright.stop()
|
| 126 |
+
global_playwright = None
|
| 127 |
+
except:
|
| 128 |
+
pass
|
| 129 |
|
| 130 |
def capture_google_trends() -> Optional[Image.Image]:
|
| 131 |
+
"""Google Trends μ€ν¬λ¦°μ· μΊ‘μ² (λμ λ‘λ© λμ)"""
|
|
|
|
|
|
|
| 132 |
page = None
|
| 133 |
|
| 134 |
try:
|
| 135 |
print("Google Trends μ€ν¬λ¦°μ· μμ...")
|
| 136 |
+
browser = get_shared_browser()
|
|
|
|
|
|
|
| 137 |
if not browser:
|
| 138 |
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
|
| 139 |
return None
|
|
|
|
| 206 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 207 |
return None
|
| 208 |
finally:
|
|
|
|
| 209 |
if page:
|
| 210 |
try:
|
| 211 |
page.close()
|
| 212 |
except:
|
| 213 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
def capture_ezme_trends() -> Optional[Image.Image]:
|
| 216 |
+
"""rank.ezme.net μ€ν¬λ¦°μ· μΊ‘μ² (λμ λ‘λ© λμ)"""
|
|
|
|
|
|
|
| 217 |
page = None
|
| 218 |
|
| 219 |
try:
|
| 220 |
print("rank.ezme.net μ€ν¬λ¦°μ· μμ...")
|
| 221 |
+
browser = get_shared_browser()
|
|
|
|
|
|
|
| 222 |
if not browser:
|
| 223 |
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
|
| 224 |
return None
|
|
|
|
| 275 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 276 |
return None
|
| 277 |
finally:
|
|
|
|
| 278 |
if page:
|
| 279 |
try:
|
| 280 |
page.close()
|
| 281 |
except:
|
| 282 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
def analyze_google_trends_with_claude(image: Image.Image) -> str:
|
| 285 |
"""Google Trends μ΄λ―Έμ§ Claude λΆμ"""
|
|
|
|
| 463 |
|
| 464 |
{ezme_analysis}
|
| 465 |
"""
|
| 466 |
+
|
| 467 |
+
# ---
|
| 468 |
+
|
| 469 |
+
# ## μ’
ν© λΆμ λ° μΈμ¬μ΄νΈ
|
| 470 |
+
|
| 471 |
+
# ### μ£Όμ κ³΅ν΅ μ΄μ
|
| 472 |
+
# - Google Trendsμ νκ΅ ν¬νΈμμ 곡ν΅μ μΌλ‘ μμκΆμ λνλλ κ²μμ΄λ€μ ν΅ν΄ νμ¬ κ°μ₯ λ¨κ±°μ΄ μ΄μλ₯Ό νμ
ν μ μμ΅λλ€.
|
| 473 |
+
|
| 474 |
+
# ### νλ«νΌλ³ νΉμ±
|
| 475 |
+
# - **Google Trends**: μ μΈκ³μ κ΄μ¬μ¬μ μ₯κΈ°μ νΈλ λ λ°μ
|
| 476 |
+
# - **νκ΅ ν¬νΈ (Zum, Nate, Google)**: νκ΅ μ¬μ©μμ μ€μκ° κ΄μ¬μ¬μ μ¦μμ± μλ μ΄μ λ°μ
|
| 477 |
+
|
| 478 |
+
# ### μκ°λλ³ κ²μ ν¨ν΄
|
| 479 |
+
# - κ° μκ°λλ³λ‘ λνλλ κ²μμ΄ λ³νλ₯Ό ν΅ν΄ νκ΅ μ¬μ©μλ€μ μΌμ ν¨ν΄κ³Ό κ΄μ¬μ¬ λ³νλ₯Ό κ΄μ°°ν μ μμ΅λλ€.
|
| 480 |
+
|
| 481 |
|
| 482 |
return combined_result
|
| 483 |
|
|
|
|
| 752 |
else:
|
| 753 |
print("Claude API ν€ νμΈλ¨")
|
| 754 |
|
| 755 |
+
# μ’
λ£μ λΈλΌμ°μ μ 리
|
| 756 |
+
atexit.register(cleanup_browser)
|
| 757 |
+
|
| 758 |
# μ΄κΈ° ν΅ν© λΆμ μν
|
| 759 |
print("μ΄κΈ° ν΅ν© λΆμ μμ...")
|
| 760 |
initial_success, _ = perform_parallel_analysis()
|
|
|
|
| 786 |
except Exception as e:
|
| 787 |
print(f"μΈν°νμ΄μ€ μμ μ€ν¨: {e}")
|
| 788 |
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 789 |
+
finally:
|
| 790 |
+
cleanup_browser()
|
| 791 |
|
| 792 |
if __name__ == "__main__":
|
| 793 |
main()
|