This repository has been archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.py
228 lines (207 loc) · 7.39 KB
/
main.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
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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import json
import re
from art import tprint
from time import sleep
import getpass
import shutil
import os
import pathlib
from download import convert
from rename import rename_dir
CLASSROOM = "https://www.scaler.com/academy/mentee-dashboard/classes/regular"
MASTERCLASS = "https://www.scaler.com/academy/mentee-dashboard/classes/events/masterclasses"
DOWNLOAD_PATH = str(pathlib.Path(
__file__).parent.absolute()) + r"\output\raw\\"
DOWNLOAD_PATH = DOWNLOAD_PATH[:-1]
driver = None
videoLinks = set([])
titleSet = set([])
hashSet = set([])
name = ''
# Driver init
def init_driver():
global driver
capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {"performance": "ALL"}
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory": DOWNLOAD_PATH}
chromeOptions.add_experimental_option("prefs", prefs)
chromeOptions.add_experimental_option(
'excludeSwitches', ['enable-logging'])
print("Initiating Chrome Driver...")
driver = webdriver.Chrome(
desired_capabilities=capabilities, options=chromeOptions)
def login():
driver.find_element(By.NAME, 'user[email]').send_keys(EMAIL)
driver.find_element(By.NAME, 'user[password]').send_keys(PASSWORD)
driver.find_element(By.CSS_SELECTOR, 'button.form__action').click()
def process_log(logs):
for entry in logs:
log = json.loads(entry["message"])["message"]
if "Network.responseReceived" in log["method"]:
yield log
def download(link, _type):
global name
driver.get(link)
if _type == 'master':
try:
WebDriverWait(driver, 5).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '.m-header__title')))
except TimeoutException:
return False
title = driver.find_element(
By.CSS_SELECTOR, '.m-header__title')
else:
try:
WebDriverWait(driver, 2).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '.event-card__content-last-button-container')))
recordBtn = driver.find_element(
By.CSS_SELECTOR, '.event-card__content-last-button-container')
except TimeoutException:
return False
if recordBtn.text != 'Watch Recording':
return False
title = driver.find_element(
By.CSS_SELECTOR, 'div.event-card__content-header')
recordBtn.click()
# Waiting for all the live m3u8 files to process
sleep(7)
events = process_log(driver.get_log("performance"))
for event in events:
try:
url = event['params']['response']['url']
except:
continue
if re.search('\.m3u8', url):
flag = 0
for item in videoLinks:
if item == url:
flag = 1
break
if flag:
continue
videoLinks.add(url)
name = title.text
driver.get(url)
params = url.split('/')
if params[2] != "www.scaler.com":
hashd = params[7]
hashd = hashd[:-5]
else:
hashd = params[4]
titleSet.add(title)
hashSet.add(hashd)
with open("output/hash.txt", 'a') as output:
output.write(f"{title.text} || {hashd}\n")
# wait for download to complete
sleep(3)
dest = f"{DOWNLOAD_PATH}{hashd}"
if not os.path.exists(dest):
os.makedirs(dest)
files = os.listdir(DOWNLOAD_PATH)
for g in files:
if g.endswith('.m3u8'):
try:
shutil.move(DOWNLOAD_PATH + g, dest)
except:
pass
sleep(1)
return True
def clear_files(_file):
with open(f"output/{_file}.txt", 'w') as output:
output.write(f"")
def download_classroom():
clear_files('hash')
clear_files('failed')
driver.get(CLASSROOM)
login()
success = 0
WebDriverWait(driver, 3).until(EC.presence_of_element_located(
(By.CLASS_NAME, 'icon-plus-circle')))
icons = driver.find_elements(By.CLASS_NAME, 'icon-plus-circle')
for i in icons:
i.click()
elements = driver.find_elements(By.CLASS_NAME, 'me-cr-classroom-url')
hrefs = [elem.get_attribute('href') for elem in elements]
links = list(filter(lambda x: 'session' in x, hrefs))
links.reverse()
print(f"Fetched {len(links)} items from 'All Classroom'....")
for link in links:
ops = download(link, 'class')
if ops:
print(f"Successfully downloaded {name}!")
success += 1
print("==================================================")
print(f"Downloaded: {success} video links")
def download_master():
clear_files('hash')
clear_files('failed')
driver.get(MASTERCLASS)
login()
success = 0
failed = 0
sleep(2)
elements = driver.find_elements(By.CLASS_NAME, 'day__link')
links = [elem.get_attribute('href') for elem in elements]
print(f"Fetched {len(links)} items from 'Masterclass'....")
for link in links:
ops = download(link, 'master')
if ops:
print(f"Successfully downloaded {name}!")
success += 1
else:
failed += 1
with open("output/failed.txt", 'a') as failed:
failed.write("{link}\n")
print("==================================================")
print(f"Success: {success}; Failed: {failed}")
def title_hash_pair():
with open("output/pair.txt", 'w') as pair:
for _title, _hash in zip(titleSet, hashSet):
pair.write(f"{_title} || {_hash}\n")
if __name__ == '__main__':
tprint("Scaler Downloader",font="cybermedium")
print("==================================================")
global EMAIL, PASSWORD
EMAIL = input("Enter email: ")
PASSWORD = getpass.getpass("Enter password: ")
init_driver()
while True:
print("What do you want me to do?")
print("1. Download videos")
print("2. Rename hashes")
print("3. Convert videos")
choice = int(input("> "))
if choice == 1:
print("==================================================")
print("What do you want to download?")
print("1. Classroom")
print("2. Masterclass")
print("==================================================")
choice = int(input("> "))
if choice == 1:
download_classroom()
elif choice == 2:
download_master()
else:
exit(1)
title_hash_pair()
print("==================================================")
choice = input("Do you want to rename and convert? y/n >")
if choice.lower() == 'y':
rename_dir()
convert()
else:
exit(2)
elif choice == 2:
rename_dir()
elif choice == 3:
convert()
else:
exit(1)