Skip to content

Commit

Permalink
Hotfix/disc upcoming (#4643)
Browse files Browse the repository at this point in the history
* Fix start_date

* Fix start_date

* Fix linting

* Fix limit parameter, view and tests

* Remove page_num, adjust view
  • Loading branch information
IgorWounds authored Apr 1, 2023
1 parent f87154b commit a0c0a99
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 64 deletions.
20 changes: 5 additions & 15 deletions openbb_terminal/stocks/discovery/disc_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import argparse
import logging
from datetime import date, datetime
from datetime import datetime
from typing import List, Optional

from openbb_terminal.core.session.current_user import get_current_user
Expand Down Expand Up @@ -659,25 +659,16 @@ def call_upcoming(self, other_args: List[str]):
action="store",
dest="limit",
type=check_positive,
default=10,
help="Limit of upcoming earnings release dates to display.",
)
parser.add_argument(
"-p",
"--pages",
action="store",
dest="n_pages",
type=check_positive,
default=1,
help="Number of pages to read upcoming earnings from in Seeking Alpha website.",
default=5,
help="Limit of upcoming earnings release dates to look ahead.",
)
parser.add_argument(
"-s",
"--start",
type=valid_date,
help="Start date of data, in YYYY-MM-DD format",
help="Start date of data, in YYYY-MM-DD format. Defaults to today.",
dest="start_date",
default=date.today(),
default=datetime.today().strftime("%Y-%m-%d"),
)
if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-l")
Expand All @@ -686,7 +677,6 @@ def call_upcoming(self, other_args: List[str]):
)
if ns_parser:
seeking_alpha_view.upcoming_earning_release_dates(
num_pages=ns_parser.n_pages,
limit=ns_parser.limit,
start_date=ns_parser.start_date,
export=ns_parser.export,
Expand Down
28 changes: 18 additions & 10 deletions openbb_terminal/stocks/discovery/seeking_alpha_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logging
from datetime import date, timedelta
from typing import Dict, List, Optional
from typing import Dict, List

import pandas as pd
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -43,40 +43,39 @@ def get_filters(date_str: str) -> str:


@log_start_end(log=logger)
def get_next_earnings(limit: int = 10, start: Optional[date] = None) -> DataFrame:
def get_next_earnings(limit: int = 5, start_date: date = date.today()) -> DataFrame:
"""Returns a DataFrame with upcoming earnings
Parameters
----------
limit : int
Number of pages
date: Optional[date]
Date to start from
Number of days to look ahead
start_date: date
Date to start from. Defaults to today
Returns
-------
DataFrame
Upcoming earnings DataFrame
"""
if start is None:
start = date.today()
base_url = "https://seekingalpha.com/api/v3/earnings_calendar/tickers"
df_earnings = pd.DataFrame()

for _ in range(0, limit):
date_str = start.strftime("%Y-%m-%d")
start_date = pd.to_datetime(start_date)
date_str = str(start_date.strftime("%Y-%m-%d"))
response = request(base_url + get_filters(date_str), timeout=10)
json = response.json()
try:
data = json["data"]
cleaned_data = [x["attributes"] for x in data]
temp_df = pd.DataFrame.from_records(cleaned_data)
temp_df = temp_df.drop(columns=["sector_id"])
temp_df["Date"] = start
temp_df["Date"] = start_date # pylint: disable=E1137
df_earnings = pd.concat(
[df_earnings, temp_df], join="outer", ignore_index=True
)
start = start + timedelta(days=1)
start_date = start_date + timedelta(days=1)
except KeyError:
pass

Expand All @@ -88,6 +87,15 @@ def get_next_earnings(limit: int = 10, start: Optional[date] = None) -> DataFram
"exchange": "Exchange",
}
)

if df_earnings.empty:
console.print("No earnings found. Try adjusting the date.\n")
return pd.DataFrame()

df_earnings = df_earnings[
df_earnings["Date"] <= pd.to_datetime(start_date + timedelta(days=limit))
]

return df_earnings


Expand Down
10 changes: 4 additions & 6 deletions openbb_terminal/stocks/discovery/seeking_alpha_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

