Lcmind commited on
Commit
a98a453
Β·
1 Parent(s): ca03dab

fix: S-tier prompt - hex to color name, blur UI text, remove watermarks

Browse files
Files changed (1) hide show
  1. app/services/screenshot.py +44 -9
app/services/screenshot.py CHANGED
@@ -41,6 +41,14 @@ async def capture_screenshot(url: str) -> tuple[str, str]:
41
  if not url.startswith(('http://', 'https://')):
42
  url = f'https://{url}'
43
 
 
 
 
 
 
 
 
 
44
  # Set environment variables for Pyppeteer
45
  os.environ['PYPPETEER_CHROMIUM_REVISION'] = '1181205'
46
  os.environ['PYPPETEER_EXECUTABLE_PATH'] = settings.puppeteer_executable_path
@@ -75,19 +83,46 @@ async def capture_screenshot(url: str) -> tuple[str, str]:
75
 
76
  # === Phase 2: 침투 (Navigation) ===
77
  navigation_success = False
 
 
 
78
  try:
79
- # domcontentloaded: HTML λΌˆλŒ€λ§Œ λ‘œλ”©λ˜λ©΄ μ§„ν–‰ (10초 νƒ€μž„μ•„μ›ƒ)
80
  await page.goto(url, {'waitUntil': 'domcontentloaded', 'timeout': 10000})
81
  navigation_success = True
82
  except Exception as e:
83
- logger.warning(f"Navigation error: {e}, retrying with longer timeout...")
84
- # μž¬μ‹œλ„: 더 κΈ΄ νƒ€μž„μ•„μ›ƒμœΌλ‘œ μ΅œμ†Œν•œμ˜ λ‘œλ”©μ΄λΌλ„ μ‹œλ„
85
- try:
86
- await page.goto(url, {'waitUntil': 'load', 'timeout': 15000})
87
- navigation_success = True
88
- except Exception as e2:
89
- logger.error(f"Navigation failed completely: {e2}")
90
- raise Exception(f"Failed to load page: {url}. Error: {str(e2)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  # === Phase 3: μ‹œκ° 데이터 μˆ˜μ§‘ (Screenshot) ===
93
  screenshot_path = '/tmp/screenshot.png'
 
41
  if not url.startswith(('http://', 'https://')):
42
  url = f'https://{url}'
43
 
44
+ # URL μ •κ·œν™”: www μžλ™ μΆ”κ°€ μ‹œλ„ (DNS 해석 μ‹€νŒ¨ λ°©μ§€)
45
+ original_url = url
46
+ if 'www.' not in url:
47
+ # instagram.com -> www.instagram.com
48
+ url_with_www = url.replace('://', '://www.', 1)
49
+ else:
50
+ url_with_www = None
51
+
52
  # Set environment variables for Pyppeteer
53
  os.environ['PYPPETEER_CHROMIUM_REVISION'] = '1181205'
54
  os.environ['PYPPETEER_EXECUTABLE_PATH'] = settings.puppeteer_executable_path
 
83
 
84
  # === Phase 2: 침투 (Navigation) ===
85
  navigation_success = False
86
+ last_error = None
87
+
88
+ # 첫 μ‹œλ„: 원본 URL
89
  try:
 
90
  await page.goto(url, {'waitUntil': 'domcontentloaded', 'timeout': 10000})
91
  navigation_success = True
92
  except Exception as e:
93
+ last_error = e
94
+ logger.warning(f"Navigation failed for {url}: {e}")
95
+
96
+ # μž¬μ‹œλ„ 1: www μΆ”κ°€ 버전 (DNS 해석 μ‹€νŒ¨ λŒ€μ‘)
97
+ if url_with_www and url_with_www != url:
98
+ try:
99
+ logger.info(f"Retrying with www: {url_with_www}")
100
+ await page.goto(url_with_www, {'waitUntil': 'domcontentloaded', 'timeout': 10000})
101
+ navigation_success = True
102
+ url = url_with_www # μ„±κ³΅ν•œ URL둜 μ—…λ°μ΄νŠΈ
103
+ except Exception as e2:
104
+ last_error = e2
105
+ logger.warning(f"www retry also failed: {e2}")
106
+
107
+ # μž¬μ‹œλ„ 2: 더 κΈ΄ νƒ€μž„μ•„μ›ƒ (느린 μ‚¬μ΄νŠΈ λŒ€μ‘)
108
+ if not navigation_success:
109
+ try:
110
+ logger.info(f"Final retry with longer timeout: {url}")
111
+ await page.goto(url, {'waitUntil': 'load', 'timeout': 15000})
112
+ navigation_success = True
113
+ except Exception as e3:
114
+ last_error = e3
115
+
116
+ # λͺ¨λ“  μ‹œλ„ μ‹€νŒ¨ μ‹œ λͺ…ν™•ν•œ μ—λŸ¬ λ©”μ‹œμ§€
117
+ if not navigation_success:
118
+ error_msg = str(last_error)
119
+ if 'ERR_NAME_NOT_RESOLVED' in error_msg:
120
+ raise Exception(
121
+ f"DNS 해석 μ‹€νŒ¨: '{original_url}' 도메인을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€. "
122
+ f"μ„œλ²„ ν™˜κ²½μ—μ„œ 이 μ‚¬μ΄νŠΈ 접속이 μ°¨λ‹¨λ˜μ—ˆκ±°λ‚˜, 잘λͺ»λœ URLμž…λ‹ˆλ‹€."
123
+ )
124
+ else:
125
+ raise Exception(f"νŽ˜μ΄μ§€ λ‘œλ”© μ‹€νŒ¨: {url}. Error: {error_msg}")
126
 
127
  # === Phase 3: μ‹œκ° 데이터 μˆ˜μ§‘ (Screenshot) ===
128
  screenshot_path = '/tmp/screenshot.png'