-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchrome_launch.py
More file actions
243 lines (203 loc) · 9.56 KB
/
Copy pathchrome_launch.py
File metadata and controls
243 lines (203 loc) · 9.56 KB
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
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
import os
import sys
import time
import random
import subprocess
import re
# Ensure parent directory is in sys.path for parent level imports
PARENT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if PARENT_DIR not in sys.path:
sys.path.append(PARENT_DIR)
# ✅ CONFIGURATION FOR KAGGLE / LOCAL FALLBACK
BASE_DIR = "/kaggle/working/blog_automation"
if not os.path.exists(BASE_DIR):
# Fallback to local agent_test path
BASE_DIR = r"C:\Users\T450\OneDrive\Desktop\kaggle_deployement\agent_test"
USER_DATA_DIR = os.path.join(BASE_DIR, "ChromeProfile")
LAUNCH_THRESHOLD = 15 # seconds
# 🎯 Progress Bar Animation
def show_progress_bar(task="Loading UC ChromeDriver", duration=3):
bar_length = 50
print(f"\n🔄 {task}")
for i in range(bar_length + 1):
percent = int((i / bar_length) * 100)
bar = '█' * i + '-' * (bar_length - i)
sys.stdout.write(f"\r[{bar}] {percent}%")
sys.stdout.flush()
time.sleep(duration / bar_length)
print("\n✅ Done.\n")
def clear_chrome_cache(user_data_dir, profile_name="Default"):
"""
Safely deletes Cache, Code Cache, GPUCache, and Service Worker cache storage
directories under the specified profile in user_data_dir.
Keeps session cookies and other local databases intact.
"""
import shutil
profile_path = os.path.join(user_data_dir, profile_name)
if not os.path.exists(profile_path):
return
print(f"🧹 [CACHE CLEANER] Scanning Chrome profile for cache files: {profile_path}")
cache_dirs = [
"Cache",
"Code Cache",
"GPUCache",
os.path.join("Service Worker", "CacheStorage"),
os.path.join("Service Worker", "ScriptCache"),
]
cleared_count = 0
for folder in cache_dirs:
target_dir = os.path.join(profile_path, folder)
if os.path.exists(target_dir):
try:
shutil.rmtree(target_dir)
print(f"🧹 [CACHE CLEANER] Cleared directory: {folder}")
cleared_count += 1
except Exception as e:
print(f"⚠️ [CACHE CLEANER] Failed to delete {folder}: {e}")
if cleared_count > 0:
print("🧹 [CACHE CLEANER] Cache directories cleared successfully!")
else:
print("🧹 [CACHE CLEANER] Profile directory is already clean.")
# 🛠️ Chrome Options Builder
def get_chrome_options(profile_name="Default", headless=True):
options = Options()
options.add_argument(f"--user-data-dir={USER_DATA_DIR}")
options.add_argument(f"--profile-directory={profile_name}")
# Limit cache size to 1 byte to prevent bloat during the session
options.add_argument("--disk-cache-size=1")
options.add_argument("--media-cache-size=1")
options.add_argument("--disable-gpu-program-cache")
options.add_argument("--disable-gpu-shader-disk-cache")
if headless:
options.add_argument("--headless=new") # Screen nahi hai Kaggle par
options.add_argument("--no-sandbox") # Docker crash se bachata hai
options.add_argument("--disable-dev-shm-usage") # Memory limit error ko rokata hai
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--no-first-run")
options.add_argument("--no-service-autorun")
options.add_argument("--password-store=basic")
options.add_argument("--window-size=1280,800") # Headless mein resolution set karna padta hai
return options
def get_installed_chrome_version():
"""Tries to find the installed Google Chrome version."""
if sys.platform == "win32":
try:
local_chrome = r"C:\Users\T450\OneDrive\Desktop\kaggle_deployement\agent_test\chrome-win64\chrome.exe"
if os.path.exists(local_chrome):
result = subprocess.run(['powershell', '-command', f"(Get-Item '{local_chrome}').VersionInfo.ProductVersion"], capture_output=True, text=True)
match = re.search(r"(\d+)\.", result.stdout)
if match: return int(match.group(1))
except:
pass
return None
else:
try:
result = subprocess.run(["/usr/bin/google-chrome", "--version"], capture_output=True, text=True, check=True)
match = re.search(r"Chrome\s+(\d+)\.", result.stdout)
if match: return int(match.group(1))
except Exception as e:
print(f"⚠️ Could not detect Chrome version from /usr/bin/google-chrome: {e}")
try:
result = subprocess.run(["google-chrome", "--version"], capture_output=True, text=True, check=True)
match = re.search(r"Chrome\s+(\d+)\.", result.stdout)
if match: return int(match.group(1))
except Exception:
pass
return None
def launch_browser(profile_name="Default", headless=None, max_retries=3, timeout_limit=15):
# Determine headless state dynamically
if headless is None:
# Default to headless on Kaggle (linux server), non-headless locally (windows)
headless = os.path.exists("/kaggle/working/blog_automation")
# 📦 AUTOMATIC KAGGLE DATASET ZIP EXTRACTOR 📦
try:
import zipfile
# Scan for ChromeProfile.zip under /kaggle/input/ recursively
zip_path = None
input_base = "/kaggle/input"
if os.path.exists(input_base):
for root, dirs, files in os.walk(input_base):
if "ChromeProfile.zip" in files:
zip_path = os.path.join(root, "ChromeProfile.zip")
break
# Unzip only if USER_DATA_DIR does not exist yet to prevent overwriting runtime updates
if zip_path and not os.path.exists(USER_DATA_DIR):
print(f"📦 [AUTO PROFILE DEPLOY] Found ChromeProfile.zip at: {zip_path}")
print(f"📦 Extracting to: {USER_DATA_DIR}...")
# Ensure the parent directory of USER_DATA_DIR exists
os.makedirs(os.path.dirname(USER_DATA_DIR), exist_ok=True)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(os.path.dirname(USER_DATA_DIR))
print("✅ [AUTO PROFILE DEPLOY] Extraction complete!")
except Exception as auto_err:
print(f"⚠️ [AUTO PROFILE DEPLOY] Failed to extract zip: {auto_err}")
# Clear browser cache before startup to free memory and prevent timeouts
try:
clear_chrome_cache(USER_DATA_DIR, profile_name)
except Exception as cache_err:
print(f"⚠️ [CACHE CLEANER] Error during cleanup: {cache_err}")
print(f"\n🤖 Setting up Chrome (headless={headless})...")
detected_version = get_installed_chrome_version()
fallback_version = 149
if detected_version:
print(f"🔍 Detected Chrome Version: {detected_version}")
use_version = detected_version
else:
print(f"⚠️ Failed to detect Chrome version. Falling back to default: {fallback_version}")
use_version = fallback_version
for attempt in range(1, max_retries + 1):
print(f"🚀 Attempt {attempt} to launch browser...")
try:
launch_start = time.time()
is_windows = sys.platform == "win32"
kwargs = {
"options": get_chrome_options(profile_name, headless=headless),
"use_subprocess": True,
}
if not os.path.exists("/kaggle/working/blog_automation"):
# Local test fallback
if is_windows:
chrome_path = r"C:\Users\T450\OneDrive\Desktop\kaggle_deployement\agent_test\chrome-win64\chrome.exe"
if os.path.exists(chrome_path):
kwargs["browser_executable_path"] = chrome_path
if use_version:
kwargs["version_main"] = use_version
else:
kwargs["browser_executable_path"] = "/usr/bin/google-chrome"
kwargs["version_main"] = use_version
else:
# Kaggle production launch
kwargs["browser_executable_path"] = "/usr/bin/google-chrome"
kwargs["version_main"] = use_version
driver = uc.Chrome(**kwargs)
# ⏳ Wait for profile to load
success = False
for i in range(timeout_limit):
try:
if driver.current_url: # URL check is safer in headless mode
success = True
print("✅ Profile aur DOM properly load ho gaye hain.")
break
except:
pass
time.sleep(1)
if success:
launch_duration = time.time() - launch_start
print(f"✅ Browser loaded in {launch_duration:.2f} seconds")
return driver
else:
print("❌ Browser loaded but unresponsive. Quitting and retrying...")
driver.quit()
except Exception as e:
print(f"❌ Launch failed: {e}")
print(f"🔁 Retrying in {timeout_limit} seconds...\n")
time.sleep(timeout_limit)
print("⛔ All attempts failed. Exiting.")
sys.exit(1)
# --- BOT LOGIC FOR TRENDFOLLOWER ---
def human_delay(min_sec=3, max_sec=8):
delay = random.uniform(min_sec, max_sec)
print(f"⏳ Waiting like a human for {round(delay, 2)} seconds...")
time.sleep(delay)