forked from dantebarba/cotizacion-bna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcotizacion_api.py
117 lines (87 loc) · 2.43 KB
/
cotizacion_api.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
from flask import Flask, current_app
from flask import json
from bs4 import BeautifulSoup
import urllib
from flask_cors import CORS, cross_origin
class Currency():
def __init__(self, compra, venta):
self.compra = compra
self.venta = venta
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
class Cotizacion():
def __init__(self, usd=None, eur=None, real=None):
self.usd = usd
self.eur = eur
self.real = real
def to_usd(self):
return self.usd
def to_real(self):
return self.real
def to_eur(self):
return self.eur
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
class BancoNacionParser():
def __init__(self, url):
self.url = url;
def openUrl(self):
response = urllib.urlopen(self.url)
html = response.read()
return html
def retrieve(self):
soup = BeautifulSoup(self.openUrl(), 'html.parser')
cotizacionDolar = []
rows = soup.find(id="billetes").table.tbody.find_all('tr')
for row in rows:
column = row.find_all('td')
cotizacionDolar.append([ele.text.strip() for ele in column])
return Cotizacion(Currency(cotizacionDolar[0][1], cotizacionDolar[0][2]),
Currency(cotizacionDolar[1][1], cotizacionDolar[1][2]),
Currency(cotizacionDolar[2][1], cotizacionDolar[2][2]))
class CotizacionParser():
def __init__(self):
self.strategy = BancoNacionParser("http://www.bna.com.ar/")
def retrieve(self):
return self.strategy.retrieve()
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/")
@cross_origin()
def index():
return current_app.send_static_file('cotizacion.html')
@app.route("/usd")
@cross_origin()
def cotizacion_usd():
cotizacion = CotizacionParser()
response = app.response_class(
response=cotizacion.retrieve().to_usd().toJSON(),
status=200,
mimetype='application/json'
)
return response
@app.route("/eur")
@cross_origin()
def cotizacion_eur():
cotizacion = CotizacionParser()
response = app.response_class(
response=cotizacion.retrieve().to_eur().toJSON(),
status=200,
mimetype='application/json'
)
return response
@app.route("/real")
@cross_origin()
def cotizacion_real():
cotizacion = CotizacionParser()
response = app.response_class(
response=cotizacion.retrieve().to_real().toJSON(),
status=200,
mimetype='application/json'
)
return response
app.run(host='0.0.0.0')
url_for('static', filename='cotizacion.html')