Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -380,6 +380,94 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 380 |
tmp_path = None # 初期化
|
| 381 |
driver = None # 初期化
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
# 1) Save HTML code to a temporary file
|
| 384 |
try:
|
| 385 |
with tempfile.NamedTemporaryFile(suffix=".html", delete=False, mode='w', encoding='utf-8') as tmp_file:
|
|
@@ -420,29 +508,42 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 420 |
)
|
| 421 |
logger.info("Body element found. Waiting for potential resource loading...")
|
| 422 |
|
| 423 |
-
#
|
| 424 |
-
time.sleep(
|
| 425 |
|
| 426 |
-
# 5) Font Awesomeと外部リソースの読み込み完了を確認
|
|
|
|
| 427 |
try:
|
| 428 |
-
|
| 429 |
-
driver.set_script_timeout(15) # 15秒のタイムアウト
|
| 430 |
driver.execute_async_script(resource_check_script)
|
| 431 |
-
logger.info("Resources
|
| 432 |
except Exception as e:
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
-
# 6)
|
|
|
|
| 437 |
try:
|
| 438 |
-
logger.info("Ensuring all content is rendered...")
|
| 439 |
driver.execute_async_script(scroll_script)
|
| 440 |
except Exception as e:
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
# 7) Hide scrollbars via CSS
|
| 447 |
try:
|
| 448 |
driver.execute_script(
|
|
@@ -451,7 +552,7 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 451 |
)
|
| 452 |
logger.info("Scrollbars hidden via JS.")
|
| 453 |
except Exception as e:
|
| 454 |
-
logger.
|
| 455 |
|
| 456 |
# 8) Get full page dimensions accurately with improved script
|
| 457 |
try:
|
|
@@ -499,7 +600,7 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 499 |
scroll_height = max(scroll_height, 100) # 最小高さ
|
| 500 |
|
| 501 |
scroll_width = min(scroll_width, 2000) # 最大幅
|
| 502 |
-
scroll_height = min(scroll_height,
|
| 503 |
|
| 504 |
except Exception as e:
|
| 505 |
logger.error(f"Error getting page dimensions: {e}")
|
|
@@ -508,15 +609,18 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 508 |
scroll_height = 1000
|
| 509 |
logger.warning(f"Falling back to dimensions: width={scroll_width}, height={scroll_height}")
|
| 510 |
|
| 511 |
-
# 9) レイアウト安定化の確認
|
|
|
|
| 512 |
try:
|
| 513 |
-
|
| 514 |
driver.execute_async_script(stability_script)
|
| 515 |
-
logger.info("Layout
|
| 516 |
except Exception as e:
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
| 520 |
# 10) Calculate adjusted height with user-specified margin
|
| 521 |
adjusted_height = int(scroll_height * (1 + extension_percentage / 100.0))
|
| 522 |
# Ensure adjusted height is not excessively large or small
|
|
@@ -530,7 +634,7 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 530 |
logger.info("Waiting for layout stabilization after resize...")
|
| 531 |
|
| 532 |
# レイアウト安定化のための待機
|
| 533 |
-
time.sleep(
|
| 534 |
|
| 535 |
# 外部リソースの読み込み状態を確認
|
| 536 |
try:
|
|
@@ -548,7 +652,7 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 548 |
logger.info("Document still loading, waiting additional time...")
|
| 549 |
time.sleep(2)
|
| 550 |
except Exception as e:
|
| 551 |
-
logger.
|
| 552 |
|
| 553 |
# Scroll to top just in case
|
| 554 |
try:
|
|
@@ -556,7 +660,7 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
|
|
| 556 |
time.sleep(1)
|
| 557 |
logger.info("Scrolled to top.")
|
| 558 |
except Exception as e:
|
| 559 |
-
logger.
|
| 560 |
|
| 561 |
# 12) Take screenshot
|
| 562 |
logger.info("Taking screenshot...")
|
|
|
|
| 380 |
tmp_path = None # 初期化
|
| 381 |
driver = None # 初期化
|
| 382 |
|
| 383 |
+
# JavaScript関数を定義 - 古い構文を使用して互換性を確保
|
| 384 |
+
resource_check_script = """
|
| 385 |
+
return (function() {
|
| 386 |
+
var callback = arguments[arguments.length - 1];
|
| 387 |
+
|
| 388 |
+
// Font Awesomeの読み込み確認
|
| 389 |
+
function checkFontAwesome() {
|
| 390 |
+
var icons = document.querySelectorAll('.fa, .fas, .far, .fab, [class*="fa-"]');
|
| 391 |
+
if (icons.length > 0) {
|
| 392 |
+
console.log('Font Awesome icons found:', icons.length);
|
| 393 |
+
// タイムアウトベースの簡易アプローチ
|
| 394 |
+
setTimeout(function() { callback(true); }, 1500);
|
| 395 |
+
} else {
|
| 396 |
+
// アイコンがない場合
|
| 397 |
+
setTimeout(function() { callback(true); }, 800);
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
// DOMContentLoadedまたはloadイベント後にチェック
|
| 402 |
+
if (document.readyState === 'complete') {
|
| 403 |
+
checkFontAwesome();
|
| 404 |
+
} else {
|
| 405 |
+
window.addEventListener('load', checkFontAwesome);
|
| 406 |
+
}
|
| 407 |
+
})();
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
scroll_script = """
|
| 411 |
+
return (function() {
|
| 412 |
+
var callback = arguments[arguments.length - 1];
|
| 413 |
+
|
| 414 |
+
var height = Math.max(
|
| 415 |
+
document.documentElement.scrollHeight,
|
| 416 |
+
document.body.scrollHeight
|
| 417 |
+
);
|
| 418 |
+
var viewportHeight = window.innerHeight;
|
| 419 |
+
|
| 420 |
+
// ページを少しずつスクロールして全体を描画させる
|
| 421 |
+
var scrollStep = Math.floor(viewportHeight * 0.8);
|
| 422 |
+
var currentPos = 0;
|
| 423 |
+
|
| 424 |
+
function scrollDown() {
|
| 425 |
+
if (currentPos < height) {
|
| 426 |
+
window.scrollTo(0, currentPos);
|
| 427 |
+
currentPos += scrollStep;
|
| 428 |
+
setTimeout(scrollDown, 100);
|
| 429 |
+
} else {
|
| 430 |
+
// 最後にトップに戻す
|
| 431 |
+
window.scrollTo(0, 0);
|
| 432 |
+
setTimeout(function() { callback(true); }, 300);
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
scrollDown();
|
| 437 |
+
})();
|
| 438 |
+
"""
|
| 439 |
+
|
| 440 |
+
stability_script = """
|
| 441 |
+
return (function() {
|
| 442 |
+
var callback = arguments[arguments.length - 1];
|
| 443 |
+
|
| 444 |
+
var lastHeight = document.body.offsetHeight;
|
| 445 |
+
var lastWidth = document.body.offsetWidth;
|
| 446 |
+
var stableCount = 0;
|
| 447 |
+
|
| 448 |
+
function checkStability() {
|
| 449 |
+
var currentHeight = document.body.offsetHeight;
|
| 450 |
+
var currentWidth = document.body.offsetWidth;
|
| 451 |
+
|
| 452 |
+
if (currentHeight === lastHeight && currentWidth === lastWidth) {
|
| 453 |
+
stableCount++;
|
| 454 |
+
if (stableCount >= 3) { // 3回連続で同じなら安定と判断
|
| 455 |
+
callback(true);
|
| 456 |
+
return;
|
| 457 |
+
}
|
| 458 |
+
} else {
|
| 459 |
+
stableCount = 0;
|
| 460 |
+
lastHeight = currentHeight;
|
| 461 |
+
lastWidth = currentWidth;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
setTimeout(checkStability, 200); // 200ms間隔でチェック
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
checkStability();
|
| 468 |
+
})();
|
| 469 |
+
"""
|
| 470 |
+
|
| 471 |
# 1) Save HTML code to a temporary file
|
| 472 |
try:
|
| 473 |
with tempfile.NamedTemporaryFile(suffix=".html", delete=False, mode='w', encoding='utf-8') as tmp_file:
|
|
|
|
| 508 |
)
|
| 509 |
logger.info("Body element found. Waiting for potential resource loading...")
|
| 510 |
|
| 511 |
+
# リソース読み込みの基本的な待機時間
|
| 512 |
+
time.sleep(2)
|
| 513 |
|
| 514 |
+
# 5) Font Awesomeと外部リソースの読み込み完了を確認 - エラーハンドリング改善
|
| 515 |
+
logger.info("Waiting for Font Awesome and other resources to load...")
|
| 516 |
try:
|
| 517 |
+
driver.set_script_timeout(10) # タイムアウト時間を短縮
|
|
|
|
| 518 |
driver.execute_async_script(resource_check_script)
|
| 519 |
+
logger.info("Resources loading check completed")
|
| 520 |
except Exception as e:
|
| 521 |
+
if "not defined" in str(e):
|
| 522 |
+
# 定義エラーは発生しないはず - ログ出力のみ
|
| 523 |
+
logger.warning(f"スクリプト変数エラー: {e}")
|
| 524 |
+
time.sleep(3)
|
| 525 |
+
else:
|
| 526 |
+
# 他のエラーでも処理を続行(短いタイムアウト)
|
| 527 |
+
logger.info(f"リソースチェックをスキップ: {str(e).split('Stacktrace')[0]}")
|
| 528 |
+
time.sleep(4)
|
| 529 |
|
| 530 |
+
# 6) コンテンツ全体を描画するためのスクロール処理
|
| 531 |
+
logger.info("Rendering full page content...")
|
| 532 |
try:
|
|
|
|
| 533 |
driver.execute_async_script(scroll_script)
|
| 534 |
except Exception as e:
|
| 535 |
+
# エラーログを簡素化
|
| 536 |
+
logger.info(f"スクロール処理をスキップ: {str(e).split('Stacktrace')[0]}")
|
| 537 |
+
# フォールバック: 単純なスクロール処理
|
| 538 |
+
try:
|
| 539 |
+
driver.execute_script("""
|
| 540 |
+
window.scrollTo(0, document.body.scrollHeight);
|
| 541 |
+
setTimeout(function() { window.scrollTo(0, 0); }, 500);
|
| 542 |
+
""")
|
| 543 |
+
time.sleep(1.5)
|
| 544 |
+
except:
|
| 545 |
+
pass
|
| 546 |
+
|
| 547 |
# 7) Hide scrollbars via CSS
|
| 548 |
try:
|
| 549 |
driver.execute_script(
|
|
|
|
| 552 |
)
|
| 553 |
logger.info("Scrollbars hidden via JS.")
|
| 554 |
except Exception as e:
|
| 555 |
+
logger.info(f"Could not hide scrollbars: {str(e).split('Stacktrace')[0]}")
|
| 556 |
|
| 557 |
# 8) Get full page dimensions accurately with improved script
|
| 558 |
try:
|
|
|
|
| 600 |
scroll_height = max(scroll_height, 100) # 最小高さ
|
| 601 |
|
| 602 |
scroll_width = min(scroll_width, 2000) # 最大幅
|
| 603 |
+
scroll_height = min(scroll_height, 4000) # 最大高さ
|
| 604 |
|
| 605 |
except Exception as e:
|
| 606 |
logger.error(f"Error getting page dimensions: {e}")
|
|
|
|
| 609 |
scroll_height = 1000
|
| 610 |
logger.warning(f"Falling back to dimensions: width={scroll_width}, height={scroll_height}")
|
| 611 |
|
| 612 |
+
# 9) レイアウト安定化の確認 - 簡素化
|
| 613 |
+
logger.info("Checking layout stability...")
|
| 614 |
try:
|
| 615 |
+
driver.set_script_timeout(5) # 短いタイムアウト
|
| 616 |
driver.execute_async_script(stability_script)
|
| 617 |
+
logger.info("Layout stability verified")
|
| 618 |
except Exception as e:
|
| 619 |
+
# エラーログを簡素化
|
| 620 |
+
logger.info(f"安定性チェックをスキップ: {str(e).split('Stacktrace')[0]}")
|
| 621 |
+
# 代わりに固定待機
|
| 622 |
+
time.sleep(2)
|
| 623 |
+
|
| 624 |
# 10) Calculate adjusted height with user-specified margin
|
| 625 |
adjusted_height = int(scroll_height * (1 + extension_percentage / 100.0))
|
| 626 |
# Ensure adjusted height is not excessively large or small
|
|
|
|
| 634 |
logger.info("Waiting for layout stabilization after resize...")
|
| 635 |
|
| 636 |
# レイアウト安定化のための待機
|
| 637 |
+
time.sleep(3) # 統一した待機時間
|
| 638 |
|
| 639 |
# 外部リソースの読み込み状態を確認
|
| 640 |
try:
|
|
|
|
| 652 |
logger.info("Document still loading, waiting additional time...")
|
| 653 |
time.sleep(2)
|
| 654 |
except Exception as e:
|
| 655 |
+
logger.info(f"Resource state check skipped: {str(e).split('Stacktrace')[0]}")
|
| 656 |
|
| 657 |
# Scroll to top just in case
|
| 658 |
try:
|
|
|
|
| 660 |
time.sleep(1)
|
| 661 |
logger.info("Scrolled to top.")
|
| 662 |
except Exception as e:
|
| 663 |
+
logger.info(f"Could not scroll to top: {str(e).split('Stacktrace')[0]}")
|
| 664 |
|
| 665 |
# 12) Take screenshot
|
| 666 |
logger.info("Taking screenshot...")
|