Skip to content

Commit

Permalink
[BugFix] obb.news.world Add Validators To Providers Where Symbol Is…
Browse files Browse the repository at this point in the history
… Mandatory (#6625)

* add validators to enforce mandatory symbols in providers where required

* linter

* fmp 3.9 test cassette

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
  • Loading branch information
deeleeramone and IgorWounds authored Aug 17, 2024
1 parent b2ab84f commit 07d8232
Show file tree
Hide file tree
Showing 8 changed files with 16,473 additions and 136 deletions.
18 changes: 15 additions & 3 deletions openbb_platform/openbb/assets/reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -35717,15 +35717,24 @@
"description": "The source of the news article.",
"default": null,
"optional": true,
"choices": null
"choices": [
"yahoo",
"moody",
"moody_us_news",
"moody_us_press_releases"
]
},
{
"name": "sentiment",
"type": "Literal['positive', 'neutral', 'negative']",
"description": "Return news only from this source.",
"default": null,
"optional": true,
"choices": null
"choices": [
"positive",
"neutral",
"negative"
]
},
{
"name": "language",
Expand Down Expand Up @@ -35791,7 +35800,10 @@
"description": "Sort order of the articles.",
"default": "desc",
"optional": true,
"choices": null
"choices": [
"asc",
"desc"
]
}
],
"tiingo": [
Expand Down
27 changes: 25 additions & 2 deletions openbb_platform/openbb/package/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def company(
Number of seconds since the news was published. (provider: benzinga)
sort : Literal['id', 'created', 'updated']
Key to sort the news by. (provider: benzinga)
order : Optional[Literal['asc', 'desc']]
order : Literal['asc', 'desc']
Order to sort the news by. (provider: benzinga);
Sort order of the articles. (provider: polygon)
isin : Optional[str]
Expand Down Expand Up @@ -240,7 +240,30 @@ def company(
"polygon": {"multiple_items_allowed": True, "choices": None},
"tiingo": {"multiple_items_allowed": True, "choices": None},
"yfinance": {"multiple_items_allowed": True, "choices": None},
}
},
"order": {
"polygon": {
"multiple_items_allowed": False,
"choices": ["asc", "desc"],
}
},
"source": {
"intrinio": {
"multiple_items_allowed": False,
"choices": [
"yahoo",
"moody",
"moody_us_news",
"moody_us_press_releases",
],
}
},
"sentiment": {
"intrinio": {
"multiple_items_allowed": False,
"choices": ["positive", "neutral", "negative"],
}
},
},
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class FMPCompanyNewsQueryParams(CompanyNewsQueryParams):
description="Page number of the results. Use in combination with limit.",
)

@field_validator("symbol", mode="before", check_fields=False)
@classmethod
def _symbol_mandatory(cls, v):
"""Symbol mandatory validator."""
if not v:
raise ValueError("Required field missing -> symbol")
return v


class FMPCompanyNewsData(CompanyNewsData):
"""FMP Company News Data."""
Expand Down
43 changes: 29 additions & 14 deletions openbb_platform/providers/fmp/openbb_fmp/models/world_news.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
"""FMP World News Model."""

import math
import warnings
# pylint: disable=unused-argument

from datetime import datetime
from typing import Any, Dict, List, Optional
from warnings import warn

from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.world_news import (
WorldNewsData,
WorldNewsQueryParams,
)
from openbb_core.provider.utils.helpers import amake_requests
from openbb_core.provider.utils.errors import EmptyDataError
from pydantic import Field, field_validator

_warn = warnings.warn


class FMPWorldNewsQueryParams(WorldNewsQueryParams):
"""FMP World News Query.
Expand All @@ -31,16 +30,18 @@ class FMPWorldNewsData(WorldNewsData):
site: str = Field(description="News source.")

@field_validator("date", mode="before", check_fields=False)
def date_validate(cls, v): # pylint: disable=E0213
@classmethod
def date_validate(cls, v):
"""Return the date as a datetime object."""
return datetime.strptime(v, "%Y-%m-%dT%H:%M:%S.%fZ")

@field_validator("images", mode="before", check_fields=False)
def images_validate(cls, v): # pylint: disable=E0213
@classmethod
def images_validate(cls, v):
"""Conform the response to a list."""
if isinstance(v, str):
return [{"o": v}]
return v if isinstance(v, List[Dict]) else None
return v if isinstance(v, list) else None


class FMPWorldNewsFetcher(
Expand All @@ -55,7 +56,7 @@ class FMPWorldNewsFetcher(
def transform_query(params: Dict[str, Any]) -> FMPWorldNewsQueryParams:
"""Transform the query params."""
if params.get("start_date") or params.get("end_date"):
_warn("start_date and end_date are not supported for this endpoint.")
warn("start_date and end_date are not supported for this endpoint.")
return FMPWorldNewsQueryParams(**params)

@staticmethod
Expand All @@ -65,21 +66,35 @@ async def aextract_data(
**kwargs: Any,
) -> List[Dict]:
"""Return the raw data from the FMP endpoint."""
# pylint: disable=import-outside-toplevel
import math # noqa
import asyncio
from openbb_core.provider.utils.helpers import amake_request
from openbb_fmp.utils.helpers import response_callback

api_key = credentials.get("fmp_api_key") if credentials else ""
pages = math.ceil(query.limit / 20)

base_url = "https://financialmodelingprep.com/api/v4"
results: List = []

async def get_one(url):
"""Get data for one URL."""
data = await amake_request(url, response_callback=response_callback)
if data:
results.extend(data)

urls = [
f"{base_url}/general_news?page={page}&apikey={api_key}"
for page in range(pages)
]
response = await amake_requests(urls, **kwargs)
data = sorted(response, key=lambda x: x["publishedDate"], reverse=True)
await asyncio.gather(*[get_one(url) for url in urls])

if results:
data = sorted(results, key=lambda x: x["publishedDate"], reverse=True)

return data[: query.limit]
return data[: query.limit]
raise EmptyDataError("The request was returned empty.")

# pylint: disable=unused-argument
@staticmethod
def transform_data(
query: FMPWorldNewsQueryParams, data: List[Dict], **kwargs: Any
Expand Down
Loading

0 comments on commit 07d8232

Please sign in to comment.