Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds command to check top hackernews stories #3841

Merged
merged 25 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f70b60d
fix(docs): added margin between sections
jose-donato Oct 8, 2022
4d72f4a
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Oct 9, 2022
cb806f4
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Oct 10, 2022
59a01f8
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Oct 10, 2022
1c48624
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Oct 12, 2022
a1fb9f6
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Oct 22, 2022
39c7bce
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Oct 31, 2022
72cdc09
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 1, 2022
c58cf67
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Nov 1, 2022
5ef060a
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 2, 2022
74fd42f
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Nov 2, 2022
d9af923
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 8, 2022
7878a74
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Nov 11, 2022
bea6e41
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 21, 2022
38e6d18
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Nov 22, 2022
0f02fbc
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 22, 2022
2f2ace5
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 22, 2022
49e575e
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 22, 2022
b91c014
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Nov 23, 2022
ecdd7da
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal…
jose-donato Nov 27, 2022
03bd5d5
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Dec 13, 2022
e90aa22
Merge branch 'main' of github.com:GamestonkTerminal/GamestonkTerminal
jose-donato Dec 29, 2022
1f49b58
feat: added hn command
jose-donato Dec 29, 2022
8f1e686
fix: added log decorator
jose-donato Jan 3, 2023
809eede
Merge branch 'main' into hackernews
jmaslek Jan 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion openbb_terminal/alternative/alt_controller.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""Alternative Data Controller Module"""
__docformat__ = "numpy"

import argparse
import logging
from typing import List

from openbb_terminal.custom_prompt_toolkit import NestedCompleter

from openbb_terminal import feature_flags as obbff
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import (
EXPORT_ONLY_RAW_DATA_ALLOWED,
)
from openbb_terminal.menu import session
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import console, MenuText
from openbb_terminal.alternative import hackernews_view

logger = logging.getLogger(__name__)
# pylint:disable=import-outside-toplevel
Expand All @@ -19,7 +24,7 @@
class AlternativeDataController(BaseController):
"""Alternative Controller class"""

CHOICES_COMMANDS: List[str] = []
CHOICES_COMMANDS: List[str] = ["hn"]
CHOICES_MENUS = ["covid", "oss"]
PATH = "/alternative/"
CHOICES_GENERATION = True
Expand All @@ -38,6 +43,8 @@ def print_help(self):
mt = MenuText("alternative/")
mt.add_menu("covid")
mt.add_menu("oss")
mt.add_raw("\n")
mt.add_cmd("hn")
console.print(text=mt.menu_text, menu="Alternative")

@log_start_end(log=logger)
Expand All @@ -53,3 +60,24 @@ def call_oss(self, _):
from openbb_terminal.alternative.oss.oss_controller import OSSController

self.queue = self.load_class(OSSController, self.queue)

@log_start_end(log=logger)
def call_hn(self, other_args: List[str]):
"""Process hn command"""

parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="hn",
description="Display Hacker News",
)

ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
limit=10,
)

if ns_parser:
hackernews_view.display_stories(limit=ns_parser.limit)
38 changes: 38 additions & 0 deletions openbb_terminal/alternative/hackernews_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""HackerNews Model"""
__docformat__ = "numpy"


import logging
import pandas as pd
import requests


logger = logging.getLogger(__name__)


def get_stories(limit: int = 10) -> pd.DataFrame:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add the log_start_end decorator here

"""Get top stories from HackerNews.
Parameters
----------
limit: int
Number of stories to return
Returns
-------
pd.DataFrame
DataFrame with stories
"""

res = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json")
if res.status_code == 200:
top_stories = res.json()
stories = []
for story_id in top_stories[:limit]:
story = requests.get(
f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"
).json()
stories.append(story)
df = pd.DataFrame(stories)
df["time"] = pd.to_datetime(df["time"], unit="s")
df = df[["title", "url", "score", "type", "time"]]
return df
return pd.DataFrame()
31 changes: 31 additions & 0 deletions openbb_terminal/alternative/hackernews_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""HackerNews view"""
__docformat__ = "numpy"

import logging
import os

from openbb_terminal.alternative.hackernews_model import get_stories
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import (
export_data,
print_rich_table,
)

logger = logging.getLogger(__name__)


@log_start_end(log=logger)
def display_stories(limit: int = 10, export: str = "") -> None:
"""View top stories from HackerNews.
Parameters
----------
limit: int
Number of stories to return
export: str
Export dataframe data to csv,json,xlsx file
"""
df = get_stories(limit)
if not df.empty:
df.columns = [col.capitalize() for col in df.columns]
print_rich_table(df, title="HackerNews Top Stories")
export_data(export, os.path.dirname(os.path.abspath(__file__)), "hn", df)
3 changes: 2 additions & 1 deletion openbb_terminal/miscellaneous/data_sources_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
"rs": ["GitHub"],
"sh": ["GitHub"],
"tr": ["GitHub"]
}
},
"hn": ["HackerNews"]
}
}
1 change: 1 addition & 0 deletions openbb_terminal/miscellaneous/i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ en:
funds/alswe: display fund allocation (sector, country, holdings)
funds/infoswe: get fund information
funds/forecast: forecasting techniques
alternative/hn: Hacker News most popular stories
alternative/covid: COVID menu, cases, deaths, rates
alternative/oss: Open Source menu, star history, repos information
alternative/covid/slopes: get countries with highest slope in cases
Expand Down
1 change: 1 addition & 0 deletions openbb_terminal/miscellaneous/library/trail_map.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
trail,model,view
alt.hn,openbb_terminal.alternative.hackernews_model.get_stories,openbb_terminal.alternative.hackernews_view.display_stories
alt.covid.slopes,openbb_terminal.alternative.covid.covid_model.get_case_slopes,openbb_terminal.alternative.covid.covid_view.display_case_slopes
alt.covid.global_cases,openbb_terminal.alternative.covid.covid_model.get_global_cases,
alt.covid.global_deaths,openbb_terminal.alternative.covid.covid_model.get_global_deaths,
Expand Down
Loading