Skip to content

Commit

Permalink
News API Support (#334)
Browse files Browse the repository at this point in the history
* feat: news api

* linting

* fix typo
  • Loading branch information
haxdds authored Aug 18, 2023
1 parent cf2e702 commit 80799b0
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
7 changes: 7 additions & 0 deletions alpaca/data/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,10 @@ class MarketType(str, Enum):

STOCKS = "stocks"
CRYPTO = "crypto"


class NewsImageSize(str, Enum):

THUMB = "thumb"
SMALL = "small"
LARGE = "large"
65 changes: 65 additions & 0 deletions alpaca/data/historical/news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import Optional, Union

from alpaca.common.rest import RESTClient

from alpaca.common.enums import BaseURL

from alpaca.data.requests import NewsRequest

from alpaca.data.models.news import NewsSet

from alpaca.common.types import RawData


class NewsClient(RESTClient):
"""
The REST client for interacting with Alpaca News API endpoints.
"""

def __init__(
self,
api_key: Optional[str] = None,
secret_key: Optional[str] = None,
oauth_token: Optional[str] = None,
use_basic_auth: bool = False,
raw_data: bool = False,
url_override: Optional[str] = None,
) -> None:
"""
Instantiates a Historical Data Client.
Args:
api_key (Optional[str], optional): Alpaca API key. Defaults to None.
secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers.
raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
methods. Defaults to False. This has not been implemented yet.
url_override (Optional[str], optional): If specified allows you to override the base url the client points
to for proxy/testing.
"""
super().__init__(
api_key=api_key,
secret_key=secret_key,
oauth_token=oauth_token,
use_basic_auth=use_basic_auth,
api_version="v1beta1",
base_url=url_override if url_override is not None else BaseURL.DATA,
sandbox=False,
raw_data=raw_data,
)

def get_news(self, request_params: NewsRequest) -> Union[RawData, NewsSet]:
"""Returns news data
Args:
request_params (NewsRequest): The request params to filter the news data"""
response = self.get(
path="/news",
data=request_params.to_request_fields(),
)
if self._use_raw_data:
return response

return NewsSet(**response)
62 changes: 62 additions & 0 deletions alpaca/data/models/news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from datetime import datetime
from typing import Optional, List

from alpaca.common.models import ValidateBaseModel as BaseModel
from alpaca.data import NewsImageSize


class NewsImage(BaseModel):
"""
images (URLs) related to given article
Attributes:
size (NewsImageSize): Possible values for size are thumb, small and large.
url (str): url to image from news article.
"""

size: NewsImageSize
url: str


class News(BaseModel):
"""
images (URLs) related to given article
Attributes:
id (str): News article ID
headline (str): Headline or title of the article
author (str): Original author of news article
created_at (datetime): Date article was created (RFC 3339)
updated_at (datetime): Date article was updated (RFC 3339)
summary (str): Summary text for the article (may be first sentence of content)
content (str): Content of the news article (might contain HTML)
url (Optional[str]): URL of article (if applicable)
images (List[NewsImage]): List of images (URLs) related to given article (may be empty)
symbols (str): List of related or mentioned symbols
source (str): Source where the news originated from (e.g. Benzinga)
"""

id: float
headline: str
author: str
created_at: datetime
updated_at: datetime
summary: str
content: str
url: Optional[str]
images: List[NewsImage]
symbols: List[str]
source: str


class NewsSet(BaseModel):
"""
images (URLs) related to given article
Attributes:
news (List[News]): Array of news objects
next_page_token (Optional[str]): Pagination token for next page
"""

news: List[News]
next_page_token: Optional[str]
28 changes: 28 additions & 0 deletions alpaca/data/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,31 @@ class MarketMoversRequest(ScreenerRequest):
"""

market_type: MarketType = MarketType.STOCKS


class NewsRequest(NonEmptyRequest):
"""
This request class is used to submit a request for most actives screener endpoint.
Attributes:
start (Optional[datetime]): The inclusive start of the interval. Format: RFC-3339 or YYYY-MM-DD.
If missing, the default value is the beginning of the current day.
end (Optional[datetime])): The inclusive end of the interval. Format: RFC-3339 or YYYY-MM-DD.
If missing, the default value is the current time.
sort (Optional[str]): Sort articles by updated date.
symbols (Optional[str]): The comma-separated list of symbols to query news for.
limit (Optional[int]): Limit of news items to be returned for given page.
include_content (Optional[bool]): Boolean indicator to include content for news articles (if available)
exclude_contentless (Optional[bool]): Boolean indicator to exclude news articles that do not contain content
page_token (Optional[str]): Pagination token to continue from. The value to pass here is returned in specific requests
when more data is available than the request limit allows.
"""

start: Optional[datetime] = None
end: Optional[datetime] = None
sort: Optional[str] = None
symbols: Optional[str] = None
limit: Optional[int] = None
include_content: Optional[bool] = None
exclude_contentless: Optional[bool] = None
page_token: Optional[str] = None

0 comments on commit 80799b0

Please sign in to comment.