-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
272 lines (207 loc) · 8.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json
import os
from functools import lru_cache
import httpx
from bs4 import BeautifulSoup
import requests
from fastapi import FastAPI, HTTPException, Depends
import redis
from dotenv import load_dotenv
from fastapi.middleware.cors import CORSMiddleware
load_dotenv()
app = FastAPI(
title="ArgentoFX",
description=(
"API RESTful para obtener cotizaciones de divisas extranjeras en vivo, en Argentina.\n\n"
"##### Ver en [Docker Hub](https://hub.docker.com/r/maxcomperatore/argentofx).\n\n"
"##### Ver en [GitHub](https://github.com/pyoneerC/argentofx)."
),
version="4.0.0",
openapi_tags=[
{"name": "usd", "description": "Cotizaciones del dólar"},
{"name": "otros", "description": "Cotizaciones de otras divisas"},
{"name": "status", "description": "Estado del servidor"},
],
contact={
"name": "Max Comperatore",
"url": "https://maxcomperatore.com",
"email": "[email protected]"
},
license_info={
"name": "Unlicense",
"url": "https://unlicense.org"
}
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
r = redis.Redis(
host=os.getenv("REDIS_HOST"),
password=os.getenv("REDIS_PASSWORD"),
port=int(os.getenv("REDIS_PORT")),
ssl=True
)
def extract_value(element, type_value):
try:
value_text = element.text.replace(",", ".").replace("$", "").strip()
value_text = "".join(filter(lambda x: x.isdigit() or x == ".", value_text))
return float(value_text)
except ValueError as e:
raise ValueError(f"Failed to convert {type_value} value to float: {e}")
@lru_cache(maxsize=7)
def scrape_currency_website(currency_type, venta_index, compra_index):
main_url = "https://dolar-arg-app.netlify.app"
cache_value = r.get(currency_type.lower())
if cache_value:
return json.loads(cache_value)
try:
response = requests.get(main_url)
if response.status_code != 200:
raise HTTPException(
status_code=404,
detail=f"Failed to fetch the webpage: Status code {response.status_code}",
)
soup = BeautifulSoup(response.content, "html.parser")
venta_elements = soup.find_all("div", class_="text-right")
compra_elements = soup.find_all(
"div", class_=lambda value: value == "" or value is None
)
if len(venta_elements) <= venta_index or len(compra_elements) <= compra_index:
raise HTTPException(
status_code=404,
detail="Required elements not found or index out of range",
)
venta_value = extract_value(venta_elements[venta_index], "Venta")
compra_value = extract_value(compra_elements[compra_index], "Compra")
promedio = (compra_value + venta_value) / 2
result = {
"currency": currency_type,
"compra": f"{compra_value:.2f} ARS",
"venta": f"{venta_value:.2f} ARS",
"promedio": f"{promedio:.2f} ARS",
"spread": f"{venta_value - compra_value:.2f} ARS",
}
r.setex(currency_type.lower(), 6000, json.dumps(result))
return result
except requests.RequestException as e:
raise HTTPException(status_code=500, detail=f"Request error: {e}")
except ValueError as e:
raise HTTPException(status_code=500, detail=f"Value error: {e}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal Server Error: {e}")
@lru_cache(maxsize=4)
def scrape_dolar_hoy(category):
url = f"https://dolarhoy.com/cotizacion-{category}"
cache_value = r.get(category)
if cache_value:
return eval(cache_value)
try:
response = requests.get(url)
if response.status_code != 200:
raise HTTPException(
status_code=404, detail="Failed to fetch the webpage"
)
soup = BeautifulSoup(response.content, "html.parser")
values = soup.find_all("div", class_="value")
if len(values) < 2:
raise HTTPException(
status_code=404, detail="Required elements not found"
)
compra_text = values[0].text.replace(",", ".").replace("$", "").strip()
venta_text = values[1].text.replace(",", ".").replace("$", "").strip()
compra = float(compra_text)
venta = float(venta_text)
promedio = (compra + venta) / 2
result = {
"currency": category.capitalize(),
"compra": f"{compra:.2f} ARS",
"venta": f"{venta:.2f} ARS",
"promedio": f"{promedio:.2f} ARS",
"spread": f"{venta - compra:.2f} ARS",
}
r.setex(category, 6000, str(result))
return result
except requests.RequestException as e:
raise HTTPException(status_code=500, detail=f"Request error: {e}")
except ValueError as e:
raise HTTPException(status_code=500, detail=f"Value error: {e}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal Server Error: {e}")
@app.get("/blue", tags=["usd"], summary="Cotización del dólar blue")
async def scrape_blue():
return scrape_currency_website("blue", 1, 2)
@app.get("/oficial", tags=["usd"], summary="Cotización del dólar oficial")
async def scrape_oficial():
return scrape_currency_website("oficial", 0, 1)
@app.get("/mep", tags=["usd"], summary="Cotización del dólar MEP")
async def scrape_mep():
return scrape_currency_website("MEP", 2, 3)
@app.get("/ccl", tags=["usd"], summary="Cotización del dólar CCL")
async def scrape_ccl():
return scrape_currency_website("CCL", 3, 4)
@app.get("/mayorista", tags=["usd"], summary="Cotización del dólar mayorista")
async def scrape_mayorista():
return scrape_currency_website("Mayorista", 4, 5)
@app.get("/cripto", tags=["usd"], summary="Cotización del dólar cripto")
async def scrape_cripto():
return scrape_currency_website("Cripto", 5, 6)
@app.get("/tarjeta", tags=["usd"], summary="Cotización del dólar tarjeta")
async def scrape_tarjeta():
return scrape_currency_website("Tarjeta", 6, 7)
@app.get("/usd", tags=["usd"], summary="Cotizaciones del dólar")
async def scrape_usd():
cache_value = r.get("usd")
if cache_value:
return eval(cache_value)
result = {
"blue": await scrape_blue(),
"oficial": await scrape_oficial(),
"MEP": await scrape_mep(),
"CCL": await scrape_ccl(),
"Mayorista": await scrape_mayorista(),
"Cripto": await scrape_cripto(),
"Tarjeta": await scrape_tarjeta(),
}
r.setex("usd", 6000, str(result))
return result
@app.get("/euro", tags=["otros"], summary="Cotización del euro")
async def scrape_euro():
return scrape_dolar_hoy("euro")
@app.get("/real", tags=["otros"], summary="Cotización del real")
async def scrape_real():
return scrape_dolar_hoy("real-brasileno")
@app.get("/clp", tags=["otros"], summary="Cotización del peso chileno")
async def scrape_chilenos():
return scrape_dolar_hoy("peso-chileno")
@app.get("/uru", tags=["otros"], summary="Cotización del peso uruguayo")
async def scrape_uruguayos():
return scrape_dolar_hoy("peso-uruguayo")
@app.get("/cotizaciones", tags=["otros"], summary="Cotizaciones de otras divisas")
async def scrape_all():
cache_value = r.get("cotizaciones")
if cache_value:
return eval(cache_value)
result = {
"usd": await scrape_usd(),
"euro": await scrape_euro(),
"real": await scrape_real(),
"clp": await scrape_chilenos(),
"uru": await scrape_uruguayos(),
}
r.setex("cotizaciones", 6000, str(result))
return result
@app.get("/", tags=["status"], summary="Estado del servidor")
async def status():
try:
async with httpx.AsyncClient() as client:
response = await client.get("https://example.com")
if response.status_code == 200:
return {"status": "OK"}
else:
return {"status": "DOWN"}
except Exception as e:
return {"status": "DOWN ", "error": str(e)}