MCPcopy Create free account
hub / github.com/geekcomputers/Python / fetch_news

Function fetch_news

news_oversimplifier.py:117–137  ·  view source on GitHub ↗

Fetches a list of news articles from NewsAPI based on a query string. args: api_key (str): NewsAPI key loaded from environment. query (str): The keyword to search for in news articles. max_articles (int): Maximum number of articles to fetch. returns: li

(api_key, query, max_articles=5)

Source from the content-addressed store, hash-verified

115
116
117def fetch_news(api_key, query, max_articles=5): # no pytest
118 """
119 Fetches a list of news articles from NewsAPI based on a query string.
120
121 args:
122 api_key (str): NewsAPI key loaded from environment.
123 query (str): The keyword to search for in news articles.
124 max_articles (int): Maximum number of articles to fetch.
125
126 returns:
127 list: List of dictionaries, each representing a news article.
128
129 raises:
130 Exception: If the API response status is not 'ok'.
131 """
132 url = f"https://newsapi.org/v2/everything?q={query}&language=en&apiKey={api_key}&pageSize={max_articles}"
133 response = requests.get(url)
134 data = response.json()
135 if data.get("status") != "ok":
136 raise Exception("Failed to fetch news:", data.get("message"))
137 return data["articles"]
138
139
140def save_summary(title, summary, path="summaries.txt"): # no pytest

Callers 1

mainFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected