Skip to content

Commit

Permalink
Code Updated For without API key
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiKollipaka committed May 17, 2023
1 parent 367f6ec commit 03223bd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 120 deletions.
31 changes: 0 additions & 31 deletions openbb_terminal/keys_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,34 +1263,3 @@ def call_ultima(self, other_args: List[str]):
self.status_dict["ultima"] = keys_model.set_ultima_key(
key=ns_parser.key, persist=True, show_output=True
)

@log_start_end(log=logger)
def call_althub(self, other_args: List[str]):
"""Process althub command"""
parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="althub",
description="Set Althub API key.",
)
parser.add_argument(
"-k",
"--key",
type=str,
dest="key",
help="key",
)

if not other_args:
console.print(
"For your API Key, https://althub-backend.invisagealpha.com/api/OnclusiveSentiment/"
)
return
if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-k")
ns_parser = self.parse_simple_args(parser, other_args)

if ns_parser:
self.status_dict["althub"] = keys_model.set_althub_key(
key=ns_parser.key, persist=True, show_output=True
)
70 changes: 0 additions & 70 deletions openbb_terminal/keys_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

API_DICT: Dict = {
"av": "ALPHA_VANTAGE",
"althub": "ALTHUB",
"fmp": "FINANCIAL_MODELING_PREP",
"quandl": "QUANDL",
"polygon": "POLYGON",
Expand Down Expand Up @@ -2856,73 +2855,4 @@ def check_ultima_key(show_output: bool = False) -> str:
if show_output:
console.print(status.colorize())

return str(status)

def set_althub_key(key: str, persist: bool = False, show_output: bool = False) -> str:
"""Set althub key
Parameters
----------
key: str
API key
persist: bool, optional
If False, api key change will be contained to where it was changed. For example, a Jupyter notebook session.
If True, api key change will be global, i.e. it will affect terminal environment variables.
By default, False.
show_output: bool, optional
Display status string or not. By default, False.
Returns
-------
str
Status of key set
Examples
--------
>>> from openbb_terminal.sdk import openbb
>>> openbb.keys.althub(key="example_key")
"""

handle_credential("API_ALTHUB_TOKEN", key, persist)
return check_althub_key(show_output)

def check_althub_key(show_output: bool = False) -> str:
"""Check Polygon key
Parameters
----------
show_output: bool
Display status string or not. By default, False.
Returns
-------
str
Status of key set
"""

current_user = get_current_user()

if current_user.credentials.API_ALTHUB_TOKEN == "REPLACE_ME":
logger.info("Althub key not defined")
status = KeyStatus.NOT_DEFINED
else:
headers = {
"accept": "application/json",
"Authorization": f"token {current_user.credentials.API_ALTHUB_TOKEN}",
}
params = {"limit":1,"all_feilds":False}

BASE_URL = "https://althub-backend.invisagealpha.com/api/OnclusiveSentiment/?all_feilds=False&limit=1"

r = request(
url=f"{BASE_URL}sdk/token", method="GET", headers=headers, params=params
)
if r.status_code in [403, 401, 429 , 511]:
logger.warning("Althub key defined, test failed")
status = KeyStatus.DEFINED_TEST_FAILED
elif r.status_code == 200:
logger.info("Althub key defined, test passed")
status = KeyStatus.DEFINED_TEST_PASSED
else:
logger.warning("Althub key defined, test inconclusive")
status = KeyStatus.DEFINED_TEST_INCONCLUSIVE

if show_output:
console.print(status.colorize())

return str(status)
3 changes: 2 additions & 1 deletion openbb_terminal/miscellaneous/sources/openbb_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@
"YahooFinance",
"AlphaVantage",
"Polygon",
"EODHD"
"EODHD",
"AlthubPlatform"
],
"headlines": [
"FinBrain"
Expand Down
17 changes: 6 additions & 11 deletions openbb_terminal/stocks/behavioural_analysis/ba_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def print_help(self):
mt.add_cmd("interest", self.ticker)
mt.add_cmd("queries", self.ticker)
mt.add_cmd("rise", self.ticker)
mt.add_cmd("ns", "Althub Platform")
mt.add_cmd("ns")
console.print(text=mt.menu_text, menu="Stocks - Behavioural Analysis")

def custom_reset(self):
Expand Down Expand Up @@ -840,14 +840,6 @@ def call_ns(self, other_args: List[str]):
prog="ns",
description="Shows the News Sentiment articles data",
)
parser.add_argument(
"-t",
"--ticker",
dest="ticker",
help="Ticker to analyze",
type=str,
default=None,
)
parser.add_argument(
"-s",
"--start_date",
Expand Down Expand Up @@ -877,7 +869,7 @@ def call_ns(self, other_args: List[str]):
parser.add_argument(
"-l",
"--limit",
default=100,
default=10,
dest="limit",
type=check_non_negative,
help="Number of news articles to be displayed.",
Expand All @@ -896,8 +888,11 @@ def call_ns(self, other_args: List[str]):
parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED
)
if ns_parser:
ticker = None
if self.ticker:
ticker=self.ticker
news_sentiment_view.display_articles_data(
ticker=ns_parser.ticker,
ticker = ticker,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
date=ns_parser.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import logging
import requests
import pandas as pd
import datetime

from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.decorators import log_start_end
from openbb_terminal.rich_config import console
from openbb_terminal.core.session.current_user import get_current_user

logger = logging.getLogger(__name__)

@log_start_end(log=logger)
@check_api_key(["API_ALTHUB_TOKEN"])
def get_data(
ticker: str = "",
start_date: str = "",
Expand Down Expand Up @@ -41,7 +40,6 @@ def get_data(

headers = {
"accept": "application/json",
"Authorization": f"token {get_current_user().credentials.API_ALTHUB_TOKEN}",
}

df = pd.DataFrame(data=None)
Expand All @@ -63,8 +61,12 @@ def get_data(
return df

if date:
date = datetime.datetime.strptime(date, '%Y-%m-%d')
query_params["published_on"] = date
if start_date:
if date < start_date:
console.print("date must be grater than or equal to start_date")
return df
del query_params["published_on__gte"]
if end_date:
del query_params["published_on__lte"]
Expand All @@ -78,9 +80,15 @@ def get_data(
"https://althub-backend.invisagealpha.com/api/OnclusiveSentiment/",
headers=headers,
params=query_params,
).json()
)
if response.status_code != 200:
console.print("Please check your API Key")
return df
else:
response = response.json()
df = pd.DataFrame(data=response["results"])
df['adjusted_sentiment'] = df['adjusted_sentiment'].astype(float)
if not df.empty:
df['adjusted_sentiment'] = df['adjusted_sentiment'].astype(float)

def condition(x):
if x >= 250:
Expand All @@ -94,7 +102,7 @@ def condition(x):
else:
return "Super Negative"

sentiment = {50:"Positive",-50:"Negative",0:"Neutral"}
sentiment = {50:"Positive",-50:"Negative",0:"Neutral",None:'Neutral'}

if not df.empty:
df['raw_sentiment'] = df['raw_sentiment'].map(sentiment)
Expand Down

0 comments on commit 03223bd

Please sign in to comment.