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
import sitemapper
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__)
2019-10-04 14:53:28 +00:00
def graph(url):
2019-04-22 15:04:52 +00:00
obj = sitemapper.url(url)
2019-04-27 12:23:56 +00:00
obj.run_check(url)
2019-05-01 12:26:11 +00:00
2020-09-26 16:26:50 +00:00
current = os.path.dirname(__file__)
2019-10-04 14:53:28 +00:00
nodes = []
2019-05-01 12:26:11 +00:00
drawn = []
for key, values in obj.sites.items():
label = key.rsplit('/')[-1]
if label == "":
label = key.rsplit('/')[-2]
2019-10-04 14:53:28 +00:00
nodes.append('{' + "id: '{}', label: '{}', group: {}".format(key, label, 0) + '}')
2019-05-01 12:26:11 +00:00
drawn.append(key)
2019-04-22 15:04:52 +00:00
2019-05-01 12:26:11 +00:00
for key, values in obj.sites.items():
for value in values:
if value not in drawn and value not in obj.sites:
2019-10-04 14:53:28 +00:00
nodes.append('{' + "id: '{}', label: '{}', group: {}".format(value, value, 1) + '}')
2019-05-01 12:26:11 +00:00
drawn.append(value)
2019-04-22 15:04:52 +00:00
2019-10-04 14:53:28 +00:00
edges = []
2019-04-29 09:03:24 +00:00
for key, values in obj.sites.items():
for value in values:
2019-10-04 14:53:28 +00:00
edges.append('{' + "from: '{}', to: '{}'".format(key, value) + '}')
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:
2019-10-04 14:53:28 +00:00
f.write(json.dumps({"nodes": nodes,"edges": edges}))
2019-04-29 09:03:24 +00:00
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"]
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():
2020-09-26 16:26:50 +00:00
url = request.args.get("url")
cached = os.listdir(os.path.join(os.path.dirname(__file__), "./cached"))
2019-05-06 21:56:23 +00:00
withoutProtocol = url.rsplit('/')[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
str1 = ","
nodes = str1.join(nodes)
edges = str1.join(edges)
2019-05-06 12:04:01 +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)