Star-Mapper/app.py

85 lines
2.2 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 urlchecker
import sitemapper
import _pickle as cPickle
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-05-06 12:04:01 +00:00
def map(url):
2019-04-27 12:23:56 +00:00
#print(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
nodes = ""
drawn = []
for key, values in obj.sites.items():
label = key.rsplit('/')[-1]
if label == "":
label = key.rsplit('/')[-2]
nodes += '{' + 'id: "{}", label: "{}", group: {}'.format(key, label, 0) + '},\n'
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:
nodes += '{' + 'id: "{}", label: "{}", group: {}'.format(value, value, 1) + '},\n'
drawn.append(value)
2019-04-22 15:04:52 +00:00
2019-05-01 12:26:11 +00:00
nodes = nodes[:-2] + "\n"
2019-04-29 09:03:24 +00:00
edges = ""
for key, values in obj.sites.items():
for value in values:
2019-05-01 12:26:11 +00:00
edges += '{' + 'from: "{}", to: "{}"'.format(key, value) + '},\n'
edges = edges[:-2] + "\n"
2019-04-29 09:03:24 +00:00
2019-05-06 21:56:23 +00:00
with open('./cached/' + url.rsplit('/')[2] + '.txt', 'w', encoding='utf-8') as f:
f.write(nodes + "end\n")
2019-05-01 12:26:11 +00:00
f.write(edges)
2019-04-29 09:03:24 +00:00
2019-05-06 12:04:01 +00:00
return nodes, edges
def load(url):
2019-05-06 21:56:23 +00:00
with open('./cached/{}.txt'.format(url), 'r', encoding='utf-8') as f:
content = f.read()
nodes, edges = content.split("end\n")
return nodes, edges
2019-05-06 12:04:01 +00:00
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
2019-06-10 10:49:42 +00:00
@app.route('/')
2019-05-06 12:04:01 +00:00
def index():
2019-06-10 10:49:42 +00:00
#url = request.args.get("url")
url = "https://www.google.com"
2019-05-06 12:04:01 +00:00
cached = os.listdir("./cached")
2019-05-06 21:56:23 +00:00
withoutProtocol = url.rsplit('/')[2]
if withoutProtocol + '.txt' not in cached:
2019-05-06 12:04:01 +00:00
nodes, edges = map(url)
else:
2019-05-06 21:56:23 +00:00
nodes, edges = load(withoutProtocol)
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)