22import logging
33import textwrap
44import time
5+ import math
56from typing import Dict , List
67import pendulum
78from bs4 import BeautifulSoup as bs
@@ -120,18 +121,10 @@ def get_news(self, symbol: str, limit: int = 6) -> List[Dict[str, str]]:
120121 """
121122 benzinga_news = self ._get_benzinga_news (symbol = symbol , limit = limit )
122123 yahoo_news = self ._get_yahoo_news (
123- symbol = symbol , limit = (limit - len (benzinga_news [:3 ]))
124+ symbol = symbol , limit = (limit - len (benzinga_news [: ( math . floor ( limit / 2 )) ]))
124125 )
125126
126- news = benzinga_news [:3 ] + yahoo_news [: (limit - len (benzinga_news [:3 ]))]
127-
128- # if len(benzinga_news) == 0:
129- # news = yahoo_news
130- # else:
131- # news = benzinga_news[:3]
132- # news.append(yahoo_news[:(limit - len(benzinga_news))])
133-
134- # news = yahoo_news + benzinga_news
127+ news = benzinga_news [: (math .floor (limit / 2 ))] + yahoo_news
135128
136129 sorted_news = sorted (
137130 news , key = lambda x : pendulum .parse (x ["publish_date" ]), reverse = True
@@ -155,10 +148,13 @@ def _get_yahoo_news(self, symbol: str, limit: int = 6) -> List[Dict[str, str]]:
155148 news_response = ticker .news
156149
157150 yahoo_news = []
158- for news in news_response [:limit ]:
151+ news_count = 0
152+ for news in news_response :
159153 try :
160154 content = self .strip_html (self .scrape_article (news ["link" ]))
161-
155+ if not content :
156+ continue
157+ news_count += 1
162158 yahoo_news .append (
163159 {
164160 "title" : news ["title" ],
@@ -174,6 +170,10 @@ def _get_yahoo_news(self, symbol: str, limit: int = 6) -> List[Dict[str, str]]:
174170 except Exception as e :
175171 logging .error (f"Error scraping article: { e } " )
176172 continue
173+ else :
174+ if news_count == limit :
175+ news_count = 0
176+ break
177177
178178 return yahoo_news
179179
0 commit comments