This repository was archived by the owner on Oct 1, 2022. It is now read-only.
forked from Ya-s-h/db-lab-autmation-hax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap_data.py
218 lines (174 loc) · 7.06 KB
/
scrap_data.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
# Copyright (c) 2021 RoguedBear
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import asyncio
import json
import traceback
from datetime import datetime
from pprint import pprint
from time import sleep
from typing import List
from pyppeteer import launch
from pyppeteer.element_handle import ElementHandle
from pyppeteer.page import Page
# CONSTANTS
DEBUG = False
with open("xpaths.json") as file:
XPATHS = json.load(file)
# ========
async def login_codezinger(page: Page, username, password, link=""):
await set_cookies(page)
if link == "":
link = "https://labs.codezinger.com/student/dashboard"
await page.goto(link)
if "login" not in (await page.title()).lower():
print("Logged in. allegedly")
return
if DEBUG or (username and password):
print("Cookie login didn't work, trying manual login")
while "login" not in (await page.title()).lower():
sleep(1)
email = (await page.xpath(XPATHS["LOGIN_EMAIL_INPUT"]))[0]
# await email.click()
await email.type(username)
pwd = (await page.xpath(XPATHS["LOGIN_PASSWD_INPUT"]))[0]
# await pwd.click()
await pwd.type(password)
submit = (await page.xpath(XPATHS["LOGIN_SUBMIT_BTN"]))[0]
await submit.click()
sleep(1)
try:
yes = await page.xpath(XPATHS["LOGIN_FORCE_YES_BTN"])
print(yes)
await yes[0].click()
except IndexError as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
print("force login option not available...")
pass
while "login" in (await page.title()).lower():
sleep(1)
print("logged in")
await save_cookies(page)
async def sort_pending_by_due_date(page: Page):
exception = True
while exception:
try:
exception = False
pending_text_button = []
while not pending_text_button:
pending_text_button = await page.xpath(XPATHS["PENDING_DUE_DATE_FILTER"])
sleep(0.1)
pending_text = await page.evaluate('(node) => node.textContent', pending_text_button[0])
if pending_text != "Pending By Due Date":
await pending_text_button[0].click()
due_by_pending_btn = (await page.xpath(XPATHS["PENDING_DUE_DATE_BTN"]))[0]
await due_by_pending_btn.click()
except Exception as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
exception = True
async def keep_clicking_load_more(page: Page):
async def get_load_more() -> list[ElementHandle]:
return await page.xpath(XPATHS["LOAD_MORE_BTN"])
# wait for the button to load first
button = []
while not button:
button = await get_load_more()
sleep(0.05)
while button := await get_load_more():
await button[0].click()
sleep(0.5)
async def get_data(page: Page) -> List[dict]:
# TODO: convert this to async
data_list: List[dict, ...] = []
questions = await page.xpath(XPATHS["QUESTION_ROW"])
print(questions)
print(len(questions))
question: ElementHandle
for index, question in enumerate(questions):
print("Processing data... ({:3.0%})".format(index / len(questions)), end="\r")
question_title = await question.xpath(XPATHS["RELATIVE_QUESTION"])
codezinger_class = await question.xpath(XPATHS["RELATIVE_CLASS"])
question_type = await question.xpath(XPATHS["RELATIVE_Q_TYPE"])
due_date = (await question.xpath(XPATHS["RELATIVE_DUE_DATE"]))
try:
due_date = await page.evaluate('node => node.getAttribute("data-bs-original-title")', due_date[0])
due_date = due_date.strip("Due: <br/>Late -")
# print(due_date)
parsed_date = datetime.strptime(due_date, "%d %b, %I:%M %p")
parsed_date = parsed_date.replace(year=datetime.now().year)
# parsed_date = parsed_date.isoformat()
# print(index, parsed_date)
except ValueError:
parsed_date = datetime.min
data_list.append({
"question": await get_text(page, question_title[0]),
"class": await get_text(page, codezinger_class[0]),
"question_type": await get_text(page, question_type[0]),
"due_date": parsed_date
})
print("Scraped", len(questions), "questions")
return data_list
async def set_cookies(page: Page):
# try to load cookies and refresh
try:
with open("cookies.json") as f:
cookies = json.load(f)
await page.setCookie(*cookies)
except FileNotFoundError:
return
async def save_cookies(page: Page):
# save cookies
cookies = await page.cookies()
with open("cookies.json", "w") as file:
json.dump(cookies, file)
async def get_text(page: Page, element: ElementHandle):
return (await page.evaluate('(node) => node.textContent', element)).strip()
async def main(email="", password="", chrome_path="", link="", **kwargs):
try:
print("Starting browser...")
browser = await launch(executablePath=chrome_path,
headless=True,
options={'args': ['--no-sandbox', '--disable-gpu']})
print("browser started")
page = await browser.newPage()
print("logging in.")
await login_codezinger(page, email, password, link)
print("sorting")
await sort_pending_by_due_date(page)
print("loading more")
await keep_clicking_load_more(page)
print("Processing stuff")
output = await get_data(page)
print("awaiting browser close")
# await page.screenshot({'path': 'example.png'})
await browser.close()
print("Browser closed")
# pprint(output)
return output
except Exception as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
await browser.close()
print("ERROR! (browser closed), ", ex)
if __name__ == '__main__':
import os
from dotenv import load_dotenv
load_dotenv()
class DateTimeEncoder(json.JSONEncoder):
def default(self, z):
if isinstance(z, datetime):
return str(z)
else:
return super().default(z)
data = asyncio.run(main(os.getenv("EMAIL"), os.getenv("PASSWORD"), os.getenv("CHROME_PATH")))
with open("test_data.json", "w") as file:
json.dump(data, file, cls=DateTimeEncoder)