From 1bba88b2e671ebd21bd74fd5a080e3b9879cc18d Mon Sep 17 00:00:00 2001 From: didier Date: Fri, 16 Apr 2021 23:10:50 +0100 Subject: [PATCH] refactor to allow to access articles from latest/trending commands --- .../discovery/disc_controller.py | 6 - .../discovery/seeking_alpha_view.py | 140 ++++++++++-------- 2 files changed, 78 insertions(+), 68 deletions(-) diff --git a/gamestonk_terminal/discovery/disc_controller.py b/gamestonk_terminal/discovery/disc_controller.py index a9a66116107..d74eaf19814 100644 --- a/gamestonk_terminal/discovery/disc_controller.py +++ b/gamestonk_terminal/discovery/disc_controller.py @@ -48,7 +48,6 @@ class DiscoveryController: "spectrum", "latest", "trending", - "news_article", ] def __init__(self): @@ -92,7 +91,6 @@ class DiscoveryController: 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("") def switch(self, an_input: str): @@ -201,10 +199,6 @@ class DiscoveryController: """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) - def menu(): """Discovery Menu""" diff --git a/gamestonk_terminal/discovery/seeking_alpha_view.py b/gamestonk_terminal/discovery/seeking_alpha_view.py index 639e145f0d0..a11c548be72 100644 --- a/gamestonk_terminal/discovery/seeking_alpha_view.py +++ b/gamestonk_terminal/discovery/seeking_alpha_view.py @@ -73,15 +73,22 @@ def latest_news_view(other_args: List[str]): Parameters ---------- other_args : List[str] - argparse other args - ["-n", "5"] + argparse other args - ["-i", "123123", "-n", "5"] """ - parser = argparse.ArgumentParser( add_help=False, prog="latest", description="""Latest news articles. [Source: Seeking Alpha]""", ) - + parser.add_argument( + "-i", + "--id", + action="store", + dest="n_id", + type=check_positive, + default=-1, + help="article number found on Seeking Alpha website", + ) parser.add_argument( "-n", "--num", @@ -92,24 +99,44 @@ def latest_news_view(other_args: List[str]): help="number of latest articles being printed", ) + if other_args: + if "-" not in other_args[0]: + other_args.insert(0, "-i") + ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return - articles = seeking_alpha_model.get_article_list(ns_parser.n_num) - for idx, article in enumerate(articles): + # User wants to see all latest news + if ns_parser.n_id == -1: + articles = seeking_alpha_model.get_article_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 + + # User wants to access specific article + else: + article = seeking_alpha_model.get_article_data(ns_parser.n_id) print( - article["publishedAt"].replace("T", " ").replace("Z", ""), - "-", - article["id"], - "-", + article["publishedAt"][: article["publishedAt"].rfind(":") - 3].replace( + "T", " " + ), + " ", article["title"], ) print(article["url"]) print("") - - if idx >= ns_parser.n_num - 1: - break + print(article["content"]) def trending_news_view(other_args: List[str]): @@ -118,15 +145,22 @@ def trending_news_view(other_args: List[str]): Parameters ---------- other_args : List[str] - argparse other args - ["-n", "5"] + argparse other args - ["i", "123123", "-n", "5"] """ - parser = argparse.ArgumentParser( add_help=False, prog="trending", description="""Trending news articles. [Source: Seeking Alpha]""", ) - + parser.add_argument( + "-i", + "--id", + action="store", + dest="n_id", + type=check_positive, + default=-1, + help="article number found on Seeking Alpha website", + ) parser.add_argument( "-n", "--num", @@ -137,59 +171,41 @@ def trending_news_view(other_args: List[str]): help="number of trending articles being printed", ) + if other_args: + if "-" not in other_args[0]: + other_args.insert(0, "-i") + 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): + # User wants to see all trending articles + if ns_parser.n_id == -1: + 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 + + # User wants to access specific article + else: + article = seeking_alpha_model.get_article_data(ns_parser.n_id) print( - article["publishedAt"].replace("T", " ").replace("Z", ""), - "-", - article["id"], - "-", + article["publishedAt"][: article["publishedAt"].rfind(":") - 3].replace( + "T", " " + ), + " ", 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 - - Parameters - ---------- - other_args : List[str] - argparse other args - ["-i", "3681944"] - """ - - parser = argparse.ArgumentParser( - add_help=False, - prog="up_article", - description="""News article. [Source: Seeking Alpha]""", - ) - - parser.add_argument( - "-i", - "--id", - action="store", - dest="n_id", - type=check_positive, - default=-1, - help="article number found on Seeking Alpha website", - ) - - ns_parser = parse_known_args_and_warn(parser, other_args) - if not ns_parser: - return - if ns_parser.n_id == -1: - return - - article = seeking_alpha_model.get_article_data(ns_parser.n_id) - print(article["publishedAt"], " ", article["title"]) - print(article["url"]) - print("") - print(article["content"]) + print(article["content"])