File size: 3,782 Bytes
3eed4de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, Response, url_for
from main import analyze_news, extract_trending_words
import io
import time
import matplotlib.pyplot as plt
from wordcloud import WordCloud


app = Flask(__name__)
cache = {}
plt.switch_backend("Agg")

def get_cached_data(cache_key):
    cached = cache.get(cache_key)
    if cached and time.time() - cached["timestamp"] < 600:
        print(f"Cache hit for {cache_key}")
        return cached["results"], cached["trending_words"]
    return None, None

@app.route("/", methods=["GET", "POST"])
def index():
    results, trending_words = [], []
    sentiment_chart = None
    query = ""

    if request.method == "POST":
        query = request.form["query"]
        num_articles = int(request.form["num_articles"])
        api_key = "f80d8a6206cd472baeb21f04786b2626"

        cache_key = f"{query}_{num_articles}"
        results, trending_words = get_cached_data(cache_key)

        if results is None:
            print(f"No cache for {cache_key}, fetching new data...")
            results = analyze_news(query, api_key, num_articles)
            texts = [article["title"] + " " + article.get("summary", "") for article in results]
            trending_words = extract_trending_words(texts)

            cache[cache_key] = {
                "results": results,
                "trending_words": trending_words,
                "timestamp": time.time()
            }

        if results:
            sentiment_chart = url_for('sentiment_chart_route')

    return render_template("index.html", results=results, sentiment_chart=sentiment_chart, query=query, trending_words=trending_words)

@app.route("/sentiment_chart")
def sentiment_chart_route():
    if not cache:
        return "No sentiment data", 404

    last_query = list(cache.keys())[-1]
    cached = cache.get(last_query)

    if not cached:
        return "No sentiment data", 404

    results = cached["results"]
    sentiments = [article["sentiment"] for article in results]

    sentiment_counts = dict((x, sentiments.count(x)) for x in set(sentiments))

    labels = list(sentiment_counts.keys())
    values = list(sentiment_counts.values())

    color_map = {
        "POSITIVE": "#28a745",
        "NEGATIVE": "#c82333"
    }

    colors = [color_map[label] for label in labels]
    total = sum(values)

    plt.figure(figsize=(3, 3))
    plt.pie(values, autopct=lambda pct: f'{int(pct * total / 100)} ({pct:.1f}%)', startangle=140, colors=colors)
    plt.axis('equal')

    img = io.BytesIO()
    plt.savefig(img, format="png", bbox_inches="tight", transparent = True)
    plt.close()
    img.seek(0)

    return Response(img.getvalue(), mimetype="image/png")

@app.route("/wordcloud_chart")
def wordcloud_chart():
    if not cache:
        return "No word data", 404

    last_query = list(cache.keys())[-1]
    cached = cache.get(last_query)

    if not cached:
        return "No word data", 404

    results = cached["results"]
    texts = [article["title"] + " " + article.get("summary", "") for article in results]

    text = " ".join(texts)

    plt.figure(figsize=(16, 8), dpi=150)
    wordcloud = WordCloud(width=1600, height=800, colormap="Blues", background_color='#222').generate(text)
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis("off")

    img = io.BytesIO()
    plt.savefig(img, format="png", bbox_inches="tight", pad_inches=0, dpi=150)
    plt.close()
    img.seek(0)

    return Response(img.getvalue(), mimetype="image/png")

@app.route("/about")
def about():
    return render_template("about.html")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)