-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_screenshots.py
162 lines (116 loc) · 5.86 KB
/
schedule_screenshots.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
"""
===============================================================================
Filename: schedule_screenshots.py
Description:
For all groups that are avaiable on https://plan.polsl.pl/, script finds
up-to-date schedule, takes screenshot and saves as a .png file in a given
folder. File name is the name of group (‘/’ replaced with ‘$’).
Input parameters:
- folder path to save screenshots
- location with up-to-date browser driver
Output:
- info. about start and finish time, program progress
- 0 - successful completion
- error info. string - error
===============================================================================
"""
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from bs4 import BeautifulSoup
from datetime import datetime, date
import sys
# Debug purposes
from selenium.webdriver.chrome.options import Options
if __name__ == "__main__":
start = datetime.now()
print('\nStart time:', start)
# Setup webdriver
try:
s = Service(sys.argv[2])
op = webdriver.EdgeOptions()
op.add_argument('headless')
driver = webdriver.Edge(service=s, options=op)
driver.set_window_size(2160, 1280)
driver.maximize_window()
# Get the webpage
try:
driver.get("https://plan.polsl.pl/")
# Retrieve linked texts of all groups. Linked texts are going to be used to get to every group schedule.
# Do the screenshot of the schedule. Save screenshot with a name of the group ('/' replaced with '$').
try:
driver.switch_to.frame("page_content")
# Uncheck "Teacher".
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/table[2]/tbody/tr[2]/td[2]/input[2]")))
element.click()
# Click "Search".
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/table[2]/tbody/tr[3]/td/center/input")))
element.click()
# Get HTML content of the page.
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
# Extract demanded part of the HTML content (link texts).
link_texts = soup.find("div", {"id": "result_plan"}).find_all("a")
# Needed for progress info.
group_no = 0
groups_quantity = len(link_texts)
for link_text in link_texts:
# Print progress info.
print("Group no.", group_no, "of", groups_quantity)
# Click the result link.
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.LINK_TEXT, link_text.string)))
element.click()
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.ID, "wBWeek")))
element = Select(element)
# Select current week.
current_day = date.today().day
current_month = date.today().month
weeks = element.options[1:]
for week in weeks:
if(int(week.text[3:5]) == current_month and int(week.text[0:2]) <= current_day and
int(week.text[6:8]) >= current_day):
week.click()
break
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.ID, "wBButton")))
element.click()
# Do the screenshot.
# Retrive group name.
group = link_text.string
# Create filepath (folder path provided as a argument + '\' +
# + group name retrived from the webpage ('/' replaced with '$') + '.png').
filepath = sys.argv[1] + '\\' + group.replace('/', '$') + '.png'
# Do the screenshot.
driver.save_screenshot(filepath)
# Get back to page with all groups.
driver.get("https://plan.polsl.pl/")
driver.switch_to.frame("page_content")
# Uncheck "Teacher".
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/table[2]/tbody/tr[2]/td[2]/input[2]")))
element.click()
# Click "Search".
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((
By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/table[2]/tbody/tr[3]/td/center/input")))
element.click()
group_no = group_no + 1
print(0)
except:
print("Something went wrong when getting the screenshots.")
except:
print("Something went wrong when getting the schedule webpage.")
# Quit the driver instance.
driver.quit()
except:
print("Something went wrong when setting up the webdriver.")
finish = datetime.now()
print('Finish time:', finish)
duration = finish - start
print('Duration:', duration)