File size: 5,213 Bytes
1949e5b
 
be80984
09d4ed0
1949e5b
be80984
 
 
 
 
 
 
 
 
a34745f
 
 
4e6f257
a34745f
 
 
 
 
 
 
09d4ed0
 
 
 
 
 
be80984
09d4ed0
 
 
be80984
6c0855b
 
be80984
09d4ed0
 
 
1949e5b
 
 
 
 
 
09d4ed0
a34745f
 
1949e5b
 
a34745f
d0ebfee
 
 
 
 
 
09d4ed0
 
a34745f
be80984
 
e63cf91
09d4ed0
41c7719
e63cf91
 
 
 
41c7719
be80984
 
e63cf91
a34745f
be80984
 
 
a34745f
 
 
 
 
 
 
1949e5b
a34745f
 
 
 
be80984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ebfee
09d4ed0
a34745f
be80984
 
 
 
a34745f
be80984
 
 
 
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
import streamlit as st
from gfnews import GoogleBussinessNews
from datetime import datetime
from transformers import pipeline


financial_business_news_domains = [
    "economictimes.indiatimes.com", "business-standard.com", "financialexpress.com", 
    "livemint.com", "thehindubusinessline.com", "moneycontrol.com", "bloombergquint.com", 
    "cnbctv18.com", "businesstoday.in", "forbesindia.com", "reuters.com", "bloomberg.com", 
    "ft.com", "wsj.com", "cnbc.com", "marketwatch.com", "investing.com", "finance.yahoo.com", 
    "seekingalpha.com", "businessinsider.com"
]

st.markdown(
    """
    <style>
        .stMainBlockContainer{
            max-width: 70vw;
            padding-top: 4rem;
        }
    </style>
    """,
    unsafe_allow_html=True
)
sentiment_analyzer = pipeline("sentiment-analysis")

if 'search_history' not in st.session_state:
    st.session_state.search_history = []

common_topics = [
    "HCLTech", "Stock Market", "Interest Rates", "RBI Policies", 
    "Nifty 50", "Banking Sector", "Mutual Funds", "Corporate Earnings", "Budget 2025"
]

nifty_50_companies = [    
    "Reliance", "TCS", "HDFC", "Infosys", "HUL", "ICICI", "SBI", 
    "Bajaj Finance", "Airtel", "Wipro", "Kotak Mahindra", "Adani Enterprises", 
    "Larsen & Toubro", "Tata Motors", "Maruti Suzuki", "Asian Paints",
    "NTPC", "Nestle India", "Sun Pharma", "Power Grid", "ITC", "HDFC", "Tata Steel"
]

st.title("πŸ“° Finance News App")

st.markdown(
    """
    **Welcome to the Finance News App!**

    Stay informed with the latest financial news from trusted sources. Use the search bar or select from common topics.
    """,
    unsafe_allow_html=True
)

col1, col2 = st.columns([2, 1])
scraper = None

with col2:
    selected_sources = st.multiselect("Select News Sources", default=financial_business_news_domains[:2], options=financial_business_news_domains)
    scraper = GoogleBussinessNews(selected_sources,max_articles=25)


with col1:
    st.markdown("## πŸ” Search for News")
    
    st.markdown("")
    topic_disabled = False

    query = st.text_input("Enter search query:")
    if query:
        topic_disabled = True

    st.markdown("")
    topic = st.selectbox("OR Search by topic:", common_topics + nifty_50_companies, disabled=topic_disabled)

    st.markdown("")
    query = query if query else topic

    first_day_of_current_month = datetime(datetime.today().year, datetime.today().month, 1)

    start_date = st.date_input("Start Date:", value=first_day_of_current_month)
    end_date = st.date_input("End Date:", value=datetime.today())

    if st.button("Search", type="primary"):
        if start_date > end_date:
            st.error("Start date cannot be after the end date.")
        elif not query:
            st.warning("Please enter or select a query.")
        else:
            st.session_state.search_history.insert(0, query)
            if len(st.session_state.search_history) > 5:
                st.session_state.search_history.pop()
            
            with st.spinner("Fetching news..."):
                results = scraper.scrape(query=query, start_date=start_date, end_date=end_date)
                
                if results:
                    for result in results:
                        sentiment = sentiment_analyzer(result["description"])[0]
                        sentiment_label = sentiment["label"]
                        sentiment_score = sentiment["score"]
                        if sentiment_label == "POSITIVE":
                            progress_color = "green"
                        elif sentiment_label == "NEGATIVE":
                            progress_color = "red"
                        else:
                            progress_color = "gray"

                        st.markdown("---")
                        with st.container():
                            st.subheader(result["title"])
                            st.write(f"**Source:** {result['source']}")
                            st.write(f"**Date:** {result['date']}")
                            st.write(f"**Description:** {result['description']}")
                            st.markdown(f"**Sentiment:** <span style='color:{progress_color}; font-weight:bold;'>{sentiment_label}</span> (Score: {sentiment_score:.2f})", unsafe_allow_html=True)

                            st.markdown(
                                f"""
                                <div style="height: 10px; background-color: {progress_color}; width: {sentiment_score * 100}%;">
                                </div>
                                """,
                                unsafe_allow_html=True
                            )
                            st.markdown(f"[Go to Page]({result['url']})", unsafe_allow_html=True)
                else:
                    st.info("Please try again, might be a network delay.")

with col2:
    st.markdown("")
    st.markdown("---")
    st.markdown("")
    st.markdown("## πŸ”„ Recent Searches")
    if st.session_state.search_history:
        for idx, past_query in enumerate(st.session_state.search_history):
            st.button(past_query, key=f"search_{idx}_{past_query}")
    else:
        st.text('No previous searches.')