-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
116 lines (101 loc) · 3.61 KB
/
app.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
import os
import requests
from bs4 import BeautifulSoup
from flask import Flask, render_template, send_from_directory, request, redirect, url_for, jsonify, make_response
import json
import urlparse
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data', methods=['POST'])
def data():
if (not request.form['username'] or not request.form['password']):
return redirect(url_for('index'))
return render_template('index.html')
@app.route('/<path:path>')
def static_proxy(path):
return app.send_static_file(path)
@app.route('/', methods=['POST'])
def handle_data():
if (not request.form['username'] or not request.form['password']):
return redirect(url_for('index'))
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.set_window_size(1300, 700)
driver.get('https://laulima.hawaii.edu/portal/relogin')
username = driver.find_element_by_id("eid")
password = driver.find_element_by_id("pw")
username.send_keys(request.form['username'])
password.send_keys(request.form['password'])
driver.find_element_by_name("submit").click()
elements = driver.find_elements_by_class_name('alertMessage')
# Object to return
json = {
'data': {
'nav': [],
'body': {
'sections': []
}
},
'status_code': '',
'text': ''
}
# Getting information
drop = driver.find_elements_by_class_name('drop')
for span in drop:
span.click()
# Hack to fix html parsing too fast
time.sleep(1)
soup = BeautifulSoup(driver.page_source, 'lxml')
# Content
titles = []
bodies = []
i = 0
for div in soup.find_all('div', 'portletTitleWrap'):
for title in div.find_all('div', 'title'):
titles.append(title.getText())
for div in soup.find_all('div', 'portletMainWrap'):
i = i + 1
iframe = div.find('iframe')
if (iframe):
if(not bool(urlparse.urlparse(iframe.attrs['src']).netloc)):
iframe.attrs['src'] = 'https://laulima.hawaii.edu' + iframe.attrs['src']
obj = {'src': iframe.attrs['src']}
bodies.append(obj)
else:
titles.pop(i)
for idx, val in enumerate(titles):
obj = {}
obj[val] = bodies[idx]
json['data']['body']['sections'].append(obj)
# Nav Bar
for li in soup.find_all('li', 'nav-menu'):
obj = {'text': li.find(text=True, recursive=True), 'a': []}
for ul in li.find_all('ul'):
for a in ul.find_all('a'):
obj['a'].append({'href': a.attrs['href'], 'text': a.getText()})
json['data']['nav'].append(obj)
if (str(elements[0].text) == 'Invalid login'):
driver.quit()
json['status_code'] = 401
json['text'] = 'Unsuccessful Authentication.'
resp = make_response(jsonify(json))
return resp
else:
driver.quit()
json['status_code'] = 200
json['text'] = 'Successful Authentication!'
resp = make_response(jsonify(json))
resp.set_cookie('username', request.form['username'])
resp.set_cookie('password', request.form['password'])
return resp
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='localhost', port=port)