-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (166 loc) · 6.23 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
229
230
231
232
233
from os import getenv
from pathlib import Path
from time import sleep
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
class GCScraper:
def __init__(
self, email, password, downloadDirectory
):
self.email = email
self.password = password
self.prefs = {
"download.default_directory": downloadDirectory,
'download.directory_upgrade': True
}
self.options = webdriver.ChromeOptions()
self.options.add_experimental_option("prefs", self.prefs)
self.service = Service()
self.driver = webdriver.Chrome(
service=self.service, options=self.options
)
self.driver.maximize_window()
self.LOGIN_URL = r"https://accounts.google.com/v3/signin/identifier"
self.LOGIN_URL += r"?continue=https%3A%2F%2Fclassroom.google.com"
self.LOGIN_URL += r"&passive=true&flowName=GlifWebSignIn"
self.LOGIN_URL += r"&flowEntry=ServiceLogin&theme=glif"
self.LOGIN_URL += r"&dsh=S-469499581%3A1699752740709820"
def login(self):
try:
self.driver.get(self.LOGIN_URL)
self.driver.implicitly_wait(15)
self.driver.find_element(
"xpath", '//*[@id="identifierId"]'
).send_keys(self.email)
self.driver.find_elements(
"xpath", '//*[@id="identifierNext"]'
)[0].click()
sleep(2)
self.driver.find_element(
"xpath", '//*[@id="password"]/div[1]/div/div[1]/input'
).send_keys(self.password)
self.driver.find_elements(
"xpath", '//*[@id="passwordNext"]'
)[0].click()
sleep(5)
self.gcURL = self.driver.current_url
print("Login Successful")
except Exception as e:
print("Login Failed")
print("Error:", e)
def getDriver(self):
return self.driver
def findCourse(self, courseTitle):
try:
sleep(10)
course = self.driver.find_element(
"xpath", f'//div[contains(text(), "{courseTitle}") and @class="YVvGBb z3vRcc-ZoZQ1"]'
)
self.courseTitle = course.text
course.click()
classwork = self.driver.find_element(
"xpath", '//a[contains(text(), "Classwork")]'
)
classwork.click()
print("Course Found -", self.courseTitle)
except Exception as e:
print("Course Not Found")
print("Error:", e)
def getLinks(self):
sleep(10)
links = []
self.driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);"
)
sleep(5)
viewMore = self.driver.execute_script(
"return document.getElementsByClassName('VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-dgl2Hf ksBjEc lKxP2d LQeN7 nZ34k')"
)
if viewMore:
self.driver.execute_script("arguments[0].click()", viewMore[0])
sleep(2)
self.driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);"
)
sleep(5)
posts = self.driver.execute_script(
"return document.getElementsByClassName('xVnXCf QRiHXd')"
)
print(f"Found {len(posts)} Posts")
self.driver.implicitly_wait(5)
for post in posts:
self.driver.execute_script(
"arguments[0].scrollIntoView(true);", post
)
self.driver.execute_script("arguments[0].click();", post)
sleep(5)
materials = self.driver.execute_script(
"return document.getElementsByClassName('pOf0gc QRiHXd Aopndd M4LFnf');"
)
print(f"Found {len(materials)} Materials")
self.driver.implicitly_wait(5)
for material in materials:
anchors = material.find_elements(By.TAG_NAME, "a")
for anchor in anchors:
links.append(anchor.get_attribute("href"))
return self.courseTitle, links
def close(self):
self.driver.close()
class Downloader:
def __init__(self, courseTitle, links, driver):
self.courseTitle = courseTitle
self.links = links
self.driver = driver
def classifyLinks(self):
self.driveLinks = [link for link in self.links if link.split(
"/")[2] == "drive.google.com"]
self.otherLinks = [
link for link in self.links if link not in self.driveLinks
]
def download(self, downloadDirectory):
otherLinks = '\n'.join(self.otherLinks)
directory = Path(downloadDirectory)
if not directory.exists():
directory.mkdir(parents=False, exist_ok=False)
file = Path(f"{downloadDirectory}/{self.courseTitle}.txt")
file.touch(exist_ok=True)
file.write_text(otherLinks)
print("Saved Other Links\n")
print("Starting Download...\n")
for link in self.driveLinks:
print("Downloading:", link)
self.driver.get(
f"https://drive.google.com/uc?export=download&id={link.split('/')[5]}"
)
sleep(5)
print("Downloaded\n")
if __name__ == "__main__":
load_dotenv()
course_list = list(
map(lambda x: x[1:-1], getenv("COURSE_LIST")[1:-1].split(", "))
)
print(course_list)
email = getenv("EMAIL")
password = getenv("PASSWORD")
downloadDirectory = getenv("DOWNLOAD_DIRECTORY")
gcscraper = GCScraper(
email=email,
password=password,
downloadDirectory=downloadDirectory
)
gcscraper.login()
driver = gcscraper.getDriver()
for course in course_list:
print(course)
gcscraper.findCourse(courseTitle=course)
ct, links = gcscraper.getLinks()
downloader = Downloader(courseTitle=ct, links=links, driver=driver)
downloader.classifyLinks()
downloader.download(downloadDirectory=downloadDirectory)
driver.get(gcscraper.gcURL)
print("Waiting for any remaining downloads to finish...")
sleep(300)
print("Done")
gcscraper.close()