File size: 9,039 Bytes
9df4cc0
3e4bf85
3778f9f
33a4549
 
 
9df4cc0
 
 
33a4549
9df4cc0
a9b1809
9df4cc0
 
 
 
 
 
 
33a4549
3778f9f
 
33a4549
 
 
 
 
 
 
 
f2e1cf5
68427a0
33a4549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3778f9f
33a4549
 
3778f9f
33a4549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3778f9f
 
 
 
 
 
33a4549
3778f9f
 
 
 
 
 
 
 
 
 
 
 
33a4549
 
 
 
9df4cc0
33a4549
9df4cc0
 
 
3778f9f
9df4cc0
 
 
 
 
3778f9f
 
9df4cc0
 
 
 
 
 
 
3778f9f
3e4bf85
 
9df4cc0
 
 
 
 
 
 
 
 
3778f9f
9df4cc0
 
3778f9f
9df4cc0
 
 
 
3778f9f
 
3e4bf85
9df4cc0
 
 
3e4bf85
 
9df4cc0
 
 
3778f9f
 
 
 
9df4cc0
 
 
3778f9f
 
 
 
9df4cc0
 
a9b1809
3778f9f
 
3e4bf85
9df4cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e4bf85
 
3778f9f
 
3e4bf85
 
9df4cc0
3778f9f
9df4cc0
 
3778f9f
3e4bf85
9df4cc0
3e4bf85
 
 
 
9df4cc0
3e4bf85
9df4cc0
 
3e4bf85
 
 
9df4cc0
3e4bf85
33a4549
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import pandas as pd
from datetime import datetime
from dotenv import load_dotenv
import traceback

from md_html import convert_single_md_to_html as convert_md_to_html
from news_analysis import fetch_deep_news, generate_value_investor_report
from csv_utils import detect_changes
from fin_interpreter import analyze_article

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATA_DIR = os.path.join(BASE_DIR, "data")
HTML_DIR = os.path.join(BASE_DIR, "html")
CSV_PATH = os.path.join(BASE_DIR, "investing_topics.csv")

os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(HTML_DIR, exist_ok=True)

load_dotenv()


def build_metrics_box(topic, num_articles):
    now = datetime.now().strftime("%Y-%m-%d %H:%M")
    return f"""
> Topic: `{topic}`
> Articles Collected: `{num_articles}`
> Generated: `{now}`
>
"""


def run_value_investing_analysis(csv_path, progress_callback=None):
    """
    Runs the analysis for all topics in the CSV.
    Returns:
        md_files (list of md file paths)
        all_articles (list of article dicts)
    """
    current_df = pd.read_csv(csv_path)
    prev_path = os.path.join(BASE_DIR, "investing_topics_prev.csv")

    if os.path.exists(prev_path):
        previous_df = pd.read_csv(prev_path)
        changed_df = detect_changes(current_df, previous_df)
        if changed_df.empty:
            if progress_callback:
                progress_callback("βœ… No changes detected. Skipping processing.")
            return [], []
    else:
        changed_df = current_df

    new_md_files = []
    all_articles = []

    for _, row in changed_df.iterrows():
        topic = row.get("topic")
        timespan = row.get("timespan_days", 7)
        msg = f"πŸ” Processing: {topic} ({timespan} days)"
        print(msg)
        if progress_callback:
            progress_callback(msg)

        news = fetch_deep_news(topic, timespan)
        if not news:
            warning = f"⚠️ No news found for: {topic}"
            print(warning)
            if progress_callback:
                progress_callback(warning)
            continue

        # Add articles to all_articles
        for article in news:
            try:
                res = analyze_article(article.get("summary", ""))
                if isinstance(res, dict):
                    sentiment = res.get("sentiment")
                    confidence = res.get("confidence")
                    signal = res.get("signal")
                else:
                    sentiment, confidence, signal = res[0], res[1], res[2]
            except Exception as e:
                sentiment, confidence, signal = "Unknown", 0.0, "None"
                print(f"Error analyzing article: {e}")

            all_articles.append({
                "Title": article.get("title"),
                "URL": article.get("url"),
                "Summary": article.get("summary"),
                "Priority": article.get("priority", "Low"),
                "Date": article.get("date"),
                "Company": article.get("company", topic),
                "Sentiment": sentiment,
                "Confidence": confidence,
                "Signal": signal
            })

        # Generate report
        report_body = generate_value_investor_report(topic, news)
        metrics_md = build_metrics_box(topic, len(news))
        full_md = metrics_md + report_body

        filename = f"{topic.replace(' ', '_').lower()}_{datetime.now().strftime('%Y-%m-%d')}.md"
        filepath = os.path.join(DATA_DIR, filename)
        counter = 1
        while os.path.exists(filepath):
            filename = f"{topic.replace(' ', '_').lower()}_{datetime.now().strftime('%Y-%m-%d')}_{counter}.md"
            filepath = os.path.join(DATA_DIR, filename)
            counter += 1

        with open(filepath, "w", encoding="utf-8") as f:
            f.write(full_md)

        new_md_files.append(filepath)

    if progress_callback:
        progress_callback(f"βœ… Markdown saved to: {DATA_DIR}")
    current_df.to_csv(prev_path, index=False)
    return new_md_files, all_articles


