-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_session.py
More file actions
56 lines (47 loc) · 2.19 KB
/
Copy pathbrowser_session.py
File metadata and controls
56 lines (47 loc) · 2.19 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
import random
import time
from playwright.sync_api import sync_playwright
# UPDATED: Import the standard, current Stealth configuration interface class
from playwright_stealth import Stealth
def generate_random_fingerprint():
"""Generates random combos of computer stats and shit."""
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"
]
viewports = [
{"width": 1920, "height": 1080},
{"width": 1366, "height": 768},
{"width": 1440, "height": 900},
{"width": 1536, "height": 864}
]
return random.choice(user_agents), random.choice(viewports)
def run_single_session(video_url):
"""
Launches a stealth headless window, runs the viewing profile, and returns
the active total session execution duration.
"""
start_time = time.time()
ua, viewport = generate_random_fingerprint()
with sync_playwright() as p:
browser = p.chromium.launch(
headless="shell",
args=["--disable-blink-features=AutomationControlled"]
)
context = browser.new_context(viewport=viewport, user_agent=ua)
# UPDATED: This applies stealth mechanics natively across the target context
Stealth().apply_stealth_sync(context)
page = context.new_page()
try:
page.goto(video_url, timeout=25000)
page.wait_for_load_state("domcontentloaded")
# Watch window calculation (randomized float between 1.0 and 7.0 seconds)
watch_time = random.uniform(1.0, 7.0)
time.sleep(watch_time)
except Exception:
pass # Keep execution continuous if a single network request fails
finally:
browser.close()
return time.time() - start_time