From f2bc9c1cbe689faf42b7c62be691e8a78bda1194 Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Fri, 16 Apr 2021 10:44:48 +0200 Subject: [PATCH] add trending news list, from Seeking Alpha --- .../discovery/disc_controller.py | 6 +++ .../discovery/seeking_alpha_model.py | 37 +++++++++++++++ .../discovery/seeking_alpha_view.py | 45 +++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/gamestonk_terminal/discovery/disc_controller.py b/gamestonk_terminal/discovery/disc_controller.py index 9feec08025a..12bc5149ad3 100644 --- a/gamestonk_terminal/discovery/disc_controller.py +++ b/gamestonk_terminal/discovery/disc_controller.py @@ -47,6 +47,7 @@ class DiscoveryController: "performance", "spectrum", "latest", + "trending", "news_article" ] @@ -90,6 +91,7 @@ class DiscoveryController: print(" performance performance of sectors, industry, country [Finviz]") print(" spectrum spectrum of sectors, industry, country [Finviz]") print(" latest latest news [SeekingAlpha]") + print(" trending trending news [SeekingAlpha]") print(" news_article news article [SeekingAlpha]") print("") @@ -195,6 +197,10 @@ class DiscoveryController: """Process latest command""" seeking_alpha_view.latest_news_view(other_args) + def call_trending(self, other_args: List[str]): + """Process trending command""" + seeking_alpha_view.trending_news_view(other_args) + def call_news_article(self, other_args: List[str]): """Process news_article command""" seeking_alpha_view.news_article_view(other_args) diff --git a/gamestonk_terminal/discovery/seeking_alpha_model.py b/gamestonk_terminal/discovery/seeking_alpha_model.py index aa74045c041..684e009390a 100755 --- a/gamestonk_terminal/discovery/seeking_alpha_model.py +++ b/gamestonk_terminal/discovery/seeking_alpha_model.py @@ -133,6 +133,43 @@ def get_article_list(num: int) -> list: return articles +def get_trending_list(num: int) -> list: + """Returns a list of trending articles + + Parameters + ---------- + pages : int + Number of articles + + Returns + ------- + list + Trending articles list + """ + + articles = list() + url_articles = "https://seekingalpha.com/news/trending_news" + response = requests.get(url_articles, headers={"User-Agent": get_user_agent()}) + + # Check that the API response was successful + if response.status_code != 200: + print("Invalid response\n") + else: + for item in response.json(): + article_url = item['uri'] + if not article_url.startswith('/news/'): + continue + article_id = article_url.split('/')[2].split('-')[0] + articles.append({ + 'title': item['title'], + 'publishedAt': item['publish_on'], + 'url': "https://seekingalpha.com"+article_url, + 'id': article_id + }) + + return articles + + def get_article_data(article_id: int) -> dict: """Returns an article diff --git a/gamestonk_terminal/discovery/seeking_alpha_view.py b/gamestonk_terminal/discovery/seeking_alpha_view.py index eff8c341112..0d3764c1a34 100644 --- a/gamestonk_terminal/discovery/seeking_alpha_view.py +++ b/gamestonk_terminal/discovery/seeking_alpha_view.py @@ -112,6 +112,51 @@ def latest_news_view(other_args: List[str]): break +def trending_news_view(other_args: List[str]): + """Prints the trending news article list + + Parameters + ---------- + other_args : List[str] + argparse other args - ["-n", "5"] + """ + + parser = argparse.ArgumentParser( + add_help=False, + prog="trending", + description="""Trending news articles. [Source: Seeking Alpha]""", + ) + + parser.add_argument( + "-n", + "--num", + action="store", + dest="n_num", + type=check_positive, + default=10, + help="number of trending articles being printed.", + ) + + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + articles = seeking_alpha_model.get_trending_list(ns_parser.n_num) + for idx, article in enumerate(articles): + print( + article["publishedAt"].replace("T", " ").replace("Z", ""), + "-", + article['id'], + "-", + article["title"], + ) + print(article["url"]) + print("") + + if idx >= ns_parser.n_num - 1: + break + + def news_article_view(other_args: List[str]): """Prints a news article