-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
212 lines (181 loc) · 7.05 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
#
# * Author: wassim ben jdida
# * Github: wassimbj
# -----------------------------------------------------------------------------------
# a python script for downloading educative.io courses
# ? NOTE: this is a not a hacking tool, you must have access to the course to be able to download it
# ? read the readme to see how you can use it :)
# -----------------------------------------------------------------------------------s
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
import img2pdf
import os
from pathlib import Path
base_url = "https://educative.io"
options = webdriver.ChromeOptions()
# change this one according to where you have the binary
options.binary_location = "D:\workspace\chrome.sync\Chrome-bin\chrome.exe"
options.headless = True
options.add_argument("--log-level=3")
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s, options=options)
driver.maximize_window()
driver.set_window_size(1700, 1700) # you can change this based on your PC
def login(email, password):
"""
opens the login modal, enter the login details and submit the form
@returns true for success and Excpetion otherwise
"""
try:
driver.get(base_url)
sleep(7)
# # open login modal
driver.execute_script(
'document.querySelector(".logged-out > div > button").click()')
sleep(2)
# enter email and password
driver.execute_script(
'document.querySelector("form")[0].value = "'+email+'"')
driver.execute_script(
'document.querySelector("form")[1].value = "'+password+'"')
# submit the form
driver.execute_script('document.querySelector("form")[3].click()')
return True
except Exception as e:
print("Login Error: ")
print(e)
return e
def getCourseContentsLinks():
"""
loop throught the course contents, get the categories and the lessons for each category
@returns: a dict
"""
menu = driver.execute_script("""
// category and links indexes will be the same here
let data = {
totalLessons: 0,
categories: [],
links: []
}
let categories = document.querySelectorAll(".course-category-hover")
for(let i = 0; i < categories.length; i++){
let category = categories[i].parentNode.querySelector("h5").textContent
let menuLinks = categories[i].parentNode.querySelectorAll("menu a")
let menu = []
for(let j = 0; j < menuLinks.length; j++){
menu.push({
name: menuLinks[j].querySelector('span').textContent,
url: menuLinks[j].getAttribute("href")
})
data.totalLessons += 1
}
data.categories.push(category)
data.links.push(menu)
}
return data
""")
# return the data object
return dict(menu)
def cleanFileName(name):
return str(name).replace(":", "-").replace("?", "")
def downloadCourse(whereToSaveIt, url):
# url example: https://www.educative.io/courses/grokking-computer-networking
"""
navigate to the lesson link, take a screenshot and convert it to pdf
@params: <url> = course url, example is above
@returns: a stats dict which has the total lessons and the successfully downloaded files.
"""
try:
driver.get(url)
sleep(1)
courseName = driver.execute_script("return document.title")
pathToSaveEverything = os.path.join(whereToSaveIt, courseName)
Path(pathToSaveEverything).mkdir(parents=True, exist_ok=True)
downloadedFiles = 0
content = getCourseContentsLinks()
stats = {
"totalLessons": content.get("totalLessons"),
"totalDownloaded": 0
}
for i, categoryName in enumerate(content.get("categories")):
print("\n")
print(categoryName)
print("-------------------------------------")
courseCategoryDir = os.path.join(
pathToSaveEverything, cleanFileName(categoryName))
Path(courseCategoryDir).mkdir(parents=True, exist_ok=True)
for course in content.get("links")[i]:
pdfFileName = cleanFileName(course.get("name") + ".pdf")
pdfFilePath = os.path.join(courseCategoryDir, pdfFileName)
screenShotImgName = cleanFileName(course.get("name")+".png")
screenShotImgPath = os.path.join(
courseCategoryDir, screenShotImgName)
isScreenShoted = takeScreenShot(
base_url+course.get("url"), screenShotImgPath)
# screenshot is not taken
if isScreenShoted == False:
print("NOT Downloaded: [" +screenShotImgName+"]")
continue
with open(pdfFilePath, "wb") as f:
img2pdf
f.write(img2pdf.convert(screenShotImgPath))
os.remove(screenShotImgPath)
print("Downloaded: [" + course.get("name")+"]")
downloadedFiles += 1
stats["totalDownloaded"] = downloadedFiles
return stats
except Exception as e:
print(e)
return e
def takeScreenShot(url, savingPath):
"""
@params: <url> which is the course url
@returns: True if success else it returns False
"""
try:
driver.get(url)
sleep(10)
screenShotFileName = driver.execute_script("return document.title")
def getWindowLen(X): return driver.execute_script(
"return document.body.parentNode.scroll"+X)
driver.set_window_size(1700, getWindowLen("Height"))
isSaved = driver.find_element_by_tag_name(
"body").screenshot(savingPath)
if isSaved == False:
# print(f"\n\n NOT SAVED ({savingPath}) !!! \n\n")
return False
sleep(1)
except Exception as e:
return False
if __name__ == "__main__":
try:
# getting the data
print("\n\n ------------------------------------------------------- \n")
email = input("Your educative.io email: ")
password = input("your password: ")
print("\n")
courseUrl = input("Course url: ")
saveDir = input("Path to save this course: ")
print("\n ------------------------------------------------------- \n")
# the real stuff
result = login(email, password)
if result != True:
print("LOGIN ERROR")
raise Exception()
print("\n LOGGING IN... \n")
sleep(3) # wait until we login
print("\n DOWNLOADING... \n")
downloadStats = downloadCourse(saveDir, courseUrl)
print(f"""
\n FINISHED. \n
Total lessons: {downloadStats.get("totalLessons")}
\n
Total downloaded: {downloadStats.get("totalDownloaded")}
""")
except Exception as e:
print("\n FAILED !!")
print(e)
finally:
driver.quit()