Star-Mapper/app.py

86 lines
2.3 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:
if "web.archive.org" in nn:
continue
2021-12-28 23:10:10 +00:00
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": nn, "label": label, "group": 0})
2021-12-28 23:10:10 +00:00
drawn.append(nn)
for e0, e1 in e:
if "web.archive.org" in e1:
continue
if e1 not in drawn and e1 not in n:
nodes.append({"id": e1, "label": e1, "group": 1})
drawn.append(e1)
2019-04-22 15:04:52 +00:00
edges.append({"from": e0, "to": e1})
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)
return transformForDrawing(jsonContent["nodes"], jsonContent["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.split("/")[2]
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
2021-12-28 23:10:10 +00:00
print(url)
return render_template('graph.html', nodes = json.dumps(nodes), edges = json.dumps(edges))
2019-04-22 15:04:52 +00:00
if __name__ == '__main__':
port = int(os.environ.get('PORT', 80))
app.run(host='0.0.0.0', port=port)