def run_pipeline(csv_path, tavily_api_key, progress_callback=None):
    os.environ["TAVILY_API_KEY"] = tavily_api_key

    new_md_files, all_articles = run_value_investing_analysis(csv_path, progress_callback)
    new_html_paths = []
    for md_path in new_md_files:
        convert_md_to_html(md_path, HTML_DIR)
        html_path = os.path.join(HTML_DIR, os.path.basename(md_path).replace(".md", ".html"))
        new_html_paths.append(html_path)

    articles_df = pd.DataFrame(all_articles)
    insights_df = build_company_insights(articles_df)
    return new_html_paths, articles_df, insights_df


def build_company_insights(articles_df):
    if articles_df.empty:
        return pd.DataFrame()
    grouped = (
        articles_df.groupby("Company")
        .agg({
            "Title": "count",
            "Sentiment": lambda x: x.mode()[0] if not x.mode().empty else "Neutral",
            "Signal": lambda x: x.mode()[0] if not x.mode().empty else "Watch"
        })
        .reset_index()
        .rename(columns={"Title": "Mentions"})
    )
    return grouped


if __name__ == "__main__":
    md_files, _ = run_value_investing_analysis(CSV_PATH)
    for md in md_files:
        convert_md_to_html(md, HTML_DIR)
    print(f"🌐 All reports converted to HTML at: {HTML_DIR}")

#import os
# import sys
# from datetime import datetime
# from dotenv import load_dotenv
# import pandas as pd

# from md_html import convert_single_md_to_html as convert_md_to_html
# from news_analysis import fetch_deep_news, generate_value_investor_report
# from csv_utils import detect_changes

# # === Setup Paths ===
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# DATA_DIR = os.path.join(BASE_DIR, "data")
# HTML_DIR = os.path.join(BASE_DIR, "html")
# CSV_PATH = os.path.join(BASE_DIR, "investing_topics.csv")

# os.makedirs(DATA_DIR, exist_ok=True)
# os.makedirs(HTML_DIR, exist_ok=True)

# # === Load .env ===
# load_dotenv()

# def build_metrics_box(topic, num_articles):
#     now = datetime.now().strftime("%Y-%m-%d %H:%M")
#     return f"""
# > Topic: `{topic}`
# > Articles Collected: `{num_articles}`
# > Generated: `{now}`
# >
# """

# def run_value_investing_analysis(csv_path, progress_callback=None):
#     current_df = pd.read_csv(csv_path)
#     prev_path = os.path.join(BASE_DIR, "investing_topics_prev.csv")
    
#     if os.path.exists(prev_path):
#         previous_df = pd.read_csv(prev_path)
#         changed_df = detect_changes(current_df, previous_df)
#         if changed_df.empty:
#             if progress_callback:
#                 progress_callback("βœ… No changes detected. Skipping processing.")
#             return []
#     else:
#         changed_df = current_df

#     new_md_files = []

#     for _, row in changed_df.iterrows():
#         topic = row.get("topic")
#         timespan = row.get("timespan_days", 7)
#         msg = f"πŸ” Processing: {topic} ({timespan} days)"
#         print(msg)
#         if progress_callback:
#             progress_callback(msg)

#         news = fetch_deep_news(topic, timespan)
#         if not news:
#             warning = f"⚠️ No news found for: {topic}"
#             print(warning)
#             if progress_callback:
#                 progress_callback(warning)
#             continue

#         report_body = generate_value_investor_report(topic, news)
#         image_url = "https://via.placeholder.com/1281x721?text=No+Image+Available"
#         image_credit = "Image placeholder"

#         metrics_md = build_metrics_box(topic, len(news))
#         full_md = metrics_md + report_body

#         base_filename = f"{topic.replace(' ', '_').lower()}_{datetime.now().strftime('%Y-%m-%d')}"
#         filename = base_filename + ".md"
#         filepath = os.path.join(DATA_DIR, filename)

#         counter = 1
#         while os.path.exists(filepath):
#             filename = f"{base_filename}_{counter}.md"
#             filepath = os.path.join(DATA_DIR, filename)
#             counter += 1

#         with open(filepath, "w", encoding="utf-8") as f:
#             f.write(full_md)

#         new_md_files.append(filepath)

#     if progress_callback:
#         progress_callback(f"βœ… Markdown saved to: {DATA_DIR}")
#     current_df.to_csv(prev_path, index=False)
#     return new_md_files

# def run_pipeline(csv_path, tavily_api_key, progress_callback=None):
#     os.environ["TAVILY_API_KEY"] = tavily_api_key

#     new_md_files = run_value_investing_analysis(csv_path, progress_callback)
#     new_html_paths = []

#     for md_path in new_md_files:
#         convert_md_to_html(md_path, HTML_DIR)
#         html_path = os.path.join(HTML_DIR, os.path.basename(md_path).replace(".md", ".html"))
#         new_html_paths.append(html_path)

#     return new_html_paths

# if __name__ == "__main__":
#     md_files = run_value_investing_analysis(CSV_PATH)
#     for md in md_files:
#         convert_md_to_html(md, HTML_DIR)
#     print(f"🌐 All reports converted to HTML at: {HTML_DIR}")