-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
179 lines (137 loc) · 6.95 KB
/
scraper.py
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
from selenium import webdriver
#webdriver: The main module in Selenium to control web browsers. It allows the script to open a browser, navigate to web pages,
# interact with elements (e.g., clicks, inputs), and retrieve data.
from selenium.webdriver.chrome.service import Service
#Service: Manages the ChromeDriver executable, which enables Selenium to communicate with the Chrome browser.
# It specifies the driver path and ensures proper handling of the ChromeDriver process.
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
#options: Used to customize the behavior of the Chrome browser. For example:
#Running the browser headlessly (without UI).
#Setting window size or position.
#Blocking pop-ups or enabling specific features.
from selenium.webdriver.support import expected_conditions as EC
import os
import crome_webdriver
import requests
#requests: A popular Python library for making HTTP requests. It is used here to download video files by sending requests to their
# URLs and saving the response data.
import time
import platform
#platform: A standard library module to detect the operating system being used (e.g., Windows, macOS, Linux).
from selenium.webdriver.common.action_chains import ActionChains
#ActionChains are a way to automate low-level interactions such as mouse movements,mouse button actions, keypress, and context menu
# interactions. This is useful for doing more complex actions like hovering over and drag and drop.
def setup_driver():
chrome_options = Options()
chrome_options.add_argument("--start-minimized")
# Detect the operating system and set the path accordingly
if platform.system() == "Windows":
service = Service(executable_path="driver/chromedriver.exe")
else :
service = Service(executable_path="driver/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
return driver
def download_videos(url,filename):
if not os.path.exists('videos'): #This will check if any file name videos is present there or not
os.makedirs('videos') #if not present there make the file name videos in the folder to store the videos
file_path = os.path.join('videos',filename) #Combines the directory path (videos) with the filename to get the full path for saving the video.
print("ok")
try:
print(f"Downloading : {filename}")
response = requests.get(url,stream=True)
if response.status_code == 200: #200: Indicates the request was successful, and the server is returning the video file.
with open(file_path,'wb') as file:
for chunk in response.iter_content(1024): #Opens the file at file_path in write-binary mode ('wb') to save the video data.
#Reads the response data in chunks of 1024 bytes (1 KB).
#Prevents loading the entire file into memory at once.
file.write(chunk)
print(f"Download Complete: {file_path}")
else:
print(f"Failed to download : {url} ")
except Exception as e: #Catches any unexpected errors during the download process, such as: Network interruptions,File writing issues (e.g., permission errors).
print(f"Error Downloading {filename}:{e} ")
def close_all_popups(driver: webdriver.Chrome):
#It will close all the unwanted popups and try to stay on the main window.
main_window = driver.current_window_handle
for handle in driver.window_handles:
if handle != main_window:
driver.switch_to.window(handle)
driver.close()
driver.switch_to.window(main_window)
def keep_clicking_until_video_plays(driver, streamtape_url):
driver.set_window_position(500, 100)
#driver.minimize_window()
driver.get(streamtape_url)
time.sleep(3)
attempts = 0
video_found = False
max_attempts = 100
while attempts < max_attempts:
try:
overlay = driver.find_element(By.CLASS_NAME, "play-overlay")
driver.execute_script("arguments[0].click();", overlay)
print(f"Overlay clicked on attempt {attempts+1}")
except Exception:
print(f"Overlay not found or already clicked on attempt {attempts+1}")
try:
play_button = driver.find_element(By.CLASS_NAME, "plyr__control--overlaid")
driver.execute_script("arguments[0].click();", play_button)
print(f"Play button clicked on attempt {attempts+1}")
video_player = driver.find_element(By.TAG_NAME, 'video')
video_url = video_player.get_attribute('src')
if video_url:
print(f"Video found and playing on attempt {attempts+1}")
video_found = True
break
except Exception:
print(f"Play button or video not found on attempt {attempts+1}")
close_all_popups(driver)
attempts += 1
time.sleep(0.25)
if video_found:
return True
else:
print(f"Failed to start video after {attempts} attempts.")
return False
def get_download_link(driver: webdriver.Chrome, streamtape_url):
driver.set_window_position(500, 100)
#driver.minimize_window()
driver.get(streamtape_url)
try:
if not keep_clicking_until_video_plays(driver, streamtape_url):
return None, None
print(0)
time.sleep(5)
h2_element = driver.find_element(By.TAG_NAME, "h2")
file_name = h2_element.text.strip()
print(f"File Name Found: {file_name}")
video_player = driver.find_element(By.TAG_NAME, 'video')
video_url = video_player.get_attribute('src')
print(f"Download Link Found: {video_url}")
return file_name, video_url
except Exception as e:
print(f"Error extracting file name or download link from {streamtape_url}: {e}")
return None, None
def read_links_from_file(file_path):
if os.path.exists(file_path):
with open(file_path, 'r') as file:
return [line.strip() for line in file.readlines() if line.strip()]
else:
print(f"{file_path} does not exist.")
return []
def main():
driver = setup_driver()
links = read_links_from_file("links.txt")
for i,link in enumerate(links,1):
print(f"processing {i}/{len(links)}: {link}")
file_name,video_url = get_download_link(driver,link)
if video_url:
download_videos(video_url,file_name)
driver.quit()
if __name__ == "__main__":
crome_webdriver.setup_chromedriver() #We will first setup the chrome webdriver to proceed further
#The chrome webdriver installation in the device.
main() #Then we will call main funtion to procced further.