-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (67 loc) · 2.27 KB
/
main.py
File metadata and controls
87 lines (67 loc) · 2.27 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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
def skip_ads(driver):
print("Checking for ads...")
end_time = time.time() + 20
while time.time() < end_time:
buttons = [
(By.CLASS_NAME, "ytp-skip-ad-button"),
(By.CLASS_NAME, "ytp-ad-skip-button-container"),
(By.XPATH, "//button[contains(text(),'Skip')]")
]
for selector in buttons:
try:
btn = driver.find_element(*selector)
btn.click()
print("Ad skipped.")
return
except:
pass
time.sleep(0.5)
print("unskippble ad.")
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)
try:
driver.get("https://www.youtube.com/")
time.sleep(4)
search_box = driver.find_element(By.NAME, "search_query")
search_box.send_keys("Pink lips")
search_box.send_keys(Keys.ENTER)
time.sleep(3)
video = driver.find_element(By.XPATH, "(//ytd-video-renderer//a[@id='video-title'])[1]")
video.click()
time.sleep(6)
skip_ads(driver)
video_el = None
for _ in range(20):
try:
video_el = driver.find_element(By.TAG_NAME, "video")
break
except:
time.sleep(1)
if video_el is None:
print("vedio not found.")
else:
driver.execute_script("arguments[0].currentTime = 30;", video_el)
print("Jumped to 30 sec.")
time.sleep(3)
driver.execute_script("arguments[0].pause();", video_el)
print("paused")
time.sleep(2)
driver.execute_script("arguments[0].play();", video_el)
print("palying for 8 sec")
time.sleep(8)
driver.execute_script("arguments[0].pause();", video_el)
print("Paused again.")
time.sleep(2)
driver.execute_script("arguments[0].play();", video_el)
print("Video replayed.")
except Exception as e:
print("ERROR OCCURRED, but browser will NOT close:")
print(e)
print("\nBrowser is still open. It will stay open until you press Enter.")
input("Press ENTER to close browser...\n")