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

[BugFix] Fix obb.news.company(provider="yfinance") #7003

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# pylint: disable=unused-argument

import asyncio
from datetime import datetime
from typing import Any, Dict, List, Optional
from typing import Any, Optional

from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.company_news import (
Expand Down Expand Up @@ -34,63 +32,64 @@ def _symbol_mandatory(cls, v):
class YFinanceCompanyNewsData(CompanyNewsData):
"""YFinance Company News Data."""

__alias_dict__ = {
"symbols": "relatedTickers",
"date": "providerPublishTime",
"url": "link",
"images": "thumbnail",
"source": "publisher",
}

source: str = Field(description="Source of the news article")

@field_validator("symbols", mode="before", check_fields=False)
@classmethod
def symbols_string(cls, v):
"""Symbols string validator."""
return ",".join(v)
source: Optional[str] = Field(
default=None, description="Source of the news article"
)


class YFinanceCompanyNewsFetcher(
Fetcher[
YFinanceCompanyNewsQueryParams,
List[YFinanceCompanyNewsData],
list[YFinanceCompanyNewsData],
]
):
"""Transform the query, extract and transform the data from the Yahoo Finance endpoints."""

@staticmethod
def transform_query(params: Dict[str, Any]) -> YFinanceCompanyNewsQueryParams:
def transform_query(params: dict[str, Any]) -> YFinanceCompanyNewsQueryParams:
"""Transform query params."""
return YFinanceCompanyNewsQueryParams(**params)

@staticmethod
async def aextract_data(
query: YFinanceCompanyNewsQueryParams,
credentials: Optional[Dict[str, str]],
credentials: Optional[dict[str, str]],
**kwargs: Any,
) -> List[Dict]:
) -> list[dict]:
"""Extract data."""
from yfinance import Ticker # pylint: disable=import-outside-toplevel
# pylint: disable=import-outside-toplevel
import asyncio # noqa
from yfinance import Ticker

results = []
results: list = []
symbols = query.symbol.split(",") # type: ignore

async def get_one(symbol):
data = Ticker(symbol).get_news()
data = Ticker(symbol).get_news(count=query.limit, tab="all")
for d in data:
images = None
if d.get("thumbnail"):
images = d["thumbnail"].get("resolutions")
_ = d.pop("uuid")
_ = d.pop("type")
d["date"] = datetime.utcfromtimestamp(d["providerPublishTime"])
d["images"] = (
[{k: str(v) for k, v in img.items()} for img in images]
if images
else None
)
results.extend(data)
new_content: dict = {}
content = d.get("content")
if not content:
continue
if thumbnail := content.get("thumbnail"):
images = thumbnail.get("resolutions")
if images:
new_content["images"] = [
{k: str(v) for k, v in img.items()} for img in images
]
new_content["url"] = content.get("canonicalUrl", {}).get("url")
new_content["source"] = content.get("provider", {}).get("displayName")
new_content["title"] = content.get("title")
new_content["date"] = content.get("pubDate")
description = content.get("description")
summary = content.get("summary")

if description:
new_content["text"] = description
elif summary:
new_content["text"] = summary

results.append(new_content)

tasks = [get_one(symbol) for symbol in symbols]

Expand All @@ -101,8 +100,8 @@ async def get_one(symbol):
@staticmethod
def transform_data(
query: YFinanceCompanyNewsQueryParams,
data: List[Dict],
data: list[dict],
**kwargs: Any,
) -> List[YFinanceCompanyNewsData]:
) -> list[YFinanceCompanyNewsData]:
"""Transform data."""
return [YFinanceCompanyNewsData.model_validate(d) for d in data]
Loading
Loading