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()
|