-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
113 lines (77 loc) · 2.69 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
import json
import requests
from flask import Flask, jsonify, render_template, request
from cache_handler import cache_query, clear_caches, top_news, trending_news
from newsflash import analyze_article
# Initialize server
app = Flask(__name__)
def get_news(topic):
parsed_articles, sentiments = cache_query("query_cache.json", topic)
return jsonify(topic=topic, parsed_articles=parsed_articles, sentiments=sentiments)
@app.route("/", methods=["GET", "POST"])
def index() -> str:
top_articles = topRes().json["top_articles"]
return render_template("index.html", top_articles=top_articles)
@app.route("/mobile")
def mobile() -> str:
return render_template("mobile.html")
@app.route("/summaries", methods=["GET", "POST"])
def summaries() -> str:
# Store user topic from homepage post request
topic = request.form["topic"]
topic = topic.title()
topic = topic.replace(" ", "+")
res_json = get_news(topic).json
topic = res_json["topic"]
parsed_articles = res_json["parsed_articles"]
sentiments = res_json["sentiments"]
return render_template(
"summaries.html",
topic=topic,
parsed_articles=parsed_articles,
sentiments=sentiments,
)
@app.route("/trending", methods=["GET", "POST"])
def trending() -> str:
trending_topics = trendingApi().json["trending_list"]
return render_template("trending.html", trending_topics=trending_topics)
@app.route("/api")
def apiRes() -> str:
# Store user topic from request
topic = list(request.args)[0]
return get_news(topic)
@app.route("/topApi")
def topRes() -> str:
return jsonify(top_articles=top_news("top_cache.json"))
@app.route("/analysisApi")
def articleAnalysis() -> str:
article_url = list(request.args)[0]
title, image, keywords, summary, sentiment_list = analyze_article(
article_url, nlp=True
)
return jsonify(
title=title,
image=image,
keywords=keywords,
summary=summary,
sentiment=sentiment_list,
)
@app.route("/trendingApi")
def trendingApi() -> str:
return jsonify(trending_list=trending_news("query_cache.json"))
@app.route("/fullArticle")
def fullApi() -> str:
article_url = list(request.args)[0]
title, image, full_text = analyze_article(article_url, nlp=False)
return jsonify(title=title, image=image, full_text=full_text)
@app.route("/reset")
def clear() -> str:
clear_caches(["query_cache.json", "top_cache.json"])
@app.route("/privacy")
def privacy() -> str:
return render_template("privacy.html")
@app.route("/support")
def contact() -> str:
return render_template("support.html")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)