Files
OpenBB/openbb_terminal/common/newsapi_model.py
montezdesousa cf9670303c Populate model functions with check_api_key decorator (#2755)
* fred decorator

* fmp decorator

* quandl decorator

* reddit decorator

* polygon decorator

* twitter decorator

* news decorator

* tradier decorator

* cmc decorator

* binance decorator

* finnhub decorator

* iex decorator

* sentiment investor

* walert decorator

* glassnode decorator

* coinglass decorator

* cryptopanic decorator

* bitquery decorator

* smartstake decorator

* messari decorator

* github decorator

* santiment decorator

* eodhd decorator

* import check_api_decorator

* fix some decorators

* fix some decorators

* remove some decorator

* decorator fixes

* black

* mypy

* mypy again

* mypy again

* pylint

* decorator

* too many line

* fix fred ycrv

* mypy

* rewrite events test

* another try

Co-authored-by: minhhoang1023 <40023817+minhhoang1023@users.noreply.github.com>
2022-10-12 17:36:49 +01:00

101 lines
2.9 KiB
Python

""" News Model """
__docformat__ = "numpy"
import logging
from datetime import datetime, timedelta
from typing import Any, List, Tuple
import requests
import pandas as pd
from openbb_terminal import config_terminal as cfg
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.rich_config import console
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
@check_api_key(["API_NEWS_TOKEN"])
def get_news(
query: str,
limit: int = 10,
start_date: str = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d"),
show_newest: bool = True,
sources: str = "",
) -> List[Tuple[Any, Any]]:
"""Get news for a given term. [Source: NewsAPI]
Parameters
----------
query : str
term to search on the news articles
start_date: str
date to start searching articles from formatted YYYY-MM-DD
show_newest: bool
flag to show newest articles first
sources: str
sources to exclusively show news from
Returns
----------
tables : List[Tuple]
List of tuples containing news df in first index and dict containing title of news df
"""
link = (
f"https://newsapi.org/v2/everything?q={query}&from={start_date}&sortBy=publishedAt"
f"&language=en&apiKey={cfg.API_NEWS_TOKEN}"
)
if sources:
link += f"&domain={sources}"
response = requests.get(link)
articles = {}
# Check that the API response was successful
if response.status_code == 200:
response_json = response.json()
console.print(
f"{response_json['totalResults']} news articles for",
f" {query} were found since {start_date}\n",
)
if show_newest:
articles = response_json["articles"]
else:
articles = response_json["articles"][::-1]
elif response.status_code == 426:
console.print(f"Error in request: {response.json()['message']}", "\n")
elif response.status_code == 401:
console.print("[red]Invalid API Key[/red]\n")
elif response.status_code == 429:
console.print("[red]Exceeded number of calls per minute[/red]\n")
else:
console.print(f"Error in request: {response.json()['message']}", "\n")
tables = []
if articles:
for idx, article in enumerate(articles):
# Unnecessary to use source name because contained in link article["source"]["name"]
data = [
[article["publishedAt"].replace("T", " ").replace("Z", "")],
[f"{article['description']}"],
[article["url"]],
]
table = pd.DataFrame(
data, index=["published", "content", "link"], columns=["Content"]
)
table.columns = table.columns.str.title()
tables.append((table, article))
if idx >= limit - 1:
break
return tables