Skip to content

Commit

Permalink
Added sources argument to the news command (OpenBB-finance#756)
Browse files Browse the repository at this point in the history
* Added sources arg to newsapi

* Updated docs

* Added tld check
  • Loading branch information
nav1s authored Sep 24, 2021
1 parent 942219b commit 78d1678
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
16 changes: 11 additions & 5 deletions gamestonk_terminal/common/newsapi_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def news(
num: int,
s_from: str,
show_newest: bool,
sources: str,
):
"""Display news for a given title. [Source: NewsAPI]
Expand All @@ -24,14 +25,19 @@ def news(
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
"""
# TODO: Add argument to specify news source being used

response = requests.get(
f"https://newsapi.org/v2/everything?q={term}&from={s_from}"
f"&sortBy=publishedAt&language=en&apiKey={cfg.API_NEWS_TOKEN}",
link = (
f"https://newsapi.org/v2/everything?q={term}&from={s_from}&sortBy=publishedAt&language=en"
f"&apiKey={cfg.API_NEWS_TOKEN}"
)

if sources:
link += f"&domains={sources}"

response = requests.get(link)

# Check that the API response was successful
if response.status_code == 426:
print(f"Error in request: {response.json()['message']}", "\n")
Expand Down
17 changes: 17 additions & 0 deletions gamestonk_terminal/stocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [Load](#Load)
* [Quote](#Quote)
* [Candle](#Candle)
* [News](#News)
* [Discover Stocks](#Discover-Stocks-)
* [Behavioural Analysis](#Behavioural-Analysis-)
* [Research](#Research-)
Expand Down Expand Up @@ -49,6 +50,22 @@ Visualize candles historical data, with support and resistance bars, and moving
![nio](https://user-images.githubusercontent.com/25267873/111053397-4d609e00-845b-11eb-9c94-89b8892a8e81.png)


#### News

```text
news [-n N_NUM] [-d N_START_DATE] [-o] [-s N_SOURCES [N_SOURCES ...]]
```

Prints latest news about company, including date, title and web link. [Source: News API]

* -n : Number of latest news being printed. Default 5.
* -d : The starting date (format YYYY-MM-DD) to search articles from.
* -o : Show oldest articles first.
* -s : Show news only from the sources specified (e.g bbc yahoo.com)

<img width="770" alt="Captura de ecrã 2021-03-22, às 22 47 42" src="https://user-images.githubusercontent.com/25267873/112070935-b2587a00-8b66-11eb-8dfb-0353fc83311d.png">


## [Discover Stocks »»](discovery/README.md)

Command|Description|Source
Expand Down
15 changes: 0 additions & 15 deletions gamestonk_terminal/stocks/due_diligence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

This menu aims to help in due-diligence of a pre-loaded stock, and the usage of the following commands along with an example will be exploited below.

* [news](#news)
* latest news of the company [News API]
* [red](#red)
* gets due diligence from another user's post [Reddit]
* [analyst](#analyst)
Expand Down Expand Up @@ -39,19 +37,6 @@ This menu aims to help in due-diligence of a pre-loaded stock, and the usage of
* [customer](#customer)
* list of customers [csimarket]

## news <a name="news"></a>

```text
news [-n N_NUM]
```

Prints latest news about company, including date, title and web link. [Source: News API]

* -n : Number of latest news being printed. Default 10.

<img width="770" alt="Captura de ecrã 2021-03-22, às 22 47 42" src="https://user-images.githubusercontent.com/25267873/112070935-b2587a00-8b66-11eb-8dfb-0353fc83311d.png">


## red <a name="red"></a>

```text
Expand Down
13 changes: 13 additions & 0 deletions gamestonk_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,30 @@ def call_news(self, other_args: List[str]):
default=True,
help="Show oldest articles first",
)
parser.add_argument(
"-s",
"--sources",
default=[],
nargs="+",
help="Show news only from the sources specified (e.g bbc yahoo.com)",
)

try:
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return

sources = ns_parser.sources
for idx, source in enumerate(sources):
if source.find(".") == -1:
sources[idx] += ".com"

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,
sources=",".join(sources),
)

except Exception as e:
Expand Down
7 changes: 6 additions & 1 deletion website/content/stocks/news/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```
usage: news [-n N_NUM] [-h]
usage: news [-n N_NUM] [-d N_START_DATE] [-o] [-s N_SOURCES [N_SOURCES ...]] [-h]
```

Prints latest news about company, including date, title and web link. [Source: News API]
Expand All @@ -8,5 +8,10 @@ Prints latest news about company, including date, title and web link. [Source: N
optional arguments:
-n N_NUM, --num N_NUM
Number of latest news being printed.
-d N_START_DATE, --date N_START_DATE
The starting date (format YYYY-MM-DD) to search articles from
-o, --oldest Show oldest articles first
-s N_SOURCES [N_SOURCES ...], --sources N_SOURCES [N_SOURCES ...]
Show news only from the sources specified (e.g bbc yahoo.com)
-h, --help show this help message
```

0 comments on commit 78d1678

Please sign in to comment.