Last active
November 6, 2019 13:48
-
-
Save dlee35/e9c7cfb68d61bf8dd056abbfbb33b44a to your computer and use it in GitHub Desktop.
Flask App for querying GIAC cert info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, make_response | |
from flask_restful import Api, Resource, reqparse | |
from io import StringIO | |
import requests, re, csv | |
import lxml.html as lh | |
app = Flask(__name__) | |
api = Api(app) | |
proxyDict = {} | |
sslVerify = True | |
flaskDebug = True | |
users = [ | |
{ | |
"name": "", | |
"id": "", | |
"certifications": [] | |
} | |
] | |
class User(Resource): | |
def get(self, name): | |
for user in users: | |
if(name.lower() == user['name'].lower()): | |
return user, 200 | |
return "User not found", 404 | |
def put(self, name): | |
parser = reqparse.RequestParser() | |
#parser.add_argument("age") | |
#parser.add_argument("occupation") | |
#args = parser.parse_args() | |
first = name.split(' ')[0].capitalize() | |
last = name.split(' ')[1].capitalize() | |
name = first + ' ' + last | |
for user in users: | |
if(name.lower() == user["name"].lower()): | |
return "User with name {} already exists".format(name), 400 | |
user = { | |
"name": name, | |
"id": "", | |
"certifications": [] | |
} | |
global users | |
users.append(user) | |
return user, 201 | |
def delete(self, name): | |
global users | |
users = [user for user in users if user["name"] != name] | |
return "{} is deleted.".format(name), 200 | |
class Update(Resource): | |
def get(self, name): | |
if(name.lower() != "all"): | |
for index, user in enumerate(users): | |
if(name.lower() == user['name'].lower()): | |
strictname = '/' + '-'.join(name.lower().split()) + '/' | |
linklist = [] | |
headers = requests.utils.default_headers() | |
headers.update( | |
{ | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36', | |
} | |
) | |
r=requests.post("https://www.giac.org/certified-professionals/directory/search", data={'name': name.lower(), 'submit': 'Search'}, headers=headers, proxies=proxyDict, verify=sslVerify) | |
doc = lh.fromstring(r.text) | |
for hrefs in doc.xpath('//a/@href'): | |
if strictname in hrefs and hrefs not in linklist: | |
linklist.append(hrefs) | |
if len(linklist) == 0: | |
global users | |
first = name.split(' ')[0].capitalize() | |
last = name.split(' ')[1].capitalize() | |
name = first + ' ' + last | |
users = [user for user in users if user["name"] != name] | |
return "User not found on GIAC... Deleted {} from user list".format(name), 404 | |
r=requests.get("https://www.giac.org" + linklist[0], headers=headers, proxies=proxyDict, verify=sslVerify) | |
newlist = [] | |
doc = lh.fromstring(r.text) | |
for i in doc.xpath('//h2|//h4|//ul/li[span[@class="label"]]'): | |
if not i.text_content().startswith('Active') and not i.text_content().startswith('Certified'): | |
newlist.append(i.text_content()) | |
users[index]['id'] = re.sub('\(\ |\ \)','', newlist[1]) | |
users[index]['certifications'] = [] | |
for i in range(2,len(newlist),4): | |
users[index]['certifications'].append({ 'certification': newlist[i].strip(), 'obtained': newlist[i+1].split(' ')[1], 'expires': newlist[i+2].split(' ')[1], 'analyst_no': newlist[i+3].split(' ')[1]}) | |
return "Updated!", 200 | |
else: | |
for index, user in enumerate(users): | |
if user['name'] != "": | |
name = user['name'] | |
strictname = '/' + '-'.join(name.lower().split()) + '/' | |
linklist = [] | |
headers = requests.utils.default_headers() | |
headers.update( | |
{ | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36', | |
} | |
) | |
r=requests.post("https://www.giac.org/certified-professionals/directory/search", data={'name': name.lower(), 'submit': 'Search'}, headers=headers, proxies=proxyDict, verify=sslVerify) | |
doc = lh.fromstring(r.text) | |
for hrefs in doc.xpath('//a/@href'): | |
if strictname in hrefs and hrefs not in linklist: | |
linklist.append(hrefs) | |
if len(linklist) == 0: | |
global users | |
first = name.split(' ')[0].capitalize() | |
last = name.split(' ')[1].capitalize() | |
name = first + ' ' + last | |
users = [user for user in users if user["name"] != name] | |
return "User not found on GIAC... Deleted {} from user list".format(name), 404 | |
r=requests.get("https://www.giac.org" + linklist[0], headers=headers, proxies=proxyDict, verify=sslVerify) | |
newlist = [] | |
doc = lh.fromstring(r.text) | |
for i in doc.xpath('//h2|//h4|//ul/li[span[@class="label"]]'): | |
if not i.text_content().startswith('Active') and not i.text_content().startswith('Certified'): | |
newlist.append(i.text_content()) | |
users[index]['id'] = re.sub('\(\ |\ \)','', newlist[1]) | |
users[index]['certifications'] = [] | |
for i in range(2,len(newlist),4): | |
users[index]['certifications'].append({ 'certification': newlist[i].strip(), 'obtained': newlist[i+1].split(' ')[1], 'expires': newlist[i+2].split(' ')[1], 'analyst_no': newlist[i+3].split(' ')[1]}) | |
@app.route('/download') | |
def download(): | |
si = StringIO() | |
cw = csv.writer(si) | |
cw.writerow(["Name", "ID", "Certification", "Obtained", "Expires", "Analyst No"]) | |
for user in users: | |
for index, cert in enumerate(user['certifications']): | |
cw.writerow( [ | |
user['name'], | |
user['id'], | |
user['certifications'][index]['certification'], | |
user['certifications'][index]['obtained'], | |
user['certifications'][index]['expires'], | |
user['certifications'][index]['analyst_no'] | |
] | |
) | |
output = make_response(si.getvalue()) | |
output.headers["Content-Disposition"] = "attachment; filename=export.csv" | |
output.headers["Content-type"] = "text/csv" | |
return output | |
api.add_resource(User, "/user/<string:name>") | |
api.add_resource(Update, "/update/<string:name>") | |
app.run(debug=flaskDebug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment