-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
113 lines (87 loc) · 2.85 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
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from datetime import datetime
import requests
import time
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('config.ini')
# open driver
driver = webdriver.Chrome(config.get('Webdriver', 'path'))
# date params
arrival_month = 'Jul'
arrival_day = '21st'
num_nights = '2'
# urls
url = 'https://www.midnrreservations.com/HoffmasterStatePark/'
reserve_url = 'https://www.midnrreservations.com/HoffmasterStatePark?Map'
# other constants
all_camp_sites = ['1-132,C1-C2?Map', '133-293,B1-B2?Map'] # camping areas
time_last_email_sent = False
email_notification_interval = 180 # 3 minutes
def prettify_time(timestamp):
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%dT%H:%M:%S')
def send_email(sites, timestamp):
print 'Sending email...'
payload = {
'secret' : config.get('Email', 'secret'),
'website_url' : reserve_url,
'timestamp' : prettify_time(timestamp),
'camp_sites' : ', '.join(sites),
'addresses' : config.get('Email', 'addresses'),
}
print payload
r = requests.post(config.get('Email', 'api_url'), payload)
print r.text
def get_avail(site_range_url):
# access website
driver.get(url+site_range_url)
# config month element
elem_month = Select(driver.find_element_by_name('selArrMth'))
elem_month.select_by_visible_text(arrival_month)
# config day element
elem_day = Select(driver.find_element_by_name('selArrDay'))
elem_day.select_by_visible_text(arrival_day)
# config number-of-nights element
elem_nights = Select(driver.find_element_by_name('selNumNights'))
elem_nights.select_by_visible_text(num_nights)
print 'page loading...(%s)'%(site_range_url)
time.sleep(3)
# css select the available dots on ze map!
avail_sites = driver.find_elements_by_css_selector('div#map > div[id^=rce_] > .avail:first-child')
out = []
for node in avail_sites:
parent = node.find_element_by_xpath('..')
site_num = parent.get_attribute('id').split('_')[1]
if site_num.isdigit():
out.append(site_num)
return out
def run():
cnt = 0
avail = []
now = time.time()
for s in all_camp_sites:
avail += get_avail(s)
time.sleep(3)
for site_num in avail:
cnt += 1
print site_num + ',',
if not cnt:
print '\nThere are NO available sites :('
else:
print '\n\n%s site(s) AVAILABLE !!!!!!!!!'%(cnt)
# only send email if it has been over n minutes
global time_last_email_sent
if not time_last_email_sent or (now - time_last_email_sent) >= email_notification_interval:
time_last_email_sent = now
send_email(avail, now)
print prettify_time(now)
print '-----------------------------------------------\n\n'
if __name__ == "__main__":
while True:
try:
run()
except KeyboardInterrupt:
driver.close()
print 'shutting down.'
break