Skip to content

Commit

Permalink
merge branch develop
Browse files Browse the repository at this point in the history
  • Loading branch information
deeleeramone committed Jan 14, 2025
2 parents 060fe9e + 58b252c commit e88e956
Show file tree
Hide file tree
Showing 6 changed files with 1,308 additions and 681 deletions.
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,44 +32,33 @@ 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."""
# pylint: disable=import-outside-toplevel
import asyncio # noqa
from openbb_core.provider.utils.errors import EmptyDataError
from openbb_core.provider.utils.helpers import get_requests_session
from yfinance import Ticker
Expand All @@ -81,25 +68,35 @@ async def aextract_data(
session = get_requests_session()

async def get_one(symbol):
"""Get the data for one ticker symbol."""
data = Ticker(
symbol,
session=session,
data = Ticker(symbol, session=session).get_news(
count=query.limit,
tab="all",
proxy=session.proxies if session.proxies else None,
).get_news()
)
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 @@ -113,8 +110,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

0 comments on commit e88e956

Please sign in to comment.