-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrap_data.py
144 lines (118 loc) · 5.36 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
# 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 traceback
from datetime import datetime
from pprint import pprint
from time import sleep
from typing import List
import selenium.webdriver.remote.webelement
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
# CONSTANTS
FOLDER_BUTTON_XPATH = "/html/body/app/div[1]/main/ng-component/div[1]/div/problemset-tree/div[1]/div[2]/div/tree-root/tree-viewport/div/div/tree-node-collection/div/tree-node/div/tree-node-wrapper/div/div/div/div/button"
QUESTIONS_XPATH = "/html/body/app/div[1]/main/ng-component/div[1]/div/problemset-tree/div[1]/div[2]/div/tree-root/tree-viewport/div/div/tree-node-collection/div/tree-node/div/tree-node-children/div/tree-node-collection/div/tree-node/div/tree-node-wrapper/div/div/div"
QUESTION_TITLE_XPATH = "div[1]/button/span[4]"
PROBLEM_NUMBER_XPATH = "div[1]/button/span[2]"
DUE_DATE_XPATH = "div[2]/div[1]/div/span/span[2]"
STATUS_XPATH = "div[2]/div[4]/div/span[2]"
DEBUG = False
# ========
def main(driver: webdriver.Chrome):
print("A new CHROME browser window will open with the codezinger link in it")
print("You have to enter your login and password.")
# input("Press enter to continue... ")
login_codezinger(driver)
expand_all_labs(driver)
data = get_data(driver)
pprint(data)
def login_codezinger(driver: webdriver.Chrome, username: str = "", password: str = ""):
driver.get("https://labs.codezinger.com/student/classes/611dc47ba6ae540012d2130f")
# Auto login
if DEBUG or (username and password):
while "login" not in driver.title.lower():
sleep(1)
email = driver.find_element_by_xpath(
"/html/body/app/div[1]/main/ng-component/section/div/div[1]/div/div/form/div[1]/div/div/input")
email.send_keys(username)
pwd = driver.find_element_by_xpath(
"/html/body/app/div[1]/main/ng-component/section/div/div[1]/div/div/form/div[2]/div/div/input")
pwd.send_keys(password)
submit = driver.find_element_by_xpath(
"/html/body/app/div[1]/main/ng-component/section/div/div[1]/div/div/form/div[3]/div/button")
submit.click()
sleep(1)
try:
yes = driver.find_element_by_xpath("/html/body/app/div[1]/main/ng-component/div/div/div/div[3]/button[2]")
yes.click()
except NoSuchElementException:
pass
# ------
while "login" in driver.title.lower():
sleep(1)
print("logged in")
# driver.get("https://labs.codezinger.com/student/classes/611dc47ba6ae540012d2130f")
def expand_all_labs(driver: webdriver.Chrome):
buttons = []
while not buttons:
buttons = driver.find_elements_by_xpath(FOLDER_BUTTON_XPATH)
# print(buttons)
for button in buttons:
button.click()
print("Expanded", len(buttons), "folders.")
def get_data(driver: webdriver.Chrome) -> List[dict]:
data_list: List[dict, ...] = []
questions = driver.find_elements_by_xpath(QUESTIONS_XPATH)
print()
question: selenium.webdriver.remote.webelement.WebElement
for index, question in enumerate(questions):
print("Processing data... ({:3.0%})".format(index/len(questions)), end="\r")
problem_no = safe_find_element_by_xpath(question, PROBLEM_NUMBER_XPATH)
question_title = safe_find_element_by_xpath(question, QUESTION_TITLE_XPATH)
due_date = safe_find_element_by_xpath(question, DUE_DATE_XPATH).rstrip(" /-")
status = safe_find_element_by_xpath(question, STATUS_XPATH)
try:
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()
except ValueError:
parsed_date = "NULL"
data = {
"problem_desc": problem_no + " " + question_title,
"assigned_date": "NULL",
"submission_date": parsed_date,
"status": status == "Submitted" if status != "NULL" else status
}
data_list.append(data)
print()
print("Scraped", len(questions), "questions")
return data_list
def safe_find_element_by_xpath(element: selenium.webdriver.remote.webelement.WebElement, xpath: str) -> str:
result: str
try:
result = element.find_element_by_xpath(xpath).text
except NoSuchElementException:
result = "NULL"
return result
if __name__ == '__main__':
driver = webdriver.Chrome()
try:
main(driver)
x = input("enter to close....")
if x:
raise KeyboardInterrupt
except KeyboardInterrupt:
driver.close()
except Exception as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
driver.close()