Spaces:
Running
Running
Benjamin Consolvo
commited on
Commit
·
5fda2f9
1
Parent(s):
4f4559a
alpha vantage for news headlines
Browse files
app.py
CHANGED
|
@@ -128,10 +128,11 @@ class NewsSentiment:
|
|
| 128 |
def __init__(self, API_KEY):
|
| 129 |
self.newsapi = NewsApiClient(api_key=API_KEY)
|
| 130 |
self.sia = SentimentIntensityAnalyzer()
|
|
|
|
| 131 |
|
| 132 |
def get_sentiment_and_headlines(self, symbol):
|
| 133 |
"""
|
| 134 |
-
Try NewsAPI first, fallback to
|
| 135 |
Returns (sentiment, headlines).
|
| 136 |
"""
|
| 137 |
# Try NewsAPI
|
|
@@ -145,24 +146,34 @@ class NewsSentiment:
|
|
| 145 |
logger.warning(f"NewsAPI returned no headlines for {symbol}.")
|
| 146 |
except Exception as e:
|
| 147 |
logger.error(f"NewsAPI error for {symbol}: {e}")
|
| 148 |
-
|
| 149 |
-
logger.info(f"Falling back to yfinance for {symbol} sentiment and headlines.")
|
| 150 |
|
| 151 |
-
# Fallback to
|
| 152 |
try:
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if headlines:
|
| 157 |
-
logger.info(f"Using
|
| 158 |
sentiment = self._calculate_sentiment(headlines)
|
| 159 |
return sentiment, headlines
|
| 160 |
else:
|
| 161 |
-
logger.warning(f"
|
| 162 |
except Exception as e:
|
| 163 |
-
logger.error(f"
|
| 164 |
|
| 165 |
-
logger.info(
|
|
|
|
|
|
|
| 166 |
return None, []
|
| 167 |
|
| 168 |
def _calculate_sentiment(self, headlines):
|
|
|
|
| 128 |
def __init__(self, API_KEY):
|
| 129 |
self.newsapi = NewsApiClient(api_key=API_KEY)
|
| 130 |
self.sia = SentimentIntensityAnalyzer()
|
| 131 |
+
self.alpha_vantage_api_key = st.secrets.get("ALPHA_VANTAGE_API_KEY") # Add your Alpha Vantage key to Streamlit secrets
|
| 132 |
|
| 133 |
def get_sentiment_and_headlines(self, symbol):
|
| 134 |
"""
|
| 135 |
+
Try NewsAPI first, fallback to Alpha Vantage if needed.
|
| 136 |
Returns (sentiment, headlines).
|
| 137 |
"""
|
| 138 |
# Try NewsAPI
|
|
|
|
| 146 |
logger.warning(f"NewsAPI returned no headlines for {symbol}.")
|
| 147 |
except Exception as e:
|
| 148 |
logger.error(f"NewsAPI error for {symbol}: {e}")
|
| 149 |
+
logger.info(f"Falling back to Alpha Vantage for {symbol} sentiment and headlines.")
|
|
|
|
| 150 |
|
| 151 |
+
# Fallback to Alpha Vantage
|
| 152 |
try:
|
| 153 |
+
if not self.alpha_vantage_api_key:
|
| 154 |
+
logger.error("Alpha Vantage API key not found in Streamlit secrets.")
|
| 155 |
+
return None, []
|
| 156 |
+
import requests
|
| 157 |
+
url = (
|
| 158 |
+
f"https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers={symbol}"
|
| 159 |
+
f"&apikey={self.alpha_vantage_api_key}"
|
| 160 |
+
)
|
| 161 |
+
resp = requests.get(url)
|
| 162 |
+
data = resp.json()
|
| 163 |
+
# Alpha Vantage returns a list of news items under "feed"
|
| 164 |
+
headlines = [item.get("title") for item in data.get("feed", [])[:5] if item.get("title")]
|
| 165 |
if headlines:
|
| 166 |
+
logger.info(f"Using Alpha Vantage headlines for {symbol}: {headlines}")
|
| 167 |
sentiment = self._calculate_sentiment(headlines)
|
| 168 |
return sentiment, headlines
|
| 169 |
else:
|
| 170 |
+
logger.warning(f"Alpha Vantage returned no headlines for {symbol}.")
|
| 171 |
except Exception as e:
|
| 172 |
+
logger.error(f"Alpha Vantage error for {symbol}: {e}")
|
| 173 |
|
| 174 |
+
logger.info(
|
| 175 |
+
f"No sentiment/headlines available for {symbol} from either NewsAPI or Alpha Vantage."
|
| 176 |
+
)
|
| 177 |
return None, []
|
| 178 |
|
| 179 |
def _calculate_sentiment(self, headlines):
|