-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter.py
More file actions
71 lines (61 loc) · 2.4 KB
/
router.py
File metadata and controls
71 lines (61 loc) · 2.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
from flask import Flask, request, render_template, abort, send_file
from requests_toolbelt import MultipartEncoder
import json, io, os
import parse_file, webex
import parse_file
from urllib.request import Request, urlopen
from random import randint
# from flask_pymongo import PyMongo
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
return render_template("form.html")
@app.route("/", methods=['POST'])
def form_submission():
url = request.form['gh_url']
g = request.form['granularity']
if g:
req_str = "{0} -g={1}".format(url, g)
else:
req_str = url
try:
im = parse_file.gh_link_entry(req_str)
except AssertionError as e:
return render_template("form.html", error=e)
filename = "static/images/graph.png"
im.save(filename, format="PNG")
passed_path = "{0}?{1}".format(filename, randint(1, 1000000))
return render_template("form.html", path=passed_path)
@app.route("/webex", methods=["POST"])
def webex_request():
webhook = json.loads(request.data)
if webhook['data']['personEmail'] != os.environ["BOT_EMAIL"]:
request_json = webex.sendGetRequest(os.environ["SPARK_MESSAGES_URL"] + webhook['data']['id'])
query_url = json.loads(request_json).get("text")
errmsg = {
"roomId": webhook['data']['roomId'],
"text": ""
}
if query_url is None:
errmsg["text"] = "{0} takes in a Python GitHub URL".format(os.environ["BOT_NAME"])
webex.sendErrorMsg(os.environ["SPARK_MESSAGES_URL"], errmsg)
abort(400, "No URL sent")
try:
im = parse_file.gh_link_entry(query_url)
except AssertionError as e:
errmsg["text"] = "Invalid input. Must be a valid Python GitHub URL and option (e.g. `-g=5`)."
webex.sendErrorMsg(os.environ["SPARK_MESSAGES_URL"], errmsg)
abort(400, "Invalid URL")
output = io.BytesIO()
im.save(output, format="PNG")
out_message = "Visualization of {0}".format(query_url[len(os.environ["BOT_NAME"]):])
fields = {
"roomId": webhook['data']['roomId'],
"text": out_message,
"files": ("visualize.png", output, "image/png")
}
data = MultipartEncoder(fields=fields)
webex.sendPostRequest(os.environ["SPARK_MESSAGES_URL"], data)
return "true"
if __name__ == "__main__":
app.run()