langchain/libs/community/langchain_community/utilities/polygon.py
Bagatur a0c2281540
infra: update mypy 1.10, ruff 0.5 (#23721)
```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path

import toml
import subprocess
import re

ROOT_DIR = Path(__file__).parents[1]


def main():
    for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
        print(path)
        with open(path, "rb") as f:
            pyproject = tomllib.load(f)
        try:
            pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
                "^1.10"
            )
            pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
                "^0.5"
            )
        except KeyError:
            continue
        with open(path, "w") as f:
            toml.dump(pyproject, f)
        cwd = "/".join(path.split("/")[:-1])
        completed = subprocess.run(
            "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )
        logs = completed.stdout.split("\n")

        to_ignore = {}
        for l in logs:
            if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
                path, line_no, error_type = re.match(
                    "^(.*)\:(\d+)\: error:.*\[(.*)\]", l
                ).groups()
                if (path, line_no) in to_ignore:
                    to_ignore[(path, line_no)].append(error_type)
                else:
                    to_ignore[(path, line_no)] = [error_type]
        print(len(to_ignore))
        for (error_path, line_no), error_types in to_ignore.items():
            all_errors = ", ".join(error_types)
            full_path = f"{cwd}/{error_path}"
            try:
                with open(full_path, "r") as f:
                    file_lines = f.readlines()
            except FileNotFoundError:
                continue
            file_lines[int(line_no) - 1] = (
                file_lines[int(line_no) - 1][:-1] + f"  # type: ignore[{all_errors}]\n"
            )
            with open(full_path, "w") as f:
                f.write("".join(file_lines))

        subprocess.run(
            "poetry run ruff format .; poetry run ruff --select I --fix .",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )


if __name__ == "__main__":
    main()

```
2024-07-03 10:33:27 -07:00

134 lines
4.2 KiB
Python

"""
Util that calls several of Polygon's stock market REST APIs.
Docs: https://polygon.io/docs/stocks/getting-started
"""
import json
from typing import Any, Dict, Optional
import requests
from langchain_core.pydantic_v1 import BaseModel, root_validator
from langchain_core.utils import get_from_dict_or_env
POLYGON_BASE_URL = "https://api.polygon.io/"
class PolygonAPIWrapper(BaseModel):
"""Wrapper for Polygon API."""
polygon_api_key: Optional[str] = None
@root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key in environment."""
polygon_api_key = get_from_dict_or_env(
values, "polygon_api_key", "POLYGON_API_KEY"
)
values["polygon_api_key"] = polygon_api_key
return values
def get_financials(self, ticker: str) -> Optional[dict]:
"""
Get fundamental financial data, which is found in balance sheets,
income statements, and cash flow statements for a given ticker.
/vX/reference/financials
"""
url = (
f"{POLYGON_BASE_URL}vX/reference/financials?"
f"ticker={ticker}&"
f"apiKey={self.polygon_api_key}"
)
response = requests.get(url)
data = response.json()
status = data.get("status", None)
if status != "OK":
raise ValueError(f"API Error: {data}")
return data.get("results", None)
def get_last_quote(self, ticker: str) -> Optional[dict]:
"""
Get the most recent National Best Bid and Offer (Quote) for a ticker.
/v2/last/nbbo/{ticker}
"""
url = f"{POLYGON_BASE_URL}v2/last/nbbo/{ticker}?apiKey={self.polygon_api_key}"
response = requests.get(url)
data = response.json()
status = data.get("status", None)
if status != "OK":
raise ValueError(f"API Error: {data}")
return data.get("results", None)
def get_ticker_news(self, ticker: str) -> Optional[dict]:
"""
Get the most recent news articles relating to a stock ticker symbol,
including a summary of the article and a link to the original source.
/v2/reference/news
"""
url = (
f"{POLYGON_BASE_URL}v2/reference/news?"
f"ticker={ticker}&"
f"apiKey={self.polygon_api_key}"
)
response = requests.get(url)
data = response.json()
status = data.get("status", None)
if status != "OK":
raise ValueError(f"API Error: {data}")
return data.get("results", None)
def get_aggregates(self, ticker: str, **kwargs: Any) -> Optional[dict]:
"""
Get aggregate bars for a stock over a given date range
in custom time window sizes.
/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_date}/{to_date}
"""
timespan = kwargs.get("timespan", "day")
multiplier = kwargs.get("timespan_multiplier", 1)
from_date = kwargs.get("from_date", None)
to_date = kwargs.get("to_date", None)
adjusted = kwargs.get("adjusted", True)
sort = kwargs.get("sort", "asc")
url = (
f"{POLYGON_BASE_URL}v2/aggs"
f"/ticker/{ticker}"
f"/range/{multiplier}"
f"/{timespan}"
f"/{from_date}"
f"/{to_date}"
f"?apiKey={self.polygon_api_key}"
f"&adjusted={adjusted}"
f"&sort={sort}"
)
response = requests.get(url)
data = response.json()
status = data.get("status", None)
if status != "OK":
raise ValueError(f"API Error: {data}")
return data.get("results", None)
def run(self, mode: str, ticker: str, **kwargs: Any) -> str:
if mode == "get_financials":
return json.dumps(self.get_financials(ticker))
elif mode == "get_last_quote":
return json.dumps(self.get_last_quote(ticker))
elif mode == "get_ticker_news":
return json.dumps(self.get_ticker_news(ticker))
elif mode == "get_aggregates":
return json.dumps(self.get_aggregates(ticker, **kwargs))
else:
raise ValueError(f"Invalid mode {mode} for Polygon API.")