|
| 1 | +"""AKShare Equity Ownership Model.""" |
| 2 | + |
| 3 | +from datetime import ( |
| 4 | + date as dateType, |
| 5 | + datetime, |
| 6 | +) |
| 7 | +from typing import Any, Dict, List, Optional |
| 8 | + |
| 9 | +from openbb_core.provider.abstract.fetcher import Fetcher |
| 10 | +from openbb_core.provider.standard_models.equity_ownership import ( |
| 11 | + EquityOwnershipData, |
| 12 | + EquityOwnershipQueryParams, |
| 13 | +) |
| 14 | +from mysharelib.tools import most_recent_quarter |
| 15 | +from pydantic import field_validator |
| 16 | + |
| 17 | + |
| 18 | +class AKShareEquityOwnershipQueryParams(EquityOwnershipQueryParams): |
| 19 | + """AKShare Equity Ownership Query. |
| 20 | +
|
| 21 | + Source: https://emweb.securities.eastmoney.com/PC_HSF10/ShareholderResearch/Index?type=web&code=SH688686#sdgd-0 |
| 22 | + """ |
| 23 | + |
| 24 | + @field_validator("date", mode="before", check_fields=True) |
| 25 | + @classmethod |
| 26 | + def time_validate(cls, v: str): |
| 27 | + """Validate the date.""" |
| 28 | + if v is None: |
| 29 | + v = dateType.today() |
| 30 | + if isinstance(v, str): |
| 31 | + base = datetime.strptime(v, "%Y-%m-%d").date() |
| 32 | + return most_recent_quarter(base) |
| 33 | + return most_recent_quarter(v) |
| 34 | + |
| 35 | + |
| 36 | +class AKShareEquityOwnershipData(EquityOwnershipData): |
| 37 | + """AKShare Equity Ownership Data.""" |
| 38 | + |
| 39 | + |
| 40 | +class AKShareEquityOwnershipFetcher( |
| 41 | + Fetcher[ |
| 42 | + AKShareEquityOwnershipQueryParams, |
| 43 | + List[AKShareEquityOwnershipData], |
| 44 | + ] |
| 45 | +): |
| 46 | + """Transform the query, extract and transform the data from the AKShare endpoints.""" |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def transform_query(params: Dict[str, Any]) -> AKShareEquityOwnershipQueryParams: |
| 50 | + """Transform the query params.""" |
| 51 | + return AKShareEquityOwnershipQueryParams(**params) |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def extract_data( |
| 55 | + query: AKShareEquityOwnershipQueryParams, |
| 56 | + credentials: Optional[Dict[str, str]], |
| 57 | + **kwargs: Any, |
| 58 | + ) -> List[Dict]: |
| 59 | + """Return the raw data from the AKShare endpoint.""" |
| 60 | + from openbb_akshare.utils.ak_equity_ownership import stock_gdfx_top_10 |
| 61 | + |
| 62 | + return stock_gdfx_top_10(query.symbol, query.date).to_dict(orient="records") |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def transform_data( |
| 66 | + query: AKShareEquityOwnershipQueryParams, data: List[Dict], **kwargs: Any |
| 67 | + ) -> List[AKShareEquityOwnershipData]: |
| 68 | + """Return the transformed data.""" |
| 69 | + own = [AKShareEquityOwnershipData.model_validate(d) for d in data] |
| 70 | + own.sort(key=lambda x: x.filing_date, reverse=True) |
| 71 | + return own |
0 commit comments