feat: add FastAPI with BCV + Binance P2P endpoints
Browse files- Dockerfile +12 -0
- main.py +121 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY main.py .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import httpx
|
| 4 |
+
import asyncio
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Tazas API", version="1.0.0")
|
| 8 |
+
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_methods=["GET"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
+
# Cache simple en memoria
|
| 18 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
+
_cache: dict = {}
|
| 20 |
+
BCV_TTL = 60 * 60 * 4 # 4 horas (BCV actualiza una vez al dΓa)
|
| 21 |
+
P2P_TTL = 60 * 20 # 20 minutos
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _cached(key: str, ttl: int):
|
| 25 |
+
entry = _cache.get(key)
|
| 26 |
+
if entry and (time.time() - entry["ts"]) < ttl:
|
| 27 |
+
return entry["data"]
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _store(key: str, data: dict):
|
| 32 |
+
_cache[key] = {"ts": time.time(), "data": data}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
+
# Fetchers
|
| 37 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
+
async def fetch_bcv() -> dict:
|
| 39 |
+
cached = _cached("bcv", BCV_TTL)
|
| 40 |
+
if cached:
|
| 41 |
+
return cached
|
| 42 |
+
|
| 43 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 44 |
+
oficial, paralelo = await asyncio.gather(
|
| 45 |
+
client.get("https://ve.dolarapi.com/v1/dolares/oficial"),
|
| 46 |
+
client.get("https://ve.dolarapi.com/v1/dolares/paralelo"),
|
| 47 |
+
)
|
| 48 |
+
oficial.raise_for_status()
|
| 49 |
+
paralelo.raise_for_status()
|
| 50 |
+
|
| 51 |
+
o = oficial.json()
|
| 52 |
+
p = paralelo.json()
|
| 53 |
+
|
| 54 |
+
data = {
|
| 55 |
+
"bcvUsd": o.get("promedio"),
|
| 56 |
+
"parallelUsd": p.get("promedio"),
|
| 57 |
+
"updatedAt": o.get("fechaActualizacion"),
|
| 58 |
+
}
|
| 59 |
+
_store("bcv", data)
|
| 60 |
+
return data
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
async def fetch_p2p() -> dict:
|
| 64 |
+
cached = _cached("p2p", P2P_TTL)
|
| 65 |
+
if cached:
|
| 66 |
+
return cached
|
| 67 |
+
|
| 68 |
+
headers = {
|
| 69 |
+
"User-Agent": "Mozilla/5.0",
|
| 70 |
+
"Content-Type": "application/json",
|
| 71 |
+
}
|
| 72 |
+
body_buy = {
|
| 73 |
+
"fiat": "VES", "page": 1, "rows": 10,
|
| 74 |
+
"tradeType": "BUY", "asset": "USDT",
|
| 75 |
+
"countries": [], "proMerchantAds": False,
|
| 76 |
+
"shieldMerchantAds": False, "publisherType": None,
|
| 77 |
+
"payTypes": [], "classifies": ["mass", "profession"],
|
| 78 |
+
}
|
| 79 |
+
body_sell = {**body_buy, "tradeType": "SELL"}
|
| 80 |
+
|
| 81 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 82 |
+
buy_res, sell_res = await asyncio.gather(
|
| 83 |
+
client.post("https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search", json=body_buy, headers=headers),
|
| 84 |
+
client.post("https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search", json=body_sell, headers=headers),
|
| 85 |
+
)
|
| 86 |
+
buy_res.raise_for_status()
|
| 87 |
+
sell_res.raise_for_status()
|
| 88 |
+
|
| 89 |
+
buy_data = buy_res.json().get("data", [])
|
| 90 |
+
sell_data = sell_res.json().get("data", [])
|
| 91 |
+
|
| 92 |
+
buy_max = float(buy_data[0]["adv"]["price"]) if buy_data else None
|
| 93 |
+
sell_max = max((float(x["adv"]["price"]) for x in sell_data), default=None)
|
| 94 |
+
|
| 95 |
+
data = {"p2pBuyMax": buy_max, "p2pSellMax": sell_max}
|
| 96 |
+
_store("p2p", data)
|
| 97 |
+
return data
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 101 |
+
# Endpoints
|
| 102 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 103 |
+
@app.get("/")
|
| 104 |
+
async def root():
|
| 105 |
+
return {"status": "ok", "service": "Tazas API"}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
@app.get("/v1/rates")
|
| 109 |
+
async def rates():
|
| 110 |
+
bcv, p2p = await asyncio.gather(fetch_bcv(), fetch_p2p())
|
| 111 |
+
return {**bcv, **p2p}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@app.get("/v1/bcv")
|
| 115 |
+
async def bcv():
|
| 116 |
+
return await fetch_bcv()
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
@app.get("/v1/p2p")
|
| 120 |
+
async def p2p():
|
| 121 |
+
return await fetch_p2p()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.6
|
| 2 |
+
uvicorn==0.34.0
|
| 3 |
+
httpx==0.28.1
|