-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
133 lines (100 loc) · 4.1 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from summarize import Summarize
from predictor import predict1
import json
import ast
import datetime
import os
from multiprocessing.pool import ThreadPool
from extractTicker import stock_graph
from news_scraper import scraper
from app_helper import pre, valid_time
app = Flask(__name__)
CORS(app, supports_credentials=True)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY']
app.config.from_object(__name__)
db = SQLAlchemy(app)
class QueryModel(db.Model):
id = db.Column(db.Integer, primary_key = True)
query_ = db.Column(db.String(90), nullable = False, unique = True)
list_predicted = db.Column(db.String(200), nullable = False)
news_score = db.Column(db.Float)
time = db.Column(db.DateTime, default=datetime.datetime.now())
@app.route('/news', methods=['GET'])
def sentiment_analyzer():
"""Returns the change in value in interval of days."""
query = request.args.get('query', type=str)
if query is None:
return Response("{'Message': 'Send query in get query'}", status=404,
mimetype='application/json')
item = QueryModel.query.filter_by(query_=query).first()
if item is not None:
if valid_time(item.time):
return json.dumps({"predict": ast.literal_eval(item.list_predicted)})
else:
db.session.delete(item)
query = query.replace("%20", " ")
list_predicted = {}
try:
news_score = predict1(query).final_pred
except Exception as e:
return Response("{'Message': '"+str(e)+ "'}", status=404,
mimetype='application/json')
pool = ThreadPool(processes=4)
inter1 = pool.apply_async(pre, (1, news_score))
inter7 = pool.apply_async(pre, (7, news_score))
inter15 = pool.apply_async(pre, (15, news_score))
inter30 = pool.apply_async(pre, (30, news_score))
list_predicted["1"] = inter1.get()
list_predicted["7"] = inter7.get()
list_predicted["15"] = inter15.get()
list_predicted["30"] = inter30.get()
if item is None:
item = QueryModel(query_=query, list_predicted=str(list_predicted),
news_score=news_score, time=datetime.datetime.now())
db.session.add(item)
item.list_predicted = str(list_predicted)
item.news_score = news_score
db.session.commit()
return json.dumps({"predict": list_predicted})
@app.route('/stock-graph', methods=['GET'])
def graph():
query = request.args.get('query', type=str)
if query is None:
return Response("{'Message': 'Send query in get query'}", status=404,
mimetype='application/json')
query = query.replace("%20", " ")
try:
list_predicted = ast.literal_eval(QueryModel.query.filter_by(query_=query).first().list_predicted)
except Exception as e:
return Response("{'Message': 'Invalid query: "+str(e)+"'}", status=404,
mimetype='application/json')
graph_pre = [list_predicted["1"],
list_predicted["7"],
list_predicted["15"],
list_predicted["30"]]
try:
return stock_graph(query,
graph_pre).graph()
except Exception as e:
print(e)
return Response("{'message': '"+str(e)+"'}", status=404)
@app.route('/get-summary', methods=["GET"])
def get_summary():
query = request.args.get('query', type=str)
if query is None:
return Response("{'Message': 'Send query in get query'}", status=404,
mimetype='application/json')
query = query.replace("%20", " ")
try:
news = scraper(query).get_title()
except Exception as e:
return Response("{'Message': '"+str(e)+"'}", status=404,
mimetype='application/json')
pointers = 3
return jsonify(Summarize(news, pointers).generate_summary())
if __name__ == '__main__':
db.create_all() # pragma: no cover
app.run(debug=True, use_reloader=False) # pragma: no cover