-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
220 lines (179 loc) · 6.84 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import subprocess
import sys
from datetime import datetime
import click
from alembic import command
from alembic.config import Config
from flask import Flask, jsonify, request
from flask.cli import with_appcontext
from enpoints import (endpoint_author_similarity_score,
endpoint_connected_comments, endpoint_connected_posts,
endpoint_dataframe, endpoint_get_articles,
endpoint_get_authors, endpoint_get_content,
endpoint_similarity_score, endpoint_specter_clustering)
from utils import create_approach, list_approaches, send_feedback_email
app = Flask(__name__)
@app.cli.command("db_migrate")
@click.option("--message", default=None, help="Revision message")
@with_appcontext
def db_migrate(message):
alembic_cfg = Config("alembic.ini")
command.revision(alembic_cfg, message=message, autogenerate=True)
@app.cli.command("db_upgrade")
@with_appcontext
def db_upgrade():
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
def is_array_empty(array):
return array is None or len(array) == 0
@app.route('/api/authors', methods=['GET'])
def get_authors():
return jsonify({
'data': endpoint_get_authors()
})
@app.route('/api/articles', methods=['GET'])
def get_articles():
return jsonify({
'data': endpoint_get_articles()
})
@app.route('/api/content', methods=['GET'])
def get_content():
return jsonify({
'data': endpoint_get_content()
})
@app.route('/api/dataframe', methods=['GET'])
def get_dataframe():
params = request.args.to_dict(flat=False)
columns = []
ascendings = []
for key, value in params.items():
if key.startswith('columns[') and key.endswith(']'):
name = key[8:-1]
columns.append(name)
ascendings.append(value[0] == 'asc')
try:
data = endpoint_dataframe(columns, ascendings)
return jsonify(data)
except:
return []
@app.route('/api/similarity-score', methods=['GET'])
def get_similarity_score():
params = request.args.to_dict(flat=False)
article_list = params.get('article_list[]')
compared_authors = params.get('compared_authors[]')
author_pair1 = params.get('author_pair1[]')
author_pair2 = params.get('author_pair2[]')
articles_result = []
if(is_array_empty(article_list) == False):
articles_result = endpoint_similarity_score(article_list, compared_authors)
return jsonify({
'authors': endpoint_author_similarity_score(
author_pair1,
author_pair2
).tolist(),
'articles': articles_result
})
@app.route('/api/specter-clustering', methods=['GET'])
def get_specter_clustering():
n = int(request.args.get('cluster_count'))
cluster_choice = int(request.args.get('cluster'))
select_by_content = request.args.get('content')
return jsonify(endpoint_specter_clustering(n, cluster_choice, select_by_content))
@app.route('/api/connected-posts', methods=['GET'])
def get_connected_posts():
depth = int(request.args.get('depth'))
a_name = request.args.get('a_name')
result = endpoint_connected_posts(a_name, depth)
return jsonify(result)
@app.route('/api/connected-comments', methods=['GET'])
def get_connected_comments():
depth = int(request.args.get('depth'))
a_name = request.args.get('a_name')
result = endpoint_connected_comments(a_name, depth)
return jsonify(result)
@app.route('/api/approaches', methods=['GET'])
def get_approaches():
limit = request.args.get('limit', default=10, type=int)
last_spotlight_count = request.args.get('lastSpotlightCount', type=int)
last_created_at = request.args.get('lastCreatedAt')
last_id = request.args.get('lastId', type=int)
filter_type = request.args.get('filter')
order_by = request.args.get('orderBy', default='spotlights')
if filter_type not in [None, 'post', 'comment']:
return jsonify({"error": "Invalid filter type. Use 'post', 'comment', or omit for both."}), 400
if order_by not in ['spotlights', 'comments', 'recency']:
return jsonify({"error": "Invalid order_by. Use 'spotlights', 'comments', or 'recency'."}), 400
if last_created_at:
last_created_at = datetime.fromisoformat(last_created_at)
approaches, next_spotlight_count, next_created_at, next_id = list_approaches(
limit, last_spotlight_count, last_created_at, last_id, filter_type, order_by
)
approaches_list = [
{
"id": approach[0],
"main_article": approach[1],
"node_id": approach[2],
"link": approach[3],
"type": approach[4],
"label": approach[5],
"created_at": approach[6].isoformat(),
"spotlight_count": approach[7],
"spotlights": [
{
"email": spotlight[0],
"comment": spotlight[1],
"created_at": spotlight[2].isoformat()
}
for spotlight in approach[9]
]
}
for approach in approaches
]
next_params = {}
if next_spotlight_count is not None:
next_params = {
'lastSpotlightCount': next_spotlight_count,
'lastCreatedAt': next_created_at.isoformat() if next_created_at else None,
'lastId': next_id
}
return jsonify({
"approaches": approaches_list,
"nextParams": next_params
})
@app.route('/api/approaches', methods=['POST'])
def create_new_approach():
data = request.json
main_article = data.get('mainArticle')
node_id = data.get('nodeId')
link = data.get('link')
type = data.get('type')
label = data.get('label')
comment = data.get('comment')
email = data.get('email')
if not node_id or not link:
return jsonify({"error": "Node ID and link are required"}), 400
approach_id, new_count, is_spotlight = create_approach(main_article, node_id, link, type, label, email, comment)
return jsonify({
"success": True,
"message": "Approach spotlighted successfully" if is_spotlight else "Approach shared successfully",
"id": approach_id,
"spotlight_count": new_count
}), 201
@app.route('/api/send-feedback', methods=['POST'])
def send_feedback():
data = request.json
name = data.get('name')
email = data.get('email')
feedback = data.get('feedback')
if not all([name, email, feedback]):
return jsonify({"error": "Name, email, and feedback are required"}), 400
success = send_feedback_email(name, email, feedback)
if success:
return jsonify({"message": "Feedback sent successfully"}), 200
else:
return jsonify({"error": "Failed to send feedback"}), 500
# with app.app_context():
# start_population_script()
if __name__ == '__main__':
app.run(port=5000, debug=True)