Star-Mapper/app.py

88 lines
2.5 KiB
Python
Raw Normal View History

2019-05-06 12:04:01 +00:00
from flask import Flask, request, render_template
2019-04-22 15:04:52 +00:00
import os
from Star import Crawler
2019-04-22 15:04:52 +00:00
import json
2019-05-01 12:26:11 +00:00
import sys
2019-04-22 15:04:52 +00:00
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
2021-12-28 23:10:10 +00:00
def transformForDrawing(n, e):
2019-10-04 14:53:28 +00:00
nodes = []
2019-05-01 12:26:11 +00:00
drawn = []
2020-09-26 16:53:05 +00:00
edges = []
2021-12-28 23:10:10 +00:00
for nn in n:
label = nn.rsplit('/')[-1]
2019-05-01 12:26:11 +00:00
if label == "":
2021-12-28 23:10:10 +00:00
label = nn.rsplit('/')[-2]
nodes.append('{' + "id: '{}', label: '{}', group: {}".format(nn, label, 0) + '}\n')
drawn.append(nn)
for ee in e:
if ee[1] not in drawn and ee[1] not in n:
nodes.append('{' + "id: '{}', label: '{}', group: {}".format(ee[1], ee[1], 1) + '}\n')
drawn.append(ee[1])
2019-04-22 15:04:52 +00:00
2021-12-28 23:10:10 +00:00
edges.append('{' + "from: '{}', to: '{}'".format(ee[0], ee[1]) + '}\n')
2019-04-22 15:04:52 +00:00
2021-12-28 23:10:10 +00:00
return nodes, edges
def graph(url):
obj = Crawler()
obj.run(url, 5000)
2019-10-04 14:53:28 +00:00
2021-12-28 23:10:10 +00:00
current = os.path.dirname(__file__)
n, e = obj.getNodesEdges()
2020-09-26 16:26:50 +00:00
with open(os.path.join(current, './cached/' + url.rsplit('/')[2] + '.json'), 'w', encoding='utf-8') as f:
2021-12-28 23:10:10 +00:00
f.write(json.dumps({"nodes": n,"edges": e}))
2019-04-29 09:03:24 +00:00
2021-12-28 23:10:10 +00:00
nodes, edges = transformForDrawing(n, e)
2019-05-06 12:04:01 +00:00
return nodes, edges
def load(url):
2020-09-26 16:26:50 +00:00
print("Loaded from cache: " + url)
current = os.path.dirname(__file__)
with open(os.path.join(current,'./cached/{}.json'.format(url)), 'r', encoding='utf-8') as f:
2019-05-06 21:56:23 +00:00
content = f.read()
2019-10-04 14:53:28 +00:00
jsonContent = json.loads(content)
nodes = jsonContent["nodes"]
edges = jsonContent["edges"]
2021-12-28 23:10:10 +00:00
nodes, edges = transformForDrawing(nodes, edges)
2019-05-06 21:56:23 +00:00
return nodes, edges
2019-05-06 12:04:01 +00:00
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
2020-09-26 16:26:50 +00:00
# input for urls over url
2019-05-06 12:04:01 +00:00
2019-06-10 10:49:42 +00:00
@app.route('/')
2019-05-06 12:04:01 +00:00
def index():
2021-12-28 23:10:10 +00:00
url = request.args.get("url")
2020-09-26 16:26:50 +00:00
cached = os.listdir(os.path.join(os.path.dirname(__file__), "./cached"))
withoutProtocol = url
2019-10-04 14:53:28 +00:00
if withoutProtocol + '.json' not in cached:
nodes, edges = graph(url)
2019-05-06 12:04:01 +00:00
else:
2019-05-06 21:56:23 +00:00
nodes, edges = load(withoutProtocol)
2019-10-04 14:53:28 +00:00
str1 = ","
nodes = str1.join(nodes)
edges = str1.join(edges)
2021-12-28 23:10:10 +00:00
return render_template('graph.html', nodes = nodes, edges = edges)
2019-04-22 15:04:52 +00:00
if __name__ == '__main__':
port = int(os.environ.get('PORT', 80))
2019-06-10 10:49:42 +00:00
sys.setrecursionlimit(5000)
2019-04-22 15:04:52 +00:00
app.run(host='0.0.0.0', port=port)