Update app.py
Browse files
app.py
CHANGED
|
@@ -15,20 +15,21 @@ from selenium.common.exceptions import TimeoutException, NoSuchElementException
|
|
| 15 |
from concurrent.futures import ThreadPoolExecutor
|
| 16 |
import threading
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Claude API ์ค์
|
| 23 |
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 24 |
|
| 25 |
-
if not ANTHROPIC_API_KEY:
|
| 26 |
-
print("ANTHROPIC_API_KEY๊ฐ ์ค์ ๋์ง ์์, Secret ํ์ธ ํ์")
|
| 27 |
-
else:
|
| 28 |
-
print("Claude API ํค ํ์ธ ์๋ฃ")
|
| 29 |
-
|
| 30 |
-
# Claude ํด๋ผ์ด์ธํธ๋ฅผ ์์ฑ
|
| 31 |
def create_claude_client():
|
|
|
|
| 32 |
try:
|
| 33 |
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
|
| 34 |
print("Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์ฑ๊ณต")
|
|
@@ -37,36 +38,25 @@ def create_claude_client():
|
|
| 37 |
print(f"Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์คํจ: {e}")
|
| 38 |
return None
|
| 39 |
|
| 40 |
-
# Google Trends ์๋ํ ํด๋์ค
|
| 41 |
class GoogleTrendsAutomator:
|
|
|
|
|
|
|
| 42 |
def __init__(self):
|
| 43 |
self.driver = None
|
| 44 |
-
|
| 45 |
-
# Chrome ๋๋ผ์ด๋ฒ ์ค์
|
| 46 |
def setup_driver(self):
|
|
|
|
| 47 |
options = Options()
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if
|
| 51 |
-
|
| 52 |
-
options.add_argument("--headless=new") # ์๋ก์ด ํค๋๋ฆฌ์ค ๋ชจ๋
|
| 53 |
options.add_argument("--no-sandbox")
|
| 54 |
options.add_argument("--disable-dev-shm-usage")
|
| 55 |
options.add_argument("--disable-gpu")
|
| 56 |
-
|
| 57 |
-
options.add_argument("--remote-debugging-port=9222")
|
| 58 |
-
else:
|
| 59 |
options.add_argument("--headless")
|
| 60 |
|
| 61 |
-
# # ํ๊ฒฝ๋ณ ์ค์
|
| 62 |
-
# if os.getenv("SPACE_ID"): # Hugging Face Space
|
| 63 |
-
# options.add_argument("--headless")
|
| 64 |
-
# options.add_argument("--no-sandbox")
|
| 65 |
-
# options.add_argument("--disable-dev-shm-usage")
|
| 66 |
-
# options.add_argument("--disable-gpu")
|
| 67 |
-
# else: # ๋ก์ปฌ ํ๊ฒฝ
|
| 68 |
-
# options.add_argument("--headless")
|
| 69 |
-
|
| 70 |
# ์๋ ์ต์ ํ ์ค์
|
| 71 |
options.add_argument("--window-size=1280,720") # ํด์๋ ์ค์
|
| 72 |
options.add_argument("--disable-blink-features=AutomationControlled")
|
|
@@ -85,8 +75,7 @@ class GoogleTrendsAutomator:
|
|
| 85 |
|
| 86 |
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 87 |
options.add_argument("--memory-pressure-off")
|
| 88 |
-
|
| 89 |
-
options.add_argument("--max_old_space_size=2048")# ๋ฉ๋ชจ๋ฆฌ ์ ํ
|
| 90 |
|
| 91 |
# ์ด๋ฏธ์ง/CSS ๋นํ์ฑํ๋ก ๋ก๋ฉ ์๋ ํฅ์
|
| 92 |
prefs = {
|
|
@@ -105,66 +94,34 @@ class GoogleTrendsAutomator:
|
|
| 105 |
options.add_argument("--accept-lang=ko-KR,ko;q=0.9,en;q=0.8")
|
| 106 |
|
| 107 |
try:
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
service = Service(ChromeDriverManager().install())
|
| 116 |
-
self.driver = webdriver.Chrome(service=service, options=options)
|
| 117 |
-
print("ChromeDriver ์ค์น ๋ฐ ์ด๊ธฐํ ์ฑ๊ณต")
|
| 118 |
-
|
| 119 |
-
except Exception as e:
|
| 120 |
-
print(f"webdriver-manager ์ค์น ์คํจ: {e}")
|
| 121 |
-
# ์์คํ
ChromeDriver ์๋
|
| 122 |
-
try:
|
| 123 |
-
self.driver = webdriver.Chrome(options=options)
|
| 124 |
-
print("์์คํ
ChromeDriver ์ฌ์ฉ ์ฑ๊ณต")
|
| 125 |
-
except Exception as sys_e:
|
| 126 |
-
print(f"์์คํ
ChromeDriver๋ ์คํจ: {sys_e}")
|
| 127 |
-
return False
|
| 128 |
-
else:
|
| 129 |
-
# ๋ก์ปฌ ํ๊ฒฝ
|
| 130 |
-
try:
|
| 131 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 132 |
-
from selenium.webdriver.chrome.service import Service
|
| 133 |
-
|
| 134 |
-
service = Service(ChromeDriverManager().install())
|
| 135 |
-
self.driver = webdriver.Chrome(service=service, options=options)
|
| 136 |
-
print("๋ก์ปฌ ChromeDriver ์ค์น ์ฑ๊ณต")
|
| 137 |
-
except:
|
| 138 |
-
self.driver = webdriver.Chrome(options=options)
|
| 139 |
-
print("๋ก์ปฌ ์์คํ
ChromeDriver ์ฌ์ฉ")
|
| 140 |
-
# # ์ฌ๋ฌ ๋ฐฉ๋ฒ์ผ๋ก ChromeDriver ์๋
|
| 141 |
-
# driver_created = False
|
| 142 |
-
|
| 143 |
-
# # ๋ฐฉ๋ฒ 1: webdriver-manager ์ฌ์ฉ
|
| 144 |
-
# try:
|
| 145 |
-
# from webdriver_manager.chrome import ChromeDriverManager
|
| 146 |
-
# from selenium.webdriver.chrome.service import Service
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
# ํ์์์ ์ค์ (์๋ ์ต์ ํ)
|
| 170 |
self.driver.set_page_load_timeout(15) # ํ์ด์ง ๋ก๋ฉ ํ์์์ 15์ด
|
|
@@ -186,8 +143,8 @@ class GoogleTrendsAutomator:
|
|
| 186 |
print(f"๋๋ผ์ด๋ฒ ์ค์ ์ต์ข
์คํจ: {e}")
|
| 187 |
return False
|
| 188 |
|
| 189 |
-
# ๋น ๋ฅด๊ฒ Element ์ฐพ๊ธฐ, Timeout ๋จ์ถ ๊ณ ๋ ค
|
| 190 |
def safe_find_element(self, selectors: list, timeout: int = 3):
|
|
|
|
| 191 |
for selector_type, selector in selectors:
|
| 192 |
try:
|
| 193 |
if selector_type == "xpath":
|
|
@@ -209,8 +166,8 @@ class GoogleTrendsAutomator:
|
|
| 209 |
continue
|
| 210 |
return None
|
| 211 |
|
| 212 |
-
# ์ฆ์ ํด๋ฆญ
|
| 213 |
def safe_click(self, selectors: list, timeout: int = 3) -> bool:
|
|
|
|
| 214 |
element = self.safe_find_element(selectors, timeout)
|
| 215 |
if element:
|
| 216 |
try:
|
|
@@ -225,8 +182,8 @@ class GoogleTrendsAutomator:
|
|
| 225 |
return False
|
| 226 |
return False
|
| 227 |
|
| 228 |
-
# ๋ณ๋ ฌ๋ก ์ค์ ๋ณ๊ฒฝ ์๋
|
| 229 |
def parallel_change_settings(self, region: str, period: str, category: str, progress_callback=None) -> dict:
|
|
|
|
| 230 |
results = {
|
| 231 |
'region': False,
|
| 232 |
'period': False,
|
|
@@ -265,8 +222,8 @@ class GoogleTrendsAutomator:
|
|
| 265 |
|
| 266 |
return results
|
| 267 |
|
| 268 |
-
# ์ง์ญ ๋ณ๊ฒฝ
|
| 269 |
def change_region(self, target_region: str) -> bool:
|
|
|
|
| 270 |
try:
|
| 271 |
region_selectors = [
|
| 272 |
("xpath", "//button[@aria-label='๋ํ๋ฏผ๊ตญ, ์์น ์ ํ']"),
|
|
@@ -302,8 +259,8 @@ class GoogleTrendsAutomator:
|
|
| 302 |
print(f"์ง์ญ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 303 |
return False
|
| 304 |
|
| 305 |
-
# ๊ธฐ๊ฐ ๋ณ๊ฒฝ
|
| 306 |
def change_time_period(self, target_period: str) -> bool:
|
|
|
|
| 307 |
try:
|
| 308 |
period_selectors = [
|
| 309 |
("xpath", "//button[contains(@aria-label, '๊ธฐ๊ฐ ์ ํ')]"),
|
|
@@ -338,8 +295,8 @@ class GoogleTrendsAutomator:
|
|
| 338 |
print(f"๊ธฐ๊ฐ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 339 |
return False
|
| 340 |
|
| 341 |
-
# ์นดํ
๊ณ ๋ฆฌ ๏ฟฝ๏ฟฝ๊ฒฝ
|
| 342 |
def change_category(self, target_category: str) -> bool:
|
|
|
|
| 343 |
try:
|
| 344 |
category_selectors = [
|
| 345 |
("xpath", "//button[contains(@aria-label, '์นดํ
๊ณ ๋ฆฌ ์ ํ')]"),
|
|
@@ -377,8 +334,8 @@ class GoogleTrendsAutomator:
|
|
| 377 |
print(f"์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 378 |
return False
|
| 379 |
|
| 380 |
-
# Google Trends ์บก์ฒ
|
| 381 |
def capture_trends(self, region: str, period: str, category: str, progress_callback=None) -> Tuple[Optional[str], bool, str]:
|
|
|
|
| 382 |
error_msg = ""
|
| 383 |
|
| 384 |
try:
|
|
@@ -446,8 +403,8 @@ class GoogleTrendsAutomator:
|
|
| 446 |
if self.driver:
|
| 447 |
self.driver.quit()
|
| 448 |
|
| 449 |
-
# Claude API๋ก ์คํฌ๋ฆฐ์ท ๋ถ์
|
| 450 |
def analyze_with_claude(screenshot_b64: str, region: str, period: str, category: str) -> str:
|
|
|
|
| 451 |
try:
|
| 452 |
# API ํค ์ฒดํฌ
|
| 453 |
if not ANTHROPIC_API_KEY or ANTHROPIC_API_KEY == "your_api_key_here":
|
|
|
|
| 15 |
from concurrent.futures import ThreadPoolExecutor
|
| 16 |
import threading
|
| 17 |
|
| 18 |
+
# .env ํ์ผ ๋ก๋ (์๋ ๊ฒฝ์ฐ)
|
| 19 |
+
try:
|
| 20 |
+
from dotenv import load_dotenv
|
| 21 |
+
load_dotenv()
|
| 22 |
+
print(".env ํ์ผ ๋ก๋ ์๋ฃ")
|
| 23 |
+
except ImportError:
|
| 24 |
+
print("python-dotenv๊ฐ ์ค์น๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ์๋์ผ๋ก ์ค์ ํด์ฃผ์ธ์.")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f".env ํ์ผ ๋ก๋ ์ค ์ค๋ฅ: {e}")
|
| 27 |
|
| 28 |
+
# Claude API ์ค์
|
| 29 |
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def create_claude_client():
|
| 32 |
+
"""Claude ํด๋ผ์ด์ธํธ๋ฅผ ์์ฑํ๋ ํจ์ (๋น ๋ฅธ ๋ฒ์ )"""
|
| 33 |
try:
|
| 34 |
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
|
| 35 |
print("Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์ฑ๊ณต")
|
|
|
|
| 38 |
print(f"Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์คํจ: {e}")
|
| 39 |
return None
|
| 40 |
|
|
|
|
| 41 |
class GoogleTrendsAutomator:
|
| 42 |
+
"""์ต์ ํ๋ Google Trends ์๋ํ ํด๋์ค"""
|
| 43 |
+
|
| 44 |
def __init__(self):
|
| 45 |
self.driver = None
|
| 46 |
+
|
|
|
|
| 47 |
def setup_driver(self):
|
| 48 |
+
"""์ด๊ณ ์ Chrome ๋๋ผ์ด๋ฒ ์ค์ """
|
| 49 |
options = Options()
|
| 50 |
|
| 51 |
+
# ํ๊ฒฝ๋ณ ์ค์
|
| 52 |
+
if os.getenv("SPACE_ID"): # Hugging Face Space
|
| 53 |
+
options.add_argument("--headless")
|
|
|
|
| 54 |
options.add_argument("--no-sandbox")
|
| 55 |
options.add_argument("--disable-dev-shm-usage")
|
| 56 |
options.add_argument("--disable-gpu")
|
| 57 |
+
else: # ๋ก์ปฌ ํ๊ฒฝ
|
|
|
|
|
|
|
| 58 |
options.add_argument("--headless")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# ์๋ ์ต์ ํ ์ค์
|
| 61 |
options.add_argument("--window-size=1280,720") # ํด์๋ ์ค์
|
| 62 |
options.add_argument("--disable-blink-features=AutomationControlled")
|
|
|
|
| 75 |
|
| 76 |
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 77 |
options.add_argument("--memory-pressure-off")
|
| 78 |
+
options.add_argument("--max_old_space_size=4096")
|
|
|
|
| 79 |
|
| 80 |
# ์ด๋ฏธ์ง/CSS ๋นํ์ฑํ๋ก ๋ก๋ฉ ์๋ ํฅ์
|
| 81 |
prefs = {
|
|
|
|
| 94 |
options.add_argument("--accept-lang=ko-KR,ko;q=0.9,en;q=0.8")
|
| 95 |
|
| 96 |
try:
|
| 97 |
+
# ์ฌ๋ฌ ๋ฐฉ๋ฒ์ผ๋ก ChromeDriver ์๋
|
| 98 |
+
driver_created = False
|
| 99 |
+
|
| 100 |
+
# ๋ฐฉ๋ฒ 1: webdriver-manager ์ฌ์ฉ
|
| 101 |
+
try:
|
| 102 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 103 |
+
from selenium.webdriver.chrome.service import Service
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
service = Service(ChromeDriverManager().install())
|
| 106 |
+
self.driver = webdriver.Chrome(service=service, options=options)
|
| 107 |
+
print("webdriver-manager๋ก ChromeDriver ์๋ ์ค์น ์ฑ๊ณต")
|
| 108 |
+
driver_created = True
|
| 109 |
|
| 110 |
+
except Exception as wm_error:
|
| 111 |
+
print(f"webdriver-manager ์คํจ: {wm_error}")
|
| 112 |
+
|
| 113 |
+
# ๋ฐฉ๋ฒ 2: ์์คํ
ChromeDriver ์ฌ์ฉ
|
| 114 |
+
if not driver_created:
|
| 115 |
+
try:
|
| 116 |
+
self.driver = webdriver.Chrome(options=options)
|
| 117 |
+
print("์์คํ
ChromeDriver ์ฌ์ฉ ์ฑ๊ณต")
|
| 118 |
+
driver_created = True
|
| 119 |
+
except Exception as sys_error:
|
| 120 |
+
print(f"์์คํ
ChromeDriver ์คํจ: {sys_error}")
|
| 121 |
+
|
| 122 |
+
if not driver_created:
|
| 123 |
+
print("๋ชจ๋ ChromeDriver ์ด๊ธฐํ ๋ฐฉ๋ฒ ์คํจ")
|
| 124 |
+
return False
|
| 125 |
|
| 126 |
# ํ์์์ ์ค์ (์๋ ์ต์ ํ)
|
| 127 |
self.driver.set_page_load_timeout(15) # ํ์ด์ง ๋ก๋ฉ ํ์์์ 15์ด
|
|
|
|
| 143 |
print(f"๋๋ผ์ด๋ฒ ์ค์ ์ต์ข
์คํจ: {e}")
|
| 144 |
return False
|
| 145 |
|
|
|
|
| 146 |
def safe_find_element(self, selectors: list, timeout: int = 3):
|
| 147 |
+
"""์ด๊ณ ์ ์์ ์ฐพ๊ธฐ (ํ์์์ ๋จ์ถ)"""
|
| 148 |
for selector_type, selector in selectors:
|
| 149 |
try:
|
| 150 |
if selector_type == "xpath":
|
|
|
|
| 166 |
continue
|
| 167 |
return None
|
| 168 |
|
|
|
|
| 169 |
def safe_click(self, selectors: list, timeout: int = 3) -> bool:
|
| 170 |
+
"""์ฆ์ ํด๋ฆญ (๋๊ธฐ ์๊ฐ ์ต์ํ)"""
|
| 171 |
element = self.safe_find_element(selectors, timeout)
|
| 172 |
if element:
|
| 173 |
try:
|
|
|
|
| 182 |
return False
|
| 183 |
return False
|
| 184 |
|
|
|
|
| 185 |
def parallel_change_settings(self, region: str, period: str, category: str, progress_callback=None) -> dict:
|
| 186 |
+
"""๋ณ๋ ฌ๋ก ์ค์ ๋ณ๊ฒฝ ์๋ (์คํ์ )"""
|
| 187 |
results = {
|
| 188 |
'region': False,
|
| 189 |
'period': False,
|
|
|
|
| 222 |
|
| 223 |
return results
|
| 224 |
|
|
|
|
| 225 |
def change_region(self, target_region: str) -> bool:
|
| 226 |
+
"""์ด๊ณ ์ ์ง์ญ ๋ณ๊ฒฝ"""
|
| 227 |
try:
|
| 228 |
region_selectors = [
|
| 229 |
("xpath", "//button[@aria-label='๋ํ๋ฏผ๊ตญ, ์์น ์ ํ']"),
|
|
|
|
| 259 |
print(f"์ง์ญ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 260 |
return False
|
| 261 |
|
|
|
|
| 262 |
def change_time_period(self, target_period: str) -> bool:
|
| 263 |
+
"""์ด๊ณ ์ ๊ธฐ๊ฐ ๋ณ๊ฒฝ"""
|
| 264 |
try:
|
| 265 |
period_selectors = [
|
| 266 |
("xpath", "//button[contains(@aria-label, '๊ธฐ๊ฐ ์ ํ')]"),
|
|
|
|
| 295 |
print(f"๊ธฐ๊ฐ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 296 |
return False
|
| 297 |
|
|
|
|
| 298 |
def change_category(self, target_category: str) -> bool:
|
| 299 |
+
"""์ด๊ณ ์ ์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ"""
|
| 300 |
try:
|
| 301 |
category_selectors = [
|
| 302 |
("xpath", "//button[contains(@aria-label, '์นดํ
๊ณ ๋ฆฌ ์ ํ')]"),
|
|
|
|
| 334 |
print(f"์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 335 |
return False
|
| 336 |
|
|
|
|
| 337 |
def capture_trends(self, region: str, period: str, category: str, progress_callback=None) -> Tuple[Optional[str], bool, str]:
|
| 338 |
+
"""Google Trends ์บก์ฒ (์ต์ ํ๋ ๋ฒ์ )"""
|
| 339 |
error_msg = ""
|
| 340 |
|
| 341 |
try:
|
|
|
|
| 403 |
if self.driver:
|
| 404 |
self.driver.quit()
|
| 405 |
|
|
|
|
| 406 |
def analyze_with_claude(screenshot_b64: str, region: str, period: str, category: str) -> str:
|
| 407 |
+
"""Claude API๋ก ์คํฌ๋ฆฐ์ท ๋ถ์ (์ต์ ํ๋ ๋ฒ์ )"""
|
| 408 |
try:
|
| 409 |
# API ํค ์ฒดํฌ
|
| 410 |
if not ANTHROPIC_API_KEY or ANTHROPIC_API_KEY == "your_api_key_here":
|