summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlodur871 <42621369+Flodur871@users.noreply.github.com>2021-09-17 20:09:25 +0000
committerGitHub <noreply@github.com>2021-09-17 16:09:25 -0400
commitcfa83a168096d0a94e64601de63706d22f26eb79 (patch)
treefc3bf15d75137616e4b45f1a3d1c2b4f79d96016
parent595a94faf68688af9504105d1c2543690639e4b8 (diff)
Added start date argument to newsapi (#749)
* Added start date argument to newsapi * Fixed typo * Fix getting tail of the news * Show newsapi error * Added show_newest arg to news * Added sort arg to news * Updated news argument
-rw-r--r--gamestonk_terminal/common/newsapi_view.py28
-rw-r--r--gamestonk_terminal/stocks/stocks_controller.py22
2 files changed, 43 insertions, 7 deletions
diff --git a/gamestonk_terminal/common/newsapi_view.py b/gamestonk_terminal/common/newsapi_view.py
index f1c51344a52..64f04b09ee8 100644
--- a/gamestonk_terminal/common/newsapi_view.py
+++ b/gamestonk_terminal/common/newsapi_view.py
@@ -1,14 +1,17 @@
""" News View """
__docformat__ = "numpy"
-from datetime import datetime, timedelta
-
import requests
from gamestonk_terminal import config_terminal as cfg
-def news(term: str, num: int):
+def news(
+ term: str,
+ num: int,
+ s_from: str,
+ show_newest: bool,
+):
"""Display news for a given title. [Source: NewsAPI]
Parameters
@@ -17,10 +20,12 @@ def news(term: str, num: int):
term to search on the news articles
num : int
number of articles to display
+ s_from: str
+ date to start searching articles from formatted YYYY-MM-DD
+ show_newest: bool
+ flag to show newest articles first
"""
# TODO: Add argument to specify news source being used
- # TODO: Add argument to specify date start to search for articles
- s_from = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")
response = requests.get(
f"https://newsapi.org/v2/everything?q={term}&from={s_from}"
@@ -28,7 +33,10 @@ def news(term: str, num: int):
)
# Check that the API response was successful
- if response.status_code != 200:
+ if response.status_code == 426:
+ print(f"Error in request: {response.json()['message']}", "\n")
+
+ elif response.status_code != 200:
print(f"Error in request {response.status_code}. Check News API token", "\n")
else:
@@ -36,7 +44,13 @@ def news(term: str, num: int):
f"{response.json()['totalResults']} news articles for {term} were found since {s_from}\n"
)
- for idx, article in enumerate(response.json()["articles"]):
+ if show_newest:
+ articles = response.json()["articles"]
+
+ else:
+ articles = response.json()["articles"][::-1]
+
+ for idx, article in enumerate(articles):
print(
article["publishedAt"].replace("T", " ").replace("Z", ""),
" ",
diff --git a/gamestonk_terminal/stocks/stocks_controller.py b/gamestonk_terminal/stocks/stocks_controller.py
index a9c2eb7c45d..4b003a42ebb 100644
--- a/gamestonk_terminal/stocks/stocks_controller.py
+++ b/gamestonk_terminal/stocks/stocks_controller.py
@@ -2,6 +2,7 @@ import argparse
import os
from typing import List
+from datetime import datetime, timedelta
import pandas as pd
from colorama import Style
from prompt_toolkit.completion import NestedCompleter
@@ -31,6 +32,7 @@ from gamestonk_terminal.stocks.research import res_controller
from gamestonk_terminal.stocks.screener import screener_controller
from gamestonk_terminal.stocks.stocks_helper import candle, load, quote
from gamestonk_terminal.stocks.technical_analysis import ta_controller
+from gamestonk_terminal.helper_funcs import valid_date
# pylint: disable=R1710
@@ -232,6 +234,24 @@ Market {('CLOSED', 'OPEN')[b_is_stock_market_open()]}
default=5,
help="Number of latest news being printed.",
)
+ parser.add_argument(
+ "-d",
+ "--date",
+ action="store",
+ dest="n_start_date",
+ type=valid_date,
+ default=datetime.now() - timedelta(days=7),
+ help="The starting date (format YYYY-MM-DD) to search articles from",
+ )
+ parser.add_argument(
+ "-o",
+ "--oldest",
+ action="store_false",
+ dest="n_oldest",
+ default=True,
+ help="Show oldest articles first",
+ )
+
try:
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
@@ -240,6 +260,8 @@ Market {('CLOSED', 'OPEN')[b_is_stock_market_open()]}
newsapi_view.news(
term=self.ticker,
num=ns_parser.n_num,
+ s_from=ns_parser.n_start_date.strftime("%Y-%m-%d"),
+ show_newest=ns_parser.n_oldest,
)
except Exception as e: