File size: 28,976 Bytes
1aa69e4 349ae2c 75152db bf40342 349ae2c bf40342 349ae2c 1aa69e4 75152db 349ae2c 16cf3af 1aa69e4 75152db 1aa69e4 bf40342 349ae2c 75152db 349ae2c 16cf3af 75152db 1aa69e4 6c4649a 75152db 6c4649a 75152db 1603d09 75152db 6c4649a 75152db 6c4649a bf40342 7fe908b 349ae2c bf40342 349ae2c bf40342 6c4649a bf40342 6c4649a bf40342 349ae2c 6c4649a 75152db 6c4649a 75152db bf40342 6c4649a 75152db bf40342 0299c67 bf40342 75152db 98e58ec 75152db bf40342 75152db bf40342 644a47d bf40342 644a47d bf40342 0299c67 75152db bf40342 0299c67 bf40342 75152db bf40342 75152db 0299c67 bf40342 644a47d 75152db 349ae2c bf40342 349ae2c 75152db bf40342 75152db bf40342 75152db bf40342 98e58ec bf40342 75152db bf40342 75152db 1aa69e4 349ae2c 1aa69e4 349ae2c 1aa69e4 75152db bf40342 75152db 349ae2c 16cf3af bf40342 1aa69e4 349ae2c 1aa69e4 bf40342 1aa69e4 bf40342 1aa69e4 bf40342 daf5ca3 bf40342 daf5ca3 bf40342 daf5ca3 bf40342 daf5ca3 bf40342 daf5ca3 bf40342 daf5ca3 bf40342 75152db bf40342 75152db 16cf3af 1603d09 75152db bf40342 75152db 1603d09 bf40342 75152db bf40342 75152db bf40342 75152db bf40342 1603d09 bf40342 75152db bf40342 75152db bf40342 75152db bf40342 75152db bf40342 1603d09 75152db 1603d09 75152db 16cf3af bf40342 75152db 2c476a3 bf40342 75152db 0299c67 bf40342 0299c67 75152db 0299c67 75152db bf40342 75152db bf40342 75152db 1aa69e4 bf40342 1aa69e4 bf40342 75152db bf40342 75152db 1aa69e4 75152db 1aa69e4 bf40342 75152db bf40342 75152db 1aa69e4 75152db 1aa69e4 bf40342 0299c67 1aa69e4 75152db 1aa69e4 75152db bf40342 1aa69e4 75152db bf40342 349ae2c 75152db 0299c67 75152db bf40342 349ae2c 75152db bf40342 75152db bf40342 1aa69e4 0299c67 75152db 16cf3af 75152db 1aa69e4 349ae2c 75152db bf40342 75152db 1aa69e4 1603d09 16cf3af 75152db 349ae2c 16cf3af 75152db 349ae2c bf40342 1603d09 75152db bf40342 75152db 349ae2c 75152db 349ae2c 75152db 1603d09 75152db bf40342 349ae2c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
import os
import time
import base64
import io
import threading
import atexit
import traceback
import asyncio
import concurrent.futures
from datetime import datetime
from typing import Optional, Dict, Any, Tuple
import gradio as gr
import anthropic
from playwright.sync_api import sync_playwright
from PIL import Image
from dotenv import load_dotenv
# νκ²½λ³μ λ‘λ
load_dotenv()
# λΆμ λ°μ΄ν° μ μ₯μ
analyzed_data = {}
last_update_time = None
update_thread = None
is_updating = False
# μ μ λΈλΌμ°μ μΈμ€ν΄μ€
global_playwright = None
global_browser = None
def get_claude_client():
"""Claude API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν"""
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError("ANTHROPIC_API_KEY νκ²½λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
return anthropic.Anthropic(
api_key=api_key,
max_retries=3,
timeout=60.0
)
def install_browsers():
"""Playwright λΈλΌμ°μ μ€μΉ"""
try:
print("Playwright λΈλΌμ°μ μ€μΉ μ€...")
os.system("playwright install chromium --force")
print("λΈλΌμ°μ μ€μΉ μλ£!")
return True
except Exception as e:
print(f"λΈλΌμ°μ μ€μΉ μ€ν¨: {e}")
return False
def get_shared_browser():
"""곡μ λΈλΌμ°μ μΈμ€ν΄μ€ λ°ν (μ¬μ¬μ© μ΅μ ν + μμ μ± κ°ν)"""
global global_playwright, global_browser
try:
# Playwright μΈμ€ν΄μ€ μ΄κΈ°ν
if global_playwright is None:
print("Playwright μΈμ€ν΄μ€ μ΄κΈ°ν...")
global_playwright = sync_playwright().start()
print("Playwright μ΄κΈ°ν μλ£")
# λΈλΌμ°μ μΈμ€ν΄μ€ νμΈ λ° μμ±
if global_browser is None or not global_browser.is_connected():
print("λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μ€...")
# λΈλΌμ°μ μ€ν μ΅μ
browser_args = [
'--no-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--disable-extensions',
'--disable-web-security',
'--disable-blink-features=AutomationControlled',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--disable-features=TranslateUI',
'--disable-ipc-flooding-protection'
]
global_browser = global_playwright.chromium.launch(
headless=True,
args=browser_args
)
print("λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μλ£")
# μ°κ²° μν μ¬νμΈ
if global_browser.is_connected():
print("λΈλΌμ°μ μ°κ²° μν μνΈ")
return global_browser
else:
print("λΈλΌμ°μ μ°κ²° μν λΆλ, μ¬μμ± μλ")
global_browser = None
return get_shared_browser() # μ¬κ· νΈμΆλ‘ μ¬μμ±
except Exception as e:
print(f"λΈλΌμ°μ μΈμ€ν΄μ€ μμ± μ€ν¨: {str(e)}")
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
# μ€ν¨μ μ 리νκ³ None λ°ν
try:
if global_browser:
global_browser.close()
if global_playwright:
global_playwright.stop()
except:
pass
global_browser = None
global_playwright = None
return None
def cleanup_browser():
"""λΈλΌμ°μ μ 리"""
global global_playwright, global_browser
try:
if global_browser:
global_browser.close()
global_browser = None
if global_playwright:
global_playwright.stop()
global_playwright = None
except:
pass
def capture_google_trends() -> Optional[Image.Image]:
"""Google Trends μ€ν¬λ¦°μ· μΊ‘μ² (λμ λ‘λ© λμ)"""
page = None
try:
print("Google Trends μ€ν¬λ¦°μ· μμ...")
browser = get_shared_browser()
if not browser:
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
return None
page = browser.new_page()
page.set_viewport_size({"width": 1400, "height": 900})
page.set_extra_http_headers({
"Accept-Language": "ko-KR,ko;q=0.9,en;q=0.8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
})
url = "https://trends.google.co.kr/trending?geo=KR"
print(f"νμ΄μ§ μ μ: {url}")
# νμ΄μ§ λ‘λ© (networkidleλ‘ JavaScript μλ£κΉμ§ λκΈ°)
try:
page.goto(url, wait_until="networkidle", timeout=45000)
print("νμ΄μ§ λ‘λ© μλ£")
except:
# networkidle μ€ν¨μ domcontentloadedλ‘ μ¬μλ
page.goto(url, wait_until="domcontentloaded", timeout=30000)
print("κΈ°λ³Έ νμ΄μ§ λ‘λ© μλ£")
# μ΄κΈ° λκΈ°
time.sleep(5)
# μΏ ν€ νμ
μ²λ¦¬
try:
accept_button = page.locator('button:has-text("λͺ¨λ μλ½"), button:has-text("Accept all"), button:has-text("μλ½")')
if accept_button.count() > 0:
accept_button.first.click(timeout=3000)
print("μΏ ν€ νμ
μ²λ¦¬λ¨")
time.sleep(3)
except Exception as e:
print(f"μΏ ν€ νμ
μ²λ¦¬ μ€ν¨ (무μ): {e}")
# νΈλ λ λ°μ΄ν° λ‘λ© λκΈ°
try:
# νΈλ λ 컨ν
μΈ κ° λ‘λλ λκΉμ§ λκΈ°
page.wait_for_selector("main, .trending-content, .trends-wrapper, [data-topic]", timeout=15000)
print("νΈλ λ 컨ν
μΈ λ‘λ© νμΈλ¨")
except:
print("νΈλ λ μμλ₯Ό μ°Ύμ μ μμ, κ³μ μ§ν")
# μΆκ° λκΈ° (λμ 컨ν
μΈ μμ λ‘λ©)
time.sleep(5)
# ν΅μ¬ μμ μ€ν¬λ¦°μ·
print("μ€ν¬λ¦°μ· μΊ‘μ² μμ...")
try:
main_content = page.locator('main, .main-content, [data-ve-type="main"], .trends-wrapper')
if main_content.count() > 0 and main_content.first.is_visible():
screenshot_bytes = main_content.first.screenshot(timeout=15000)
print("λ©μΈ 컨ν
μΈ μ€ν¬λ¦°μ· μΊ‘μ²λ¨")
else:
screenshot_bytes = page.screenshot(timeout=20000)
print("μ 체 νμ΄μ§ μ€ν¬λ¦°μ· μΊ‘μ²λ¨")
except Exception as e:
print(f"μ€ν¬λ¦°μ· μΊ‘μ² μ¬μλ: {e}")
screenshot_bytes = page.screenshot(timeout=20000)
print("μ¬μλ μ€ν¬λ¦°μ· μΊ‘μ² μλ£")
screenshot = Image.open(io.BytesIO(screenshot_bytes))
print("Google Trends μ€ν¬λ¦°μ· μλ£")
return screenshot
except Exception as e:
print(f"Google Trends μ€ν¬λ¦°μ· μ€ν¨: {str(e)}")
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
return None
finally:
if page:
try:
page.close()
except:
pass
def capture_ezme_trends() -> Optional[Image.Image]:
"""rank.ezme.net μ€ν¬λ¦°μ· μΊ‘μ² (λμ λ‘λ© λμ)"""
page = None
try:
print("rank.ezme.net μ€ν¬λ¦°μ· μμ...")
browser = get_shared_browser()
if not browser:
print("λΈλΌμ°μ μ΄κΈ°ν μ€ν¨")
return None
page = browser.new_page()
page.set_viewport_size({"width": 1400, "height": 1200})
page.set_extra_http_headers({
"Accept-Language": "ko-KR,ko;q=0.9,en;q=0.8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
})
url = "https://rank.ezme.net/diff/"
print(f"νμ΄μ§ μ μ: {url}")
# νμ΄μ§ λ‘λ© (networkidle λκΈ°λ‘ JavaScript λ‘λ© μλ£κΉμ§ λκΈ°)
try:
page.goto(url, wait_until="networkidle", timeout=45000)
print("νμ΄μ§ λ‘λ© μλ£")
except:
# networkidle μ€ν¨μ domcontentloadedλ‘ μ¬μλ
page.goto(url, wait_until="domcontentloaded", timeout=30000)
print("κΈ°λ³Έ νμ΄μ§ λ‘λ© μλ£")
# JavaScript μ€ν λ° λ°μ΄ν° λ‘λ© λκΈ°
print("λμ 컨ν
μΈ λ‘λ© λκΈ°μ€...")
time.sleep(8) # λ κΈ΄ λκΈ° μκ°
# ν
μ΄λΈ λ°μ΄ν°κ° λ‘λλμλμ§ νμΈ
try:
# μκ°λ³ λ°μ΄ν° ν
μ΄λΈ μμ λκΈ°
page.wait_for_selector("table, .table, td", timeout=10000)
print("ν
μ΄λΈ λ°μ΄ν° λ‘λ© νμΈλ¨")
except:
print("ν
μ΄λΈ μμλ₯Ό μ°Ύμ μ μμ, κ³μ μ§ν")
# νμ΄μ§ μ€κ°μΌλ‘ μ€ν¬λ‘€ (μκ°λ³ κ²μμ΄ λ°μ΄ν° μμ)
try:
page.evaluate("window.scrollTo(0, document.body.scrollHeight * 0.4)")
time.sleep(3)
print("μ€ν¬λ‘€ μλ£")
except Exception as e:
print(f"μ€ν¬λ‘€ μ€ν¨: {e}")
# μ 체 νμ΄μ§ μ€ν¬λ¦°μ·
print("μ€ν¬λ¦°μ· μΊ‘μ² μμ...")
screenshot_bytes = page.screenshot(full_page=True, timeout=20000)
screenshot = Image.open(io.BytesIO(screenshot_bytes))
print("rank.ezme.net μ€ν¬λ¦°μ· μλ£")
return screenshot
except Exception as e:
print(f"rank.ezme.net μ€ν¬λ¦°μ· μ€ν¨: {str(e)}")
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
return None
finally:
if page:
try:
page.close()
except:
pass
def analyze_google_trends_with_claude(image: Image.Image) -> str:
"""Google Trends μ΄λ―Έμ§ Claude λΆμ"""
if image is None:
return "Google Trends λΆμν μ΄λ―Έμ§κ° μμ΅λλ€."
try:
client = get_claude_client()
buffer = io.BytesIO()
image.save(buffer, format="PNG")
image_base64 = base64.b64encode(buffer.getvalue()).decode()
prompt = """Google Trends μ€μκ° μΈκΈ° κ²μμ΄ μ€ν¬λ¦°μ·μ λΆμν΄μ£ΌμΈμ.
λ€μ νλͺ©λ€μ μ€μ¬μΌλ‘ νκ΅μ΄λ‘ λΆμν΄μ£ΌμΈμ:
### A. μμ κ²μμ΄ μμ (κ²μλ κΈ°μ€)
κ²μλμ΄ λμ μμλλ‘ μ λ ¬νμ¬ λμ΄:
**1μ.** κ²μμ΄λͺ
(κ²μλ)
**2μ.** κ²μμ΄λͺ
(κ²μλ)
**3μ.** κ²μμ΄λͺ
(κ²μλ)
(μ΄λ° μμΌλ‘ 10μκΉμ§)
### B. μ£Όμ νΈλ λ ν€μλ
- νΉν μ£Όλͺ©ν λ§ν κ²μμ΄λ€
- κ²μλ μ¦κ° ν¨ν΄
### C. μΉ΄ν
κ³ λ¦¬λ³ νΉμ§
- μν°ν
μΈλ¨ΌνΈ, λ΄μ€, μ€ν¬μΈ λ± λΆμΌλ³ λν₯
### D. νΉμ΄μ¬ν λ° λΆμ
- κΈμμΉ ν€μλ
- μκΈ°μ /μ¬νμ λ°°κ²½ μΆμΈ‘
- μ 체μ μΈ κ΄μ¬μ¬ ν¨ν΄
**μ€μ μ§μμ¬ν:**
- λ§ν¬λ€μ΄ νμμ μ¬μ©νμ¬ **κ΅΅μ κΈμ¨**, *κΈ°μΈμ*, `μ½λ` λ±μΌλ‘ μ€μν λ΄μ©μ κ°μ‘°ν΄μ£ΌμΈμ
- κ²μμ΄λͺ
, μμΉ, μΉ΄ν
κ³ λ¦¬λͺ
λ± ν΅μ¬ μ 보λ **κ΅΅μ κΈμ¨**λ‘ νμ
- κΈμμΉλ₯ μ΄λ νΉλ³ν μμΉλ `λ°±ν±`μΌλ‘ κ°μΈμ κ°μ‘°
- μμ κ²μμ΄λ λ°λμ κ²μλ μμλλ‘ 1μλΆν° 10μκΉμ§ μ λ ¬"""
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1200,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_base64
}
}
]
}
]
)
return response.content[0].text
except Exception as e:
return f"Google Trends Claude λΆμ μ€ν¨: {str(e)}"
def analyze_ezme_trends_with_claude(image: Image.Image) -> str:
"""rank.ezme.net μ΄λ―Έμ§ Claude λΆμ"""
if image is None:
return "rank.ezme.net λΆμν μ΄λ―Έμ§κ° μμ΅λλ€."
try:
client = get_claude_client()
buffer = io.BytesIO()
image.save(buffer, format="PNG")
image_base64 = base64.b64encode(buffer.getvalue()).decode()
prompt = """νκ΅ ν¬νΈμ¬μ΄νΈλ³ μκ°λ³ μ€μκ° κ²μμ΄ λ°μ΄ν°λ₯Ό λΆμν΄μ£ΌμΈμ.
λ€μ νλͺ©λ€μ μ€μ¬μΌλ‘ νκ΅μ΄λ‘ λΆμν΄μ£ΌμΈμ:
### A. λλ©μΈλ³ μ£Όμ κ²μμ΄
**Zum (μ€) μ£Όμ κ²μμ΄:**
- μμ κ²μμ΄μ μ§μ
νμ
- μ£Όμ κ΄μ¬ λΆμΌ
**Nate (λ€μ΄νΈ) μ£Όμ κ²μμ΄:**
- μμ κ²μμ΄μ μ§μ
νμ
- μ£Όμ κ΄μ¬ λΆμΌ
**Google νκ΅ μ£Όμ κ²μμ΄:**
- μμ κ²μμ΄μ μ§μ
νμ
- μ£Όμ κ΄μ¬ λΆμΌ
### B. μκ°λλ³ νΈλ λ λ³ν
- μ΅κ·Ό λͺ μκ° λμμ λ³ν ν¨ν΄
- μκ°λλ³ μ£Όμ μ΄μ λ³ν
### C. κ²μμ΄ μ§μ
νμ λΆμ
- κ°μ₯ λμ μ§μ
νμλ₯Ό κΈ°λ‘ν κ²μμ΄λ€
- μ§μμ μΌλ‘ μμκΆμ μ μ§νλ κ²μμ΄λ€
### D. ν¬νΈλ³ κ΄μ¬μ¬ μ°¨μ΄μ
- κ° ν¬νΈμ¬μ΄νΈλ³ νΉμ± λΆμ
- μ¬μ©μμΈ΅λ³ κ΄μ¬μ¬ μ°¨μ΄
**μ€μ μ§μμ¬ν:**
- λ§ν¬λ€μ΄ νμμ μ¬μ©νμ¬ **κ΅΅μ κΈμ¨**λ‘ κ²μμ΄λͺ
κ°μ‘°
- μ§μ
νμλ `λ°±ν±`μΌλ‘ κ°μΈμ νμ (μ: `μ§μ
νμ: 15ν`)
- μκ°λ³ λ°μ΄ν°κ° μλ€λ©΄ ꡬ체μ μΌλ‘ μΈκΈ
- κ° ν¬νΈμ νΉμ§μ λͺ
νν ꡬλΆνμ¬ λΆμ"""
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1200,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_base64
}
}
]
}
]
)
return response.content[0].text
except Exception as e:
return f"rank.ezme.net Claude λΆμ μ€ν¨: {str(e)}"
def combine_analysis_results(google_analysis: str, ezme_analysis: str) -> str:
"""Google Trendsμ ezme λΆμ κ²°κ³Ό ν΅ν© (μ€λ₯ μν© λμ)"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# μ±κ³΅/μ€ν¨ μν νμΈ
google_failed = "λΆμν μ΄λ―Έμ§κ° μμ΅λλ€" in google_analysis or "μΊ‘μ²μ μ€ν¨" in google_analysis
ezme_failed = "λΆμν μ΄λ―Έμ§κ° μμ΅λλ€" in ezme_analysis or "μΊ‘μ²μ μ€ν¨" in ezme_analysis
# μν λ©μμ§ μμ±
if google_failed and ezme_failed:
status_msg = "**λͺ¨λ λ°μ΄ν° μμ€μ μμ§μ μ€ν¨νμ΅λλ€.** μ μ ν λ€μ μλν΄μ£ΌμΈμ."
elif google_failed:
status_msg = "**Google Trends λ°μ΄ν° μμ§μ μ€ν¨νμ΅λλ€.** νκ΅ ν¬νΈ λ°μ΄ν°λ§ μ 곡λ©λλ€."
elif ezme_failed:
status_msg = "**νκ΅ ν¬νΈ λ°μ΄ν° μμ§μ μ€ν¨νμ΅λλ€.** Google Trends λ°μ΄ν°λ§ μ 곡λ©λλ€."
else:
status_msg = "**λͺ¨λ λ°μ΄ν° μμ€λ₯Ό μ±κ³΅μ μΌλ‘ μμ§νμ΅λλ€.**"
combined_result = f"""# μ€μκ° κ²μμ΄ ν΅ν© λΆμ
**μ΅μ’
μ
λ°μ΄νΈ:** {timestamp}
{status_msg}
---
## Google Trends κΈλ‘λ² λν₯
{google_analysis}
---
## νκ΅ ν¬νΈ μ€μκ° κ²μμ΄
{ezme_analysis}
"""
# ---
# ## μ’
ν© λΆμ λ° μΈμ¬μ΄νΈ
# ### μ£Όμ κ³΅ν΅ μ΄μ
# - Google Trendsμ νκ΅ ν¬νΈμμ 곡ν΅μ μΌλ‘ μμκΆμ λνλλ κ²μμ΄λ€μ ν΅ν΄ νμ¬ κ°μ₯ λ¨κ±°μ΄ μ΄μλ₯Ό νμ
ν μ μμ΅λλ€.
# ### νλ«νΌλ³ νΉμ±
# - **Google Trends**: μ μΈκ³μ κ΄μ¬μ¬μ μ₯κΈ°μ νΈλ λ λ°μ
# - **νκ΅ ν¬νΈ (Zum, Nate, Google)**: νκ΅ μ¬μ©μμ μ€μκ° κ΄μ¬μ¬μ μ¦μμ± μλ μ΄μ λ°μ
# ### μκ°λλ³ κ²μ ν¨ν΄
# - κ° μκ°λλ³λ‘ λνλλ κ²μμ΄ λ³νλ₯Ό ν΅ν΄ νκ΅ μ¬μ©μλ€μ μΌμ ν¨ν΄κ³Ό κ΄μ¬μ¬ λ³νλ₯Ό κ΄μ°°ν μ μμ΅λλ€.
return combined_result
def perform_parallel_analysis() -> Tuple[bool, str]:
"""λ³λ ¬ μ²λ¦¬λ‘ ν΅ν© λΆμ μν (κ°μ λ μ€λ₯ μ²λ¦¬)"""
global analyzed_data, last_update_time, is_updating
is_updating = True
print(f"\n{'='*60}")
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] ν΅ν© μ€μκ° νΈλ λ λΆμ μμ")
print(f"{'='*60}")
try:
# λΈλΌμ°μ μ€μΉ νμΈ
print("1. λΈλΌμ°μ μ€μΉ νμΈ...")
install_browsers()
# λ³λ ¬ μ€ν¬λ¦°μ· μΊ‘μ²
print("2. λ³λ ¬ μ€ν¬λ¦°μ· μΊ‘μ² μμ...")
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
print(" - Google Trends μ€ν¬λ¦°μ· μμ
μμ")
google_future = executor.submit(capture_google_trends)
print(" - rank.ezme.net μ€ν¬λ¦°μ· μμ
μμ")
ezme_future = executor.submit(capture_ezme_trends)
print(" - λ μμ
μλ£ λκΈ° μ€...")
google_screenshot = google_future.result()
ezme_screenshot = ezme_future.result()
# μ€ν¬λ¦°μ· κ²°κ³Ό νμΈ
print("3. μ€ν¬λ¦°μ· κ²°κ³Ό νμΈ...")
google_success = google_screenshot is not None
ezme_success = ezme_screenshot is not None
print(f" - Google Trends μ€ν¬λ¦°μ·: {'μ±κ³΅' if google_success else 'μ€ν¨'}")
print(f" - rank.ezme.net μ€ν¬λ¦°μ·: {'μ±κ³΅' if ezme_success else 'μ€ν¨'}")
if not google_success and not ezme_success:
error_msg = "λͺ¨λ μ€ν¬λ¦°μ· μΊ‘μ²μ μ€ν¨νμ΅λλ€."
print(f" ERROR: {error_msg}")
return False, error_msg
print("4. Claude λΆμ μμ...")
# Claude λΆμ (μμ°¨ μ²λ¦¬ - API μ ν)
if google_success:
print(" - Google Trends λΆμ μ€...")
google_analysis = analyze_google_trends_with_claude(google_screenshot)
print(" - Google Trends λΆμ μλ£")
else:
google_analysis = "Google Trends μ€ν¬λ¦°μ· μΊ‘μ²μ μ€ν¨νμ¬ λΆμν μ μμ΅λλ€."
if ezme_success:
print(" - rank.ezme.net λΆμ μ€...")
ezme_analysis = analyze_ezme_trends_with_claude(ezme_screenshot)
print(" - rank.ezme.net λΆμ μλ£")
else:
ezme_analysis = "rank.ezme.net μ€ν¬λ¦°μ· μΊ‘μ²μ μ€ν¨νμ¬ λΆμν μ μμ΅λλ€."
print("5. ν΅ν© λΆμ κ²°κ³Ό μμ±...")
# ν΅ν© λΆμ κ²°κ³Ό μμ±
combined_analysis = combine_analysis_results(google_analysis, ezme_analysis)
# κ²°κ³Ό μ μ₯
analyzed_data["ν΅ν©μ€μκ°"] = {
'analysis': combined_analysis,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'google_analysis': google_analysis,
'ezme_analysis': ezme_analysis,
'google_success': google_success,
'ezme_success': ezme_success
}
last_update_time = datetime.now()
success_count = sum([google_success, ezme_success])
print(f"ν΅ν© λΆμ μλ£ - {last_update_time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"μ±κ³΅λ₯ : {success_count}/2 μμ€")
return True, combined_analysis
except Exception as e:
error_msg = f"ν΅ν© λΆμ μ€ μμμΉ λͺ»ν μ€λ₯: {str(e)}"
print(f"ERROR: {error_msg}")
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
return False, error_msg
finally:
is_updating = False
print(f"{'='*60}\n")
def auto_update_scheduler():
"""1μκ°λ§λ€ μλ μ
λ°μ΄νΈ"""
while True:
try:
print(f"[μ€μΌμ€λ¬] λ€μ μ
λ°μ΄νΈκΉμ§ 1μκ° λκΈ° μ€...")
time.sleep(3600) # 1μκ° λκΈ°
if not is_updating:
print(f"[μ€μΌμ€λ¬] μλ μ
λ°μ΄νΈ μμ")
perform_parallel_analysis()
else:
print(f"[μ€μΌμ€λ¬] μ΄λ―Έ μ
λ°μ΄νΈ μ€μ΄λ―λ‘ κ±΄λλ")
except Exception as e:
print(f"[μ€μΌμ€λ¬] μ€λ₯: {e}")
time.sleep(300) # 5λΆ ν μ¬μλ
def get_cached_analysis_result() -> tuple:
"""μ μ₯λ ν΅ν© λΆμ κ²°κ³Ό λ°ν"""
global analyzed_data, last_update_time
if "ν΅ν©μ€μκ°" in analyzed_data:
data = analyzed_data["ν΅ν©μ€μκ°"]
status = f"μ΅μ λ°μ΄ν° (μ
λ°μ΄νΈ: {data['timestamp']})"
return data['analysis'], status
else:
if last_update_time:
status = f"λ°μ΄ν° μμ (λ§μ§λ§ μ
λ°μ΄νΈ: {last_update_time.strftime('%H:%M:%S')})"
else:
status = "μμ§ λΆμ λ°μ΄ν°κ° μ€λΉλμ§ μμμ΅λλ€."
return "**λΆμ λ°μ΄ν°κ° μμ§ μ€λΉλμ§ μμμ΅λλ€.**\n\nμ μ ν λ€μ μλν΄μ£ΌμΈμ.", status
def create_interface():
"""Gradio μΈν°νμ΄μ€ μμ±"""
def show_cached_analysis():
"""μ μ₯λ ν΅ν© λΆμ κ²°κ³Ό νμ"""
if "ν΅ν©μ€μκ°" in analyzed_data:
data = analyzed_data["ν΅ν©μ€μκ°"]
status = f"μΊμλ ν΅ν© λΆμ κ²°κ³Ό (μ
λ°μ΄νΈ: {data['timestamp']})"
return data['analysis'], status
else:
if last_update_time:
status = f"μ μ₯λ λ°μ΄ν° μμ (λ§μ§λ§ μ
λ°μ΄νΈ: {last_update_time.strftime('%H:%M:%S')})"
else:
status = "μμ§ λΆμ λ°μ΄ν°κ° μ€λΉλμ§ μμμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
return "**μ μ₯λ ν΅ν© λΆμ λ°μ΄ν°κ° μμ΅λλ€.**\n\n'μ€μκ° μ
λ°μ΄νΈ' λ²νΌμ λλ¬μ£ΌμΈμ.", status
def perform_live_update():
"""μ€μκ° μ
λ°μ΄νΈ μν"""
if is_updating:
return "**νμ¬ μ
λ°μ΄νΈκ° μ§ν μ€μ
λλ€.**\n\nμ μ ν λ€μ μλν΄μ£ΌμΈμ.", "μ
λ°μ΄νΈ μ§ν μ€..."
print("μ€μκ° ν΅ν© μ
λ°μ΄νΈ μμ²λ¨")
success, result = perform_parallel_analysis()
if success and "ν΅ν©μ€μκ°" in analyzed_data:
data = analyzed_data["ν΅ν©μ€μκ°"]
return data['analysis'], f"μ€μκ° μ
λ°μ΄νΈ μλ£ - {datetime.now().strftime('%H:%M:%S')}"
else:
return "**μ
λ°μ΄νΈμ μ€ν¨νμ΅λλ€.**\n\nμ μ ν λ€μ μλν΄μ£ΌμΈμ.", f"μ
λ°μ΄νΈ μ€ν¨ - {datetime.now().strftime('%H:%M:%S')}"
# Gradio μΈν°νμ΄μ€ ꡬμ±
with gr.Blocks(
title="ν΅ν© μ€μκ° κ²μμ΄ λΆμ",
theme=gr.themes.Soft(),
css="""
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700&display=swap');
* {
font-family: 'Noto Sans KR', sans-serif !important;
}
.markdown-content {
font-size: 14px;
line-height: 1.6;
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
border: 1px solid #e9ecef;
}
.markdown-content h1 {
color: #1d4ed8;
margin-bottom: 15px;
}
.markdown-content h2 {
color: #2563eb;
margin-top: 25px;
margin-bottom: 12px;
}
.markdown-content h3 {
color: #3b82f6;
margin-top: 20px;
margin-bottom: 10px;
}
.markdown-content strong {
color: #1d4ed8;
font-weight: 600;
}
"""
) as interface:
gr.Markdown("# ν΅ν© μ€μκ° κ²μμ΄ λΆμ")
gr.Markdown("Google Trendsμ νκ΅ ν¬νΈμ¬μ΄νΈ κ²μμ΄λ₯Ό ν΅ν© λΆμν©λλ€.")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### λΆμ μ΅μ
")
with gr.Column():
cached_btn = gr.Button("ν΅ν© κ²μμ΄ λΆμ", variant="primary", size="lg")
gr.Markdown("μ μ₯λ ν΅ν© λΆμ κ²°κ³Όλ₯Ό μ¦μ νμν©λλ€.")
gr.Markdown("---")
with gr.Column():
live_btn = gr.Button("μ€μκ° μ
λ°μ΄νΈ", variant="secondary", size="lg")
gr.Markdown("Google Trends + νκ΅ ν¬νΈ μ΅μ λ°μ΄ν°λ‘ μ
λ°μ΄νΈν©λλ€. (μ½ 1λΆ μμ)")
gr.Markdown("---")
status_output = gr.Textbox(
label="μν",
value="λΆμν μ΅μ
μ μ νν΄μ£ΌμΈμ.",
interactive=False
)
# Apeach μ΄λ―Έμ§ νμ
try:
gr.Image(
value="apeach.png",
label=None,
show_label=False,
interactive=False,
height=200,
width=200
)
except:
pass
with gr.Column(scale=2):
analysis_output = gr.Markdown(
value="**λΆμ λ²νΌμ λλ¬μ£ΌμΈμ.**\n\n- **ν΅ν© κ²μμ΄ λΆμ**: μ μ₯λ κ²°κ³Ό μ¦μ νμΈ\n- **μ€μκ° μ
λ°μ΄νΈ**: μ΅μ λ°μ΄ν°λ‘ μλ‘ λΆμ",
elem_classes=["markdown-content"],
height=700
)
# μ΄λ²€νΈ μ°κ²°
cached_btn.click(
fn=show_cached_analysis,
outputs=[analysis_output, status_output]
)
live_btn.click(
fn=perform_live_update,
outputs=[analysis_output, status_output]
)
return interface
def main():
"""λ©μΈ ν¨μ"""
print("=" * 60)
print("ν΅ν© μ€μκ° κ²μμ΄ λΆμ μλΉμ€ μμ!")
print("=" * 60)
# API ν€ νμΈ
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
print("ANTHROPIC_API_KEY νκ²½λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
print(" .env νμΌμ ANTHROPIC_API_KEY=your_key_here λ₯Ό μΆκ°νμΈμ.")
return
else:
print("Claude API ν€ νμΈλ¨")
# μ’
λ£μ λΈλΌμ°μ μ 리
atexit.register(cleanup_browser)
# μ΄κΈ° ν΅ν© λΆμ μν
print("μ΄κΈ° ν΅ν© λΆμ μμ...")
initial_success, _ = perform_parallel_analysis()
if initial_success:
print("μ΄κΈ° ν΅ν© λΆμ μλ£")
else:
print("μ΄κΈ° λΆμ μ€ν¨, μλΉμ€λ κ³μ μ§νλ©λλ€.")
# μλ μ
λ°μ΄νΈ μ€μΌμ€λ¬ μμ
print("μλ μ
λ°μ΄νΈ μ€μΌμ€λ¬ μμ (1μκ° κ°κ²©)")
global update_thread
update_thread = threading.Thread(target=auto_update_scheduler, daemon=True)
update_thread.start()
print("λͺ¨λ μ€λΉ μλ£!")
print("=" * 60)
# Gradio μΈν°νμ΄μ€ μ€ν
try:
interface = create_interface()
interface.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True,
quiet=False
)
except Exception as e:
print(f"μΈν°νμ΄μ€ μμ μ€ν¨: {e}")
print(f"μμΈ μ€λ₯: {traceback.format_exc()}")
finally:
cleanup_browser()
if __name__ == "__main__":
main() |