-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
442 lines (344 loc) · 12.4 KB
/
Main.py
File metadata and controls
442 lines (344 loc) · 12.4 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/python
# Name: StudyBug
# Author(s): Grant McGovern & Gaurav Sheni
# Date: 16 March 2013
#
# URL: www.github.com/g12mcgov/studybug
#
#
import os
import csv
import sys
import time
import socket
import urllib2
import logging
import operator
import datetime
import ConfigParser
from timeit import Timer
from bs4 import BeautifulSoup
from selenium import webdriver
from multiprocessing import Pool
from collections import OrderedDict
from pyvirtualdisplay import Display
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
sys.path.append('helpers')
sys.path.append('loggings')
## Local Includes ##
from user import User
from schema import XPathSchema
from emailsend import sendEmail
from loggings.loger import configLogger
from helpers.helper import chunk, parseTime
def main():
# To indicate a new log-block
logger.info("-------- NEW LOG BLOCK ---------------")
log_start = datetime.datetime.now()
logger.info(" beginning StudyBug at " + str(log_start))
global url
global selenium_timeout
# Get date 5 days ahead
date = getDate()
# Setup our configuration parameters
configs = getConfig()
url = str(configs[0] + date)
room = "room-" + str(configs[1])
startTime = str(configs[2])
endTime = str(configs[3])
selenium_timeout = int(configs[4])
email = str(configs[5])
password = str(configs[6])
# Reads in user info
rows = readIn()
# Pulls down HTML
HTML = htmlFetch(url)
# Discovers available rooms
rooms = availability(room, HTML, startTime, endTime)
if not rooms:
logger.warning(" no available rooms at all")
return
# Creates a list of User objects, each with 4 time-slots to book
users = matchUsers(rows, rooms)
for user in users:
logger.info(" user: " + user.username + " xpaths: " + str(user.xpath))
if not users:
logger.warning(" no rooms for time constraint")
return
else:
logger.info(" total users: " + str(len(users)))
logger.info(" creating thread pool... ")
# Create a threading pool
pool = Pool(processes=4)
pool.map(bookRooms, users)
logger.info(" Executed in " + str(datetime.datetime.now() - log_start) + " seconds")
# Lastly, confirm our reservations
confirmed_times = confirm(url, room, rows)
# Send email with our reserved rooms
sendEmail(confirmed_times, room, email, password, startTime, endTime)
logger.info("------------------------")
def bookRooms(user):
logger.info(" " + user.username + " - booking rooms")
if not user:
logger.error(" " + user.username + " - NO AVAILABLE TIMES")
else:
driver = webdriver.PhantomJS()
driver.get(url)
# This is a PhantomJS bug remedied by the following method call... should
# look into a fix for this.
driver.set_window_size(2000, 2000)
wait = WebDriverWait(driver, selenium_timeout)
if not user.xpath:
pass
else:
success = False
# Sometimes Selenium will load the page faster than the javascript
# or sometimes the web page will timeout (most likely due to connection
# issues). This way, we keep trying, but if we have success, we break out.
for attempt in range(10):
for item in user.xpath:
try:
logger.info(" " + user.username + " - clicking on individual rooms...")
logger.info(" " + user.username + " - clicking on " + item['xpath'])
# Xpath looks like this: //*[@id=room-225]/dd[5]
element = wait.until(EC.presence_of_element_located((By.XPATH, item['xpath'])))
#element = driver.find_element_by_xpath(str(item['xpath']))
element.click()
success = True
logger.info(" " + user.username + " - successfully clicked on all rooms")
except NoSuchElementException:
logger.error(" " + user.username + "- COULDN'T CLICK ON ELEMENT " + str(item['xpath']))
try:
logger.info(" " + user.username + " - clicking on save for user")
save_box = wait.until(EC.presence_of_element_located((By.XPATH, "id('save')")))
#driver.find_element_by_xpath("id('save')").click()
save_box.click()
logger.info(" " + user.username + " - filling in username ")
user_name_box = wait.until(EC.presence_of_element_located((By.ID, "username")))
#user_name_box = driver.find_element_by_id("username")
user_name_box.send_keys(user.username)
logger.info(" " + user.username + " - filling in password ")
password_box = wait.until(EC.presence_of_element_located((By.ID, "password")))
#password_box = driver.find_element_by_id("password")
password_box.send_keys(user.key)
logger.info(" " + user.username + " - clicking on submit ")
reserve_button = wait.until(EC.presence_of_element_located((By.ID, "submit")))
#reserve_button = driver.find_element_by_id("submit")
reserve_button.click()
if success == True: break
except NoSuchElementException as err:
logger.error(" " + user.username + "FAILED")
logger.error(err)
except TimeoutException as err:
logger.error(" " + user.username + "FAILED")
logger.error(err)
# Close PhantomJS
driver.quit()
# Will eventually be used to change the IP address every time it's run
def IPfetch():
hostname = socket.gethostname()
ip_address = socket.gethostbyname(socket.gethostname())
logger.info(" hostname: " + hostname)
logger.info(" IP address: " + ip_address)
return (hostname, ip_address)
# Extracts HTML from ZSR Website
def htmlFetch(url):
logger.info(" requesting " + url)
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
if soup:
logger.info( "recieved HTML")
return soup
# Determines what rooms are available
def availability(room, soup, startTime, endTime):
# schema = XPathSchema()
rooms = []
# Returns a HTML code block as such:
# <dd class="cell even open">
# <input id="srr-1-1420839000" name="srr-1-1420839000" type="checkbox" value="Y"/>
# <label for="srr-1-1420839000">
# <span class="room-name">
# Room 225
# </span>
# <span class="time-slot">
# 4:30 PM
# </span>
# </label>
# <i class="drag-handle">
# </i>
# </dd>
# Sometimes BeautifulSoup attempts to find the elements before the page
# has completely loaded. This is a hacky way of ensuring the page has been
# loaded by recursively attempting to accessing content for 2 minutes before
# timing out.
for attempt in range(120):
try:
# Find all rooms on the grid which are open
blocks = [block for block in soup.find(id=room).select('dd') if "unavailable" not in block.text]
if blocks:
break
else:
pass
except AttributeError as err:
logger.error("Could not click on element")
logger.error(err)
time.sleep(1)
# Extract our times from the config file
start = parseTime(startTime)
end = parseTime(endTime)
# Get the first element on the grid
starting_time = parseTime(blocks[0].find('span', {'class': 'time-slot'}).get_text())
# Get the last element on the grid
ending_time = parseTime(blocks[-1].find('span', {'class': 'time-slot'}).get_text())
# If we configured a time later than the last possible one, limit it
if ending_time < end:
end = ending_time
# Calculate our starting time point by finding the distance between the two times.
difference = start - starting_time
# If by some chance, our xpaths start at the time we've configured, we don't need
# calculate the half hours because the xpaths we want to click on will start at 0.
if not difference:
i = 0
else:
# Breaks up the difference into half-hour blocks
halfHours = (difference.seconds / 60 / 60)*2
## Increment i (our XPath index until we hit the startime time)
i = 1
while (i != halfHours): i += 1
for block in blocks:
status = ' '.join(block.get('class'))
time_ = block.find('span', {'class': 'time-slot'}).get_text()
# Only assign rooms between the above hours
if start <= parseTime(time_) <= end:
# Pre-increment by 1, no idea why, but I should find out
i += 1
## Check to make sure room is open
if "open" in status:
rooms.append({
"room": room,
"status": status,
"time": time_,
"xpath": "//*[@id='%s']/dd[%i]" % (room, i) # schema.getXpath(time)
})
else:
pass
# Just to see how many rooms were available
if len(rooms) < 1:
logger.warning(
" no available time slots for room: " + room + " between " + startTime + " and " + endTime
)
logger.warning("Exiting...")
return
else:
logger.info(" total available time slots: " + str(len(rooms)))
# Returns a list of dicts of rooms as such:
# {'status': u'cell odd open', 'xpath': "id('room-203a')/x:dd[17]", 'room': 'room-225', 'time': u'4:00 PM'}
# {'status': u'cell even open', 'xpath': "id('room-203a')/x:dd[18]", 'room': 'room-225', 'time': u'4:30 PM'}
return rooms
## Assigns 4 timeslots to each user
def matchUsers(rows, rooms):
## Check for white space in csv file
rooms = chunk(rooms, 4)
for room in rooms:
logger.info('\n')
logger.info(room)
userdicts = []
for row, room in zip(rows, rooms):
userdicts.append({
"username": row[0],
"password": row[1],
"xpath": room
})
# Create a list of users, based on the above dict
Users = [User(dictionary) for dictionary in userdicts]
for user in Users:
if user.xpath:
logger.info(" assigned " + str(len(user.xpath)) + " chunks to " + user.username)
else:
logger.info(" did not assign any chunks to " + user.username)
return Users
## Calculates next available date (5 days ahead)
def getDate():
now = datetime.datetime.now()
startdate = now.strftime("%Y/%m/%d")
date = datetime.datetime.strptime(startdate, "%Y/%m/%d")
endate = date + datetime.timedelta(days=4)
formattedTime = endate.strftime("%Y/%m/%d")
# Example format = 2014/04/17
return formattedTime
## Read in user credentials from config file and create user Objects
def readIn():
with open("credentials/credentials.csv") as csvfile:
credentials_reader = csv.reader(csvfile, delimiter=',')
rows = [row for row in credentials_reader]
# Returns the rows of the credentials csv file:
#
# [
# mcgoga12, changeme
# guarav12, changeme1
# ben1234, changeme2
# ]
#
return rows
## Sets up config for program
def getConfig():
config = ConfigParser.RawConfigParser()
config.readfp(open('config/studybug.cfg'))
url = config.get('studybug', 'URL')
room = config.get('studybug', 'ROOM')
startTime = config.get('studybug', 'START_TIME')
endTime = config.get('studybug', 'END_TIME')
selenium_timeout = config.get('studybug', 'SELENIUM_TIMEOUT')
email = config.get('studybug', 'EMAIL')
password = config.get('studybug', 'PASSWORD')
return (url, room, startTime, endTime, selenium_timeout, email, password)
## Sends log data to Loggly via their API
def sendToLoggly():
log_data = "PLAINTEXT=" + urllib2.quote(simplejson.dumps(
{
'hoover':'beaver',
'ice':{
'ice':'baby'
}
}))
urllib2.urlopen("https://logs-01.loggly.com/inputs/65f419af-5bdb-489d-97b2-dd4883dad10a/tag/python/", log_data)
## Individually logs into each user account and confirms their reservation
def confirm(url, room, rows):
logger.info(" confirming...")
confirmationlist = []
for row in rows:
username = row[0]
password = row[1]
driver = webdriver.PhantomJS()
# This is a PhantomJS bug remedied by the following method call... should
# look into a fix for this.
driver.set_window_size(2000, 2000)
driver.get("https://zsr.wfu.edu/studyrooms/login")
logger.info(" checking user " + username + "...")
username_box = driver.find_element_by_name("username")
username_box.send_keys(username)
password_box = driver.find_element_by_name("password")
password_box.send_keys(password)
driver.find_element_by_name("submit").click()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html)
#confirmationlist = [reservation for reservation in soup.find(id=room).select('dd') if "current" in reservation.text]
for reservation in soup.find(id=room).select('dd'):
class_name = ' '.join(reservation.get('class'))
# Checks to see if WE in fact reserved that room
if "current_user_reservations" in class_name:
confirmationlist.append(reservation.get_text())
logger.info(" " + username + " booked " + reservation.get_text())
# Nasty-ass xpath... no idea why the normal minified one won't work
logout_button = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/ul/li[3]/a")
logout_button.click()
driver.quit()
return confirmationlist
if __name__ == "__main__":
# Declare our root logger
logger = configLogger("root")
main()