From 86170aef3288d467387864d704d1deca72799f0b Mon Sep 17 00:00:00 2001 From: Gianluca Romito Date: Wed, 26 Jan 2022 07:39:17 +0100 Subject: [PATCH] prima versione --- .gitignore | 155 +++++++++++++++++++++++ agsm.py | 318 +++++++++++++++++++++++++++++++++++++++++++++++ config-orig.py | 16 +++ requirements.txt | Bin 0 -> 728 bytes 4 files changed, 489 insertions(+) create mode 100644 .gitignore create mode 100644 agsm.py create mode 100644 config-orig.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb520b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,155 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +config.py +bollette.csv \ No newline at end of file diff --git a/agsm.py b/agsm.py new file mode 100644 index 0000000..5b936f0 --- /dev/null +++ b/agsm.py @@ -0,0 +1,318 @@ +import requests +import re +import csv +import fitz +import re +import pysftp +import os +import time +import paho.mqtt.client as paho +from selenium import webdriver +import subprocess +import platform +from pyzabbix import ZabbixMetric, ZabbixSender +import logging +import logging.handlers +from pathlib import Path +import config + +# python -m venv env +# attivare ambiente +# pip install -r requirements.txt + +#TODO aggiungere autocreazione di bollette.csv se non esiste, con questi campi: data,fattura,luogo,gas,elettricità,totale + + +versione = '0.1' +#logging, max 2M a file e ne tengo solo 5 +LOG_FILENAME = Path('agsm.log') +logger = logging.getLogger('agsmlog') +logger.setLevel(logging.INFO) +formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') +handler = logging.handlers.RotatingFileHandler( + LOG_FILENAME, maxBytes=2097152, backupCount=5) +handler.setFormatter(formatter) +logger.addHandler(handler) + + + +mqttbroker=config.mqttbroker +mqttport=config.mqttport + +login = config.login +passwd = config.passwd + +loginacq = config.loginacq +passwdacq = config.passwdacq + + +loginds = config.loginds +passds = config.passds + +diskstation = config.diskstation + +url = 'https://countbox.agsm.it/' + + + +cnopts = pysftp.CnOpts() +cnopts.hostkeys = None + +def bollettaGasEnergia(dati): + r = re.compile('Fattura n° *.*') + data = ([x.split()[-1] for x in r.findall(dati)]) #data fattura + fattura = ([x.split()[-3] for x in r.findall(dati)]) #n. fattura + data=data[0] + fattura=fattura[0] + + r = re.compile('energia in *.*') + dove = ([x.split()[4:5] for x in r.findall(dati)]) + dove=dove[0][0] + + r = re.compile('TV: € [0-9]+,[0-9]+') + elettricita=([x.split()[-1] for x in r.findall(dati)]) + elettricita = elettricita[0] + elettricita = elettricita.replace(',','.') + + client1= paho.Client("control1") #create client object + client1.on_publish = on_publish #assign function to callback + client1.connect(mqttbroker,mqttport) #establish connection + client1.publish('bollette/'+ dove.lower() +'/elettricita', elettricita, qos=0,retain=True) #publish + + + r = re.compile('NATURALE: € [0-9]+,[0-9]+') + gas=([x.split()[-1] for x in r.findall(dati)]) + gas = gas[0] + gas = gas.replace(',','.') + + client1= paho.Client("control1") #create client object + client1.on_publish = on_publish #assign function to callback + client1.connect(mqttbroker,mqttport) #establish connection + client1.publish('bollette/'+ dove.lower() +'/gas', gas, qos=0,retain=True) #publish + + + r = re.compile('TOTALE DA PAGARE: euro [0-9]+,[0-9]+') + tot=([x.split()[-1] for x in r.findall(dati)]) + tot = tot[0] + tot = tot.replace(',','.') + + print('Lettura data: {} n: {} dove: {} importo gas: {}€ importo elettricità: {}€ Totale: {}€'.format(data,fattura,dove,gas,elettricita,tot)) + + f = open("bollette.csv", "a") + f.write(data + ',' + fattura + ',' + dove.lower() + ',' + gas + ',' + elettricita + ',' + tot + '\n') + f.close() + +def bollettaGas(dati): + r = re.compile('Fattura n° *.*') + data = ([x.split()[-1] for x in r.findall(dati)]) #data fattura + fattura = ([x.split()[-3] for x in r.findall(dati)]) #n. fattura + data=data[0] + fattura=fattura[0] + + r = re.compile('fornendo gas in *.*') + dove = ([x.split()[4:5] for x in r.findall(dati)]) + dove=dove[0][0] + + r = re.compile('TOTALE DA PAGARE: euro [0-9]+,[0-9]+') + gas=([x.split()[-1] for x in r.findall(dati)]) + gas = gas[0] + gas = gas.replace(',','.') + + client1= paho.Client("control1") #create client object + client1.on_publish = on_publish #assign function to callback + client1.connect(mqttbroker,mqttport) #establish connection + client1.publish('bollette/'+ dove.lower() +'/gas', gas, qos=0,retain=True) #publish + + + print('Lettura data: {} n: {} dove: {} importo gas: {}€ '.format(data,fattura,dove,gas)) + f = open("bollette.csv", "a") + f.write(data + ',' + fattura + ',' + dove + ',' + gas + ',,' + gas + '\n') + f.close() + +def bollettaElettrica(dati): + r = re.compile('Fattura n° *.*') + data = ([x.split()[-1] for x in r.findall(dati)]) #data fattura + fattura = ([x.split()[-3] for x in r.findall(dati)]) #n. fattura + data=data[0] + fattura=fattura[0] + + r = re.compile('fornendo energia in *.*') + dove = ([x.split()[4:5] for x in r.findall(dati)]) + dove=dove[0][0] + + r = re.compile('TOTALE DA PAGARE: euro [0-9]+,[0-9]+') + elettricita=([x.split()[-1] for x in r.findall(dati)]) + elettricita = elettricita[0] + elettricita = elettricita.replace(',','.') + + client1= paho.Client("control1") #create client object + client1.on_publish = on_publish #assign function to callback + client1.connect(mqttbroker,mqttport) #establish connection + client1.publish('bollette/'+ dove.lower() +'/elettricita', elettricita, qos=0,retain=True) #publish + + + print('Lettura data: {} n: {} dove: {} importo elettricità: {}€ '.format(data,fattura,dove,elettricita)) + f = open("bollette.csv", "a") + f.write(data + ',' + fattura + ',' + dove + ',,' + elettricita + ',' + elettricita + '\n') + f.close() + +def on_publish(client,userdata,result): #create function for callback + print("data published \n") + pass + +def agsmenergia(): + giaScaricati = [] #doc che si trovano gia in csv. + with open('bollette.csv') as csv_file: + csv_reader = csv.reader(csv_file, delimiter=',') + line_count = 0 + for row in csv_reader: + if line_count == 0: + line_count += 1 + else: + try: + a = row[1] + a=a.split('/') + giaScaricati.append(a[1]) + except: #mai scaricato nulla + pass + + # Fill in your details here to be posted to the login form. + payload = { + 'username': login, + 'password': passwd + } + # Use 'with' to ensure the session context is closed after use. + with requests.Session() as s: + p = s.post(url, data=payload) + # print the html returned or something more intelligent to see if it's a successful login page. + #print (p.text) + try: + page = s.get('https://countbox.agsm.it/bills') + html = page.text + r = re.compile('e|SD>}n#sa0KMtFGjo1Jbkq}eptIdgty_V*`K zu2x4iI_QyauBj5vjjna2OodkJsa0C@uhnVCzfl^{DaaYvGrk)terHwV$>C`SmMU~6 zbR|?{|HFV=qvHs#-HzP3O1!iKI##$ZN$hFw z0)O;b!eVSaym1FPdw#>1@&@itu+irNL^7}( zVK{-w_$qw|jL%@I`w^4lK2D(>d2EuFbxE#Q+%NU4mwybVla7ct7@o*!E==J(N{zBp0E WUZ$t)nPnaJl<{6;iX_b2H}wa_0cPg_ literal 0 HcmV?d00001