Vereinfacht

This commit is contained in:
Hauke Zühl 2026-05-17 16:11:28 +02:00
parent 8e6549fd5e
commit 091e801b7e
Signed by: hauke
GPG Key ID: 7E70BF5E52D4CA72

View File

@ -33,18 +33,18 @@ from pathlib import Path
class Muell:
# Variablen
__key = 'e21758b9c711463552fb9c70ac7d4273'
__modus = 'd6c5855a62cf32a4dadbc2831f0f295f'
key = 'e21758b9c711463552fb9c70ac7d4273'
modus = 'd6c5855a62cf32a4dadbc2831f0f295f'
__host = 'api.abfall.io'
host = 'api.abfall.io'
__url = f'https://{__host}/?key={__key}&modus={__modus}&waction=export_csv'
__export_als = f"{{'action':'{__url}','target':''}}"
url = f'https://{host}/?key={key}&modus={modus}&waction=export_csv'
export_als = f"{{'action':'{url}','target':''}}"
__current_year = datetime.today().year
__zeitraum = f'{__current_year}0101-{__current_year}1231'
current_year = datetime.today().year
zeitraum = f'{current_year}0101-{current_year}1231'
__postdata = {
postdata = {
'f_id_abfalltyp_0': '50',
'f_id_abfalltyp_1': '161',
'f_id_abfalltyp_2': '53',
@ -52,11 +52,11 @@ class Muell:
'f_id_abfalltyp_4': '169',
'f_abfallarten_index_max': '5',
'f_abfallarten': '50,161,53,187',
'f_zeitraum': __zeitraum,
'f_export_als': __export_als,
'f_zeitraum': zeitraum,
'f_export_als': export_als,
}
__headers = {
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; x68_64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
'Content-Type': 'application/x-www-form-urlencoded',
}
@ -82,12 +82,12 @@ class Muell:
return config
def __init(self, key, modus, host, headers):
def __init(self):
''' Initialisiert das System und sucht entsprechende Daten heraus '''
url = f'https://{host}/?key={key}&modus={modus}&waction=init'
url = f'https://{self.host}/?key={self.key}&modus={self.modus}&waction=init'
http = httplib2.Http()
(resp, content) = http.request(url, "POST", headers = headers, body = urllib.parse.urlencode(self.__postdata))
(resp, content) = http.request(url, "POST", headers = self.headers, body = urllib.parse.urlencode(self.postdata))
result = re.findall(r"<input .*?name=.*? value=.*?/>", str(content))
@ -128,26 +128,26 @@ class Muell:
return (antwort_liste, headline)
def get_data(self):
(antwort_liste, headline) = self.__read_file(self.__current_year)
(antwort_liste, headline) = self.__read_file(self.current_year)
config = self.__readConfig()
if not antwort_liste:
# Keine vernünftigen Daten, ergo mal gucken, was die Webseite ergibt
self.__postdata['f_id_kommune'] = config['kommune']
self.__postdata['f_id_strasse'] = config['strasse']
self.postdata['f_id_kommune'] = config['kommune']
self.postdata['f_id_strasse'] = config['strasse']
(name, value) = self.__init(key, modus, host, headers)
(name, value) = self.__init()
if name != None and value != None:
self.__postdata[name] = value
self.postdata[name] = value
http = httplib2.Http()
(resp, content) = http.request(url, "POST", headers = headers, body = urllib.parse.urlencode(self.__postdata))
(resp, content) = http.request(self.url, "POST", headers = self.headers, body = urllib.parse.urlencode(self.postdata))
antwort = str(content)
f = open(f'muell{self.__current_year}.csv', 'wb')
f = open(f'muell{self.current_year}.csv', 'wb')
f.write(content)
f.close()
(antwort_liste, headline) = self.__read_file(self.__current_year)
(antwort_liste, headline) = self.__read_file(self.current_year)
tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y')
index = set();
@ -169,35 +169,37 @@ class Muell:
for pos in index:
tonnen.append(headline[pos])
return (tonnen, wird)
def ausgabe_telegram(self, tonnen, wird):
''' Ausgabe Richtung Telegram'''
if len(tonnen) > 0:
tonnen = ' und '.join(tonnen)
ausgabe = f'Morgen {wird} {tonnen} abgeholt'
else:
ausgabe = None
return ausgabe
def ausgabe_telegram(self, ausgabe):
''' Ausgabe Richtung Telegram'''
if ausgabe is not None:
# Jetzt den Telegram Bot ansprechen
try:
os.system(f'echo "Morgen {wird} {tonnen} abgeholt" | {config["telegram"]["pathBot"]} -u {config["telegram"]["tgReceiver"]}')
os.system(f'echo "{ausgabe}" | {config["telegram"]["pathBot"]} -u {config["telegram"]["tgReceiver"]}')
except Exception as e:
os.system(f'logger "Fehler Muellbot Telegram: {e}"')
def ausgabe_signal(self, tonnen, wird):
def ausgabe_signal(self, ausgabe):
''' Ausgabe Richtung Signal'''
if len(tonnen) > 0:
tonnen = ' und '.join(tonnen)
if ausgabe is not None:
# Signal Bot
try:
os.system(f'{config["signal"]["path"]} -a {config["signal"]["account"]} send -g {config["signal"]["group_id"]} -m "Morgen {wird} {tonnen} abgeholt"')
os.system(f'{config["signal"]["path"]} -a {config["signal"]["account"]} send -g {config["signal"]["group_id"]} -m "{ausgabe}"')
except Exception as e:
os.system(f'logger "Fehler Muellbot Signal: {e}"')
def ausgabe_text(self, tonnen, wird):
def ausgabe_text(self, ausgabe):
''' Ausgabe als Text'''
if len(tonnen) > 0:
tonnen = ' und '.join(tonnen)
print(f'Morgen {wird} {tonnen} abgeholt')
if ausgabe is not None:
print(f'{ausgabe}')