-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (130 loc) · 5.68 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
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from requests import request, Session
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
import undetected_chromedriver.v2 as uc
import time
import sms
def open_day(month: str, days: list, open_days: list):
count = 1
open_day_exists = False
for day in days:
if day != "50":
print("Got a " + month + " day: " + str(count))
open_days.append(month + " " + str(count))
open_day_exists = True
count = count + 1
return open_day_exists
def parse(driver):
try:
# Landing Page
driver.get('https://permit.pcta.org/application')
time.sleep(2)
# Sometimes it brings you to the start permit process page, without cookies?
continue_button = driver.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/a')
continue_button.click()
time.sleep(2)
# Click Continue Button
continue_button = driver.find_element(By.XPATH, '//*[@id="permit-form"]/div/div')
continue_button.click()
time.sleep(2)
# driver.get_screenshot_as_file('test.png')
# Select a Start Location (Has to be first)
start_location_dropdown = driver.find_element(By.ID, 'start_location_id')
start_select = Select(start_location_dropdown)
start_select.select_by_value("1") # Mexico Border
time.sleep(1)
# Select an End Location (Had to be last)
end_location_dropdown = driver.find_element(By.ID, 'end_location_id')
end_select = Select(end_location_dropdown)
end_select.select_by_value("116") # Canadian Border
time.sleep(1)
# Click Continue Button
continue_button = driver.find_element(By.XPATH, '//*[@id="permit-form"]/div[4]/div[1]')
continue_button.click()
time.sleep(2)
# Get all Days in March
row1xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[1]/div[2]/table/tbody/tr'
row2xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[2]/div[2]/table/tbody/tr'
row3xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[3]/div[2]/table/tbody/tr'
row4xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[4]/div[2]/table/tbody/tr'
row5xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[5]/div[2]/table/tbody/tr'
row6xp = '//*[@id="calendar"]/div[2]/div/table/tbody/tr/td/div/div/div[6]/div[2]/table/tbody/tr'
row_xpaths = list()
row_xpaths.append(row1xp)
row_xpaths.append(row2xp)
row_xpaths.append(row3xp)
row_xpaths.append(row4xp)
row_xpaths.append(row5xp)
row_xpaths.append(row6xp)
march_days = list()
april_days = list()
may_days = list()
# Get all days in March Calendar
for row_xpath in row_xpaths:
row = driver.find_element(By.XPATH, row_xpath)
nums = row.find_elements(By.CLASS_NAME, "fc-title")
for num in nums:
#print(repr(num))
#print(num.text)
march_days.append(num.text)
print(len(march_days))
# Get all days in April Calendar
next_month_button = driver.find_element(By.XPATH, '//*[@id="calendar"]/div[1]/div[3]/div/button[2]')
next_month_button.click()
time.sleep(2)
for row_xpath in row_xpaths:
row = driver.find_element(By.XPATH, row_xpath)
nums = row.find_elements(By.CLASS_NAME, "fc-title")
for num in nums:
#print(repr(num))
#print(num.text)
april_days.append(num.text)
print(len(april_days))
# Get all days in May Calendar
next_month_button = driver.find_element(By.XPATH, '//*[@id="calendar"]/div[1]/div[3]/div/button[2]')
next_month_button.click()
time.sleep(2)
for row_xpath in row_xpaths:
row = driver.find_element(By.XPATH, row_xpath)
nums = row.find_elements(By.CLASS_NAME, "fc-title")
for num in nums:
# print(repr(num))
# print(num.text)
may_days.append(num.text)
print(len(may_days))
driver.delete_all_cookies()
driver.quit()
dates = {
'March': march_days,
'April': april_days,
'May': may_days
}
return dates
except Exception as e:
print("!!! Got an exception: " + str(e))
return dict()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
options = uc.ChromeOptions()
options.headless = False
driver = uc.Chrome(options=options)
attempts = 1
while True:
print("Parsing attempt: " + str(attempts))
month_day_dict = parse(driver)
for month in month_day_dict:
open_days = []
if open_day(month, month_day_dict[month], open_days):
print("!!! Got at least one open day!")
text = "The following day(s) are open: "
for day in open_days:
text += day
text += ", "
sms.send(text)
time.sleep(120) # Rerun loop every 2 minutes.
attempts = attempts + 1