forked from NazarenoCavazzon/BlueAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
105 lines (88 loc) · 3.15 KB
/
app.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
import os
from flask import Flask, send_from_directory, jsonify
from flask_caching import Cache
from datetime import datetime
VERSION = "1.0"
CACHE_TIMEOUT_SECONDS = os.getenv('CACHE_TIMEOUT', 3600)
GIT_REPO_URL = 'https://github.com/flechajm/BluePy'
DOLAR_URL = 'https://www.paralelohoy.com.ar/p/cotizacion-dolar-hoy-argentina.html'
EURO_URL = 'https://www.paralelohoy.com.ar/p/cotizacion-euro-hoy-argentina.html'
REAL_URL = 'https://www.paralelohoy.com.ar/p/cotizacion-real-hoy-argentina.html'
def getValues(url):
import requests
from bs4 import BeautifulSoup
html_source = requests.get(url).text
soup = BeautifulSoup(html_source, 'lxml')
table = soup.find("table")
span = table.tbody.text
splittedSpan = span.split("\n")
splittedSpan = filter(None, splittedSpan)
list = []
for x in splittedSpan:
value = []
value = x.split(":")[1].split("$")
value.pop(0)
list.append(value)
return list
def formatResponse(value):
return {
"fecha": datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
"compra" : f"{value[0]}",
"venta" : f"{value[1]}"
}
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
@app.route("/favicon.ico")
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.ico')
@app.route("/")
def getRoot():
html = f"""<head>
<title>BluePy API v{VERSION}</title>
<head>
<body>
BluePy API <b>v{VERSION}</b> - <b><a href={GIT_REPO_URL} style="text-decoration: none;">GitHub</a></b>
</body>"""
return html
@app.route("/api/ping")
def ping():
return 'pong'
@app.route("/api/dolar/oficial")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getDolarOficial():
dolarValues = getValues(DOLAR_URL)
dolarOficial = formatResponse(dolarValues[0])
return jsonify(dolarOficial)
@app.route("/api/dolar/blue")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getDolarBlue():
dolarValues = getValues(DOLAR_URL)
dolarBlue = formatResponse(dolarValues[1])
return jsonify(dolarBlue)
@app.route("/api/euro/oficial")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getEuroOficial():
euroValues = getValues(EURO_URL)
euroOficial = formatResponse(euroValues[0])
return jsonify(euroOficial)
@app.route("/api/euro/blue")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getEuroBlue():
euroValues = getValues(EURO_URL)
euroBlue = formatResponse(euroValues[1])
return jsonify(euroBlue)
@app.route("/api/real/oficial")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getRealOficial():
realValues = getValues(REAL_URL)
realOficial = formatResponse(realValues[0])
return jsonify(realOficial)
@app.route("/api/real/blue")
@cache.cached(timeout=CACHE_TIMEOUT_SECONDS)
def getRealBlue():
realValues = getValues(REAL_URL)
realBlue = formatResponse(realValues[1])
return jsonify(realBlue)
if __name__ == '__main__':
app.run(debug=False, port=os.getenv('PORT', 5000))