diff --git a/README.md b/README.md index 11ad1e6..d3cc81e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ python3 -m venv env # if venv module is not installed install with `sudo apt install python3-venv` source env/bin/activate pip install -r requirements.txt +playwright install crontab -e (add coti.py) // See example below python cotiapp.py ``` @@ -55,6 +56,7 @@ virtualenv -p python3 env # activate the virtualenv: source env/bin/activate pip3 install -r requirements.txt +playwright install # and the run # if you need to deactivate the virtualenv: deactivate @@ -101,6 +103,8 @@ And for tweet the exchange rate at 8am, 12pm and 6pm on weekdays * Lucas Ginard https://github.com/LucasGinard * Padaleiana https://github.com/padaleiana * nestorcde https://github.com/nestorcde +* Lucas Martinez https://github.com/martinezlucas98 + ## TODO(APIv2) diff --git a/coti.py b/coti.py index 645dc3a..c33e455 100644 --- a/coti.py +++ b/coti.py @@ -7,9 +7,13 @@ from decimal import Decimal from bs4 import BeautifulSoup from datetime import datetime +from playwright.sync_api import sync_playwright urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) +# Global variables +conti_xhr_val = {} + def decimal_default(obj): if isinstance(obj, Decimal): return float(obj) @@ -366,6 +370,48 @@ def mundial(): compra, venta = 0, 0 return Decimal(compra), Decimal(venta) +def conti_xhr(response): + global conti_xhr_val + try: + if (response.url == "https://apibanking-gw.bancontinental.com.py/divisas/v1/api/monedas/cotizaciones"): + monedas = response.json() + for m in monedas: + if m['divisa'] == 'DOLAR CHQ./TRANSF.': + conti_xhr_val['chq_transf']={ + 'compra': m['compra'], + 'venta': m['venta'] + } + elif m['divisa'] == 'DOLAR EFECTIVO': + conti_xhr_val['efectivo']={ + 'compra': m['compra'], + 'venta': m['venta'] + } + except: + pass + +def conti(intentos=2): + global conti_xhr_val + compra = venta = compra_chq = venta_chq = 0 + if intentos>0: + with sync_playwright() as p: + url = "http://www.bancontinental.com.py/#/" + browser = p.chromium.launch() + page = browser.new_page() + + page.on("response", lambda response: conti_xhr(response)) + page.goto(url, wait_until="networkidle", timeout=90000) + + page.context.close() + browser.close() + try: + compra = conti_xhr_val['efectivo']['compra'] + venta = conti_xhr_val['efectivo']['venta'] + compra_chq = conti_xhr_val['chq_transf']['compra'] + venta_chq = conti_xhr_val['chq_transf']['venta'] + except: + return conti(intentos-1) + return Decimal(compra), Decimal(venta), Decimal(compra_chq), Decimal(venta_chq) + def create_json(): mcompra, mventa = maxi() @@ -383,6 +429,7 @@ def create_json(): visioncompra, visionventa = vision() bonanzacompra, bonanzaventa = bonanza() lamonedacompra, lamonedaventa = lamoneda() + conticompra, contiventa, conticomprachq, contiventachq = conti() respjson = { "dolarpy": { @@ -416,6 +463,14 @@ def create_json(): 'lamoneda': { 'compra': lamonedacompra, 'venta': lamonedaventa, + }, + 'contichq': { + 'compra': conticomprachq, + 'venta': contiventachq, + }, + 'conti': { + 'compra': conticompra, + 'venta': contiventa, } }, "updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), diff --git a/requirements.txt b/requirements.txt index 24ba256..df6b872 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ gunicorn==19.10.0 flask-cors==3.0.9 requests>=2.20.0 Mastodon.py==1.6.3 +playwright==1.29.1 \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 95f2b98..e14c75e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -408,6 +408,57 @@