@log_start_end(log=logger)
def upcoming_earning_release_dates(
num_pages: int = 1,
limit: int = 10,
start_date: Optional[date] = None,
limit: int = 5,
start_date: date = date.today(),
export: str = "",
sheet_name: Optional[str] = None,
):
Expand All @@ -37,8 +36,8 @@ def upcoming_earning_release_dates(
export : str
Export dataframe data to csv,json,xlsx file
"""
print(start_date)
df_earnings = seeking_alpha_model.get_next_earnings(num_pages, start_date)

df_earnings = seeking_alpha_model.get_next_earnings(limit, start_date)

if df_earnings.empty:
console.print("No upcoming earnings release dates found")
Expand All @@ -50,7 +49,6 @@ def upcoming_earning_release_dates(
headers=df_earnings.columns,
title="Upcoming Earnings Releases",
export=bool(export),
limit=limit,
)

if export:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ interactions:
response:
body:
string: !!binary |
H4sIAAAAAAAAA6tWSkksSVSyio6tBQCUZLJeCwAAAA==
H4sIAAAAAAAAA63RTUvEMBAG4L9S5lwWwe3K9hYQVLDWj4sgUsZ0aIPppCYTsJT+d5OD7H31mHln
Jg/JCj0KQv22gumhhsOhujheQgmyzJTOhJ4ND51GS9yj78ToT/KpAUW8+YhCAeoVgo1Dam+b2xQx
Tnm2HadldBMVj0K74l76XcroW4/IQ84f1Mu1ekq1QFqc7zKAo7UleLKEgdJleVGubVv5K0y+qjpb
qBrVnohqIm80cqH0VzTBiHFctPPsvEQ2shR3rP9HfXXcV/uz1c3N8+tJ3aQHdJpi+gu0ofwz8n37
AbDU8qMHAgAA
headers:
Accept-Ranges:
- bytes
Age:
- '3923'
- '5107'
Allow:
- GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS
Cache-Control:
Expand All @@ -28,16 +32,16 @@ interactions:
Content-Encoding:
- gzip
Content-Length:
- '31'
- '237'
Content-Security-Policy:
- 'default-src https: wss: data: blob: ''unsafe-inline'' ''unsafe-eval''; report-uri
https://seekingalpha.com/report/csp'
Content-Type:
- application/json; charset=utf-8
Date:
- Thu, 23 Mar 2023 17:47:28 GMT
- Thu, 30 Mar 2023 21:58:34 GMT
ETag:
- W/"8fe32e407a1038ee38753b70e5374b3a"
- W/"4dd03aba29105b143df99c7ff7fb6a44"
Referrer-Policy:
- strict-origin-when-cross-origin
Strict-Transport-Security:
Expand All @@ -57,18 +61,18 @@ interactions:
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- BqPmewoVsQSoefwD
- m1C6-dY274ak1vW4
X-Runtime:
- '0.011198'
- '0.015439'
X-Served-By:
- cache-bfi-kbfi7400066-BFI, cache-iad-kiad7000168-IAD
- cache-bfi-kbfi7400066-BFI, cache-vie6324-VIE
X-Timer:
- S1679593648.189319,VS0,VE2
- S1680213515.877602,VS0,VE1
X-XSS-Protection:
- 1; mode=block
set-cookie:
- machine_cookie=2941724868147; expires=Wed, 22 Mar 2028 17:47:28 GMT; path=/;
- machine_cookie_ts=1679593648; expires=Wed, 22 Mar 2028 17:47:28 GMT; path=/;
- machine_cookie=3072632979241; expires=Wed, 29 Mar 2028 21:58:34 GMT; path=/;
- machine_cookie_ts=1680213514; expires=Wed, 29 Mar 2028 21:58:34 GMT; path=/;
status:
code: 200
message: OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ interactions:
response:
body:
string: !!binary |
H4sIAAAAAAAAA6tWSkksSVSyio6tBQCUZLJeCwAAAA==
H4sIAAAAAAAAA63RTUvEMBAG4L9S5lwWwe3K9hYQVLDWj4sgUsZ0aIPppCYTsJT+d5OD7H31mHln
Jg/JCj0KQv22gumhhsOhujheQgmyzJTOhJ4ND51GS9yj78ToT/KpAUW8+YhCAeoVgo1Dam+b2xQx
Tnm2HadldBMVj0K74l76XcroW4/IQ84f1Mu1ekq1QFqc7zKAo7UleLKEgdJleVGubVv5K0y+qjpb
qBrVnohqIm80cqH0VzTBiHFctPPsvEQ2shR3rP9HfXXcV/uz1c3N8+tJ3aQHdJpi+gu0ofwz8n37
AbDU8qMHAgAA
headers:
Accept-Ranges:
- bytes
Age:
- '3936'
- '0'
Allow:
- GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS
Cache-Control:
Expand All @@ -28,16 +32,16 @@ interactions:
Content-Encoding:
- gzip
Content-Length:
- '31'
- '237'
Content-Security-Policy:
- 'default-src https: wss: data: blob: ''unsafe-inline'' ''unsafe-eval''; report-uri
https://seekingalpha.com/report/csp'
Content-Type:
- application/json; charset=utf-8
Date:
- Thu, 23 Mar 2023 17:47:41 GMT
- Fri, 31 Mar 2023 22:38:02 GMT
ETag:
- W/"8fe32e407a1038ee38753b70e5374b3a"
- W/"4dd03aba29105b143df99c7ff7fb6a44"
Referrer-Policy:
- strict-origin-when-cross-origin
Strict-Transport-Security:
Expand All @@ -57,18 +61,97 @@ interactions:
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- BqPmewoVsQSoefwD
- m1C6-dY274ak1vW4
X-Runtime:
- '0.011198'
- '0.015439'
X-Served-By:
- cache-bfi-kbfi7400066-BFI, cache-iad-kiad7000173-IAD
- cache-bfi-kbfi7400066-BFI, cache-vie6341-VIE
X-Timer:
- S1679593661.187593,VS0,VE1
- S1680302282.302888,VS0,VE247
X-XSS-Protection:
- 1; mode=block
set-cookie:
- machine_cookie=4920043943349; expires=Wed, 22 Mar 2028 17:47:41 GMT; path=/;
- machine_cookie_ts=1679593661; expires=Wed, 22 Mar 2028 17:47:41 GMT; path=/;
- machine_cookie=3779683730951; expires=Thu, 30 Mar 2028 22:38:02 GMT; path=/;
- machine_cookie_ts=1680302282; expires=Thu, 30 Mar 2028 22:38:02 GMT; path=/;
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
method: GET
uri: https://seekingalpha.com/api/v3/earnings_calendar/tickers?filter%5Bselected_date%5D=2023-03-23&filter%5Bwith_rating%5D=false&filter%5Bcurrency%5D=USD
response:
body:
string: !!binary |
H4sIAAAAAAAAA6WTW08CMRCF/8qkiW8IZdddFt5WIGoi11XiJYaMuyNuLC22XYMh/He7JAb1wcv6
1jmdyXznpN2wDC2yzu2G5RnrsJC3I5+zGrOvK3I1oZa5XMxTFCQz1HObp0+kXQNaq/P7wpJhnQ0z
oli49ll8PnR3Epfl8AyFpBeEpO80WqePKBelPoyTXjxxmqHUKj0vN/tBjWkShIbcjt34WBl7OED9
RJZtt7V3wiYPeLMy4TgZd/eE5Qo4VSJzHg2MUVtJ2p2hq/RKabS5kl/Yr3duPpDLQoiv7KW2R277
R/8gjuPBB+JY2NyoQqcEsTFkYYASF7Qkaf8CHfLvkYPIa7f8yinHs+lon3JZHZ+NanAm03qVp/A5
zrDJo6hVmW10lUxO9nCj9YPSGSTPBWqCLq5yi2IXJRxCUA/4AQwvYNrv9fsD8JsN3mp43IvgMumB
F/zs5uiHpEMetDzvX25ufuOmDmHdC5wZ5b4sOAthdfa77RtWRQUHOAQAAA==
headers:
Accept-Ranges:
- bytes
Age:
- '0'
Allow:
- GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS
Cache-Control:
- max-age=600, public
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Length:
- '385'
Content-Security-Policy:
- 'default-src https: wss: data: blob: ''unsafe-inline'' ''unsafe-eval''; report-uri
https://seekingalpha.com/report/csp'
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 31 Mar 2023 22:38:03 GMT
ETag:
- W/"46de6036dfaa4722979d2d427cb6cbb6"
Referrer-Policy:
- strict-origin-when-cross-origin
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
Vary:
- User-Agent, Accept-Encoding
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- LyPEy4M6J1RrUsxC
X-Runtime:
- '0.027182'
X-Served-By:
- cache-bfi-krnt7300116-BFI, cache-vie6353-VIE
X-Timer:
- S1680302283.710800,VS0,VE295
X-XSS-Protection:
- 1; mode=block
set-cookie:
- machine_cookie=2903609098146; expires=Thu, 30 Mar 2028 22:38:03 GMT; path=/;
- machine_cookie_ts=1680302283; expires=Thu, 30 Mar 2028 22:38:03 GMT; path=/;
status:
code: 200
message: OK
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
""
,Ticker,Name,Exchange,Release Time,Date
0,OMH,Ohmyhome Pte. Ltd.,NASDAQ,,2023-03-22
1,AMAO,American Acquisition Opportunity Inc.,NASDAQ,,2023-03-22
2,MGRX,"Mangoceuticals, Inc.",NASDAQ,,2023-03-22
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import pytest

# IMPORTATION INTERNAL
from openbb_terminal.core.session.current_user import (
PreferencesModel,
copy_user,
)
from openbb_terminal.core.session.current_user import PreferencesModel, copy_user
from openbb_terminal.stocks.discovery import disc_controller


Expand Down Expand Up @@ -345,9 +342,8 @@ def test_call_func_expect_queue(expected_queue, queue, func):
(
"call_upcoming",
"seeking_alpha_view.upcoming_earning_release_dates",
["--pages=10", "--start=2023-03-22", "--limit=5", "--export=csv"],
["--start=2023-03-22", "--limit=5", "--export=csv"],
{
"num_pages": 10,
"limit": 5,
"start_date": datetime(2023, 3, 22),
"export": "csv",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def vcr_config():

@pytest.mark.vcr
def test_get_next_earnings(recorder):
df_earnings = seeking_alpha_model.get_next_earnings(1, start=date(2023, 3, 22))
df_earnings = seeking_alpha_model.get_next_earnings(1, start_date=date(2023, 3, 22))
recorder.capture(df_earnings)


Expand Down
Loading

0 comments on commit a0c0a99

Please sign in to comment.