-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
63 lines (54 loc) · 1.71 KB
/
api.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
63
import flask
from flask import jsonify, request
from mining import get_keyword
from main import get_recommendations
from flask_cors import CORS, cross_origin
app = flask.Flask(__name__)
CORS(app, support_credentials=True)
# app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
@cross_origin()
def home():
message = """
<h1>Data mining API </h1>
<p>Go to /url and replace <strong>url</strong> in u=<strong>url</strong> with url to get keyword</p>
"""
return message
@app.route('/url', methods=['GET'])
@cross_origin()
def url():
"""
Input data: url
Return data:
{
"description": "A cryptocurrency is a digital or virtual currency that uses cryptography and is difficult to counterfeit.",
"h1_tag": null,
"title": "What Is Cryptocurrency?",
"url": "https://www.investopedia.com/terms/c/cryptocurrency.asp"
}
"""
args = request.args
url = args.get('url')
if 'url' not in args:
return "Error: no url. Please input url"
else:
keyword = get_keyword(url)
return jsonify(keyword)
"""
{
"request_links" : ["link1", "link2", "link3"]
}
"""
@app.route('/recommendation', methods=['POST'])
@cross_origin()
def get_recommendation():
request_data = request.get_json()
recommendations = {}
print(request_data)
if request_data and 'value' in request_data and 'request_links' in request_data['value']:
if (type(request_data['value']['request_links']) == list) and (len(request_data['value']['request_links']) > 0):
links = request_data['value']['request_links']
recommendations = get_recommendations(links)
print(recommendations)
return jsonify(recommendations)
app.run()