-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (42 loc) · 1.41 KB
/
app.py
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
import time
from urllib.parse import urlparse
from flask import Flask, g, request, jsonify
from dataaccess.storage import Storage
app = Flask(__name__)
app.config.from_object('config.dev.DevelopmentConfig')
def get_db():
if not hasattr(g, 'storage'):
g.storage = Storage(app.config['DB_HOST'])
return g.storage
@app.before_request
def before_request():
g.storage = get_db()
def extract_domains(links: list) -> list:
domains = []
for link in links:
domain = urlparse(link).hostname
if domain is None:
# delete timestamp, if link contain only domain, he not parsed correct with urlparse
try:
link = link[:link.index(':')]
except ValueError:
pass
finally:
domains.append(link)
else:
domains.append(domain)
return domains
@app.route('/visited_links', methods=['POST'])
def save_links():
data = request.get_json()
links = data['links']
timestamp = int(time.time())
g.storage.save_links(links, timestamp)
return jsonify({"status": "ok"})
@app.route('/visited_domains', methods=['GET'])
def get_domains():
time_from = request.args.get('from')
time_to = request.args.get('to')
links = g.storage.get_links(int(time_from), int(time_to))
domains = list(set(extract_domains(links)))
return jsonify({"domains": domains, "status": "ok"})