Добавил информацию по курсу рубля
This commit is contained in:
32
cbrcurrency.py
Normal file
32
cbrcurrency.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from cachetools import TTLCache
|
||||||
|
import json
|
||||||
|
|
||||||
|
from session import session
|
||||||
|
|
||||||
|
|
||||||
|
__CBR_DAYLY_URL = "https://www.cbr-xml-daily.ru/daily_json.js"
|
||||||
|
__cache: TTLCache = TTLCache(maxsize=10000, ttl=60 * 10)
|
||||||
|
|
||||||
|
|
||||||
|
def __fetch_currency() -> dict:
|
||||||
|
if __CBR_DAYLY_URL in __cache:
|
||||||
|
return __cache[__CBR_DAYLY_URL]
|
||||||
|
|
||||||
|
resp = session.get(__CBR_DAYLY_URL)
|
||||||
|
data = resp.json()
|
||||||
|
|
||||||
|
__cache[__CBR_DAYLY_URL] = data["Valute"]
|
||||||
|
return data["Valute"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_currency(currency_charcode: str) -> dict:
|
||||||
|
if not currency_charcode in __cache:
|
||||||
|
currencies = __fetch_currency()
|
||||||
|
if currency_charcode in currencies:
|
||||||
|
currency = currencies[currency_charcode]
|
||||||
|
nominal = int(currency["Nominal"])
|
||||||
|
value = float(currency["Value"])
|
||||||
|
|
||||||
|
return {f"1_{currency_charcode}": value / nominal, "CharCode": currency_charcode, "Name": currency["Name"]}
|
||||||
|
else:
|
||||||
|
return {"error": f'Unknow currency charcode {currency_charcode}'}
|
||||||
15
mcp-moex.py
15
mcp-moex.py
@@ -3,8 +3,8 @@ from starlette.routing import Mount
|
|||||||
from mcp.server.fastmcp import FastMCP
|
from mcp.server.fastmcp import FastMCP
|
||||||
from typing import TypedDict, List
|
from typing import TypedDict, List
|
||||||
|
|
||||||
|
|
||||||
from moex import bond_sequrities
|
from moex import bond_sequrities
|
||||||
|
from cbrcurrency import get_currency
|
||||||
|
|
||||||
|
|
||||||
mcp = FastMCP("mcp-moex - Данные с московской биржы.", port=8021)
|
mcp = FastMCP("mcp-moex - Данные с московской биржы.", port=8021)
|
||||||
@@ -22,5 +22,18 @@ def get_bond_securities(bond_ticker: str, board: str) -> dict:
|
|||||||
return bond_sequrities(bond_ticker, board)
|
return bond_sequrities(bond_ticker, board)
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def get_currency_to_rub(currency_charcode: str) -> dict:
|
||||||
|
"""
|
||||||
|
Возвращает курс рубля центробанка России к валюте по ее коду (например, "USD")
|
||||||
|
|
||||||
|
Args:
|
||||||
|
currency_charcode (str): Код валюты (например, "USD", указывается в uppercase).
|
||||||
|
"""
|
||||||
|
return get_currency(currency_charcode)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mcp.run(transport="streamable-http", mount_path="/mcp")
|
mcp.run(transport="streamable-http", mount_path="/mcp")
|
||||||
|
|
||||||
|
|||||||
21
moex.py
21
moex.py
@@ -1,18 +1,25 @@
|
|||||||
import requests
|
|
||||||
import xmltodict
|
import xmltodict
|
||||||
|
from cachetools import TTLCache
|
||||||
|
|
||||||
|
from session import session, MOEX_BASE_URL
|
||||||
|
|
||||||
BASE_URL: str = "https://iss.moex.com/iss"
|
__cache: TTLCache = TTLCache(maxsize=10000, ttl=60 * 60)
|
||||||
|
|
||||||
session = requests.Session()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def bond_sequrities(bond_ticker: str, board: str) -> dict:
|
def bond_sequrities(bond_ticker: str, board: str) -> dict:
|
||||||
res = requests.get(f"{BASE_URL}/engines/stock/markets/BONDS/boards/{board}/securities.xml?securities={bond_ticker}&iss.meta=off&iss.only=securities")
|
url: str = f"{MOEX_BASE_URL}/engines/stock/markets/BONDS/boards/{board}/securities.xml?securities={bond_ticker}&iss.meta=off&iss.only=securities"
|
||||||
|
|
||||||
|
if url in __cache:
|
||||||
|
return __cache[url]
|
||||||
|
|
||||||
|
res = session.get(url)
|
||||||
|
|
||||||
if res.status_code != 200:
|
if res.status_code != 200:
|
||||||
return {"error": True, "status_code": res.status_code}
|
return {"error": True, "status_code": res.status_code}
|
||||||
|
|
||||||
return xmltodict.parse(res.text)["document"]["data"]["rows"]["row"]
|
data = xmltodict.parse(res.text)["document"]["data"]["rows"]["row"]
|
||||||
|
data["@PREVPRICE_PERCENT"] = data.pop("@PREVPRICE")
|
||||||
|
__cache[url] = data
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|||||||
5
session.py
Normal file
5
session.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
session = requests.Session()
|
||||||
|
|
||||||
|
MOEX_BASE_URL: str = "https://iss.moex.com/iss"
|
||||||
Reference in New Issue
Block a user