3 Commits
2 changed files with 41 additions and 17 deletions
+18 -14
View File
@@ -30,9 +30,11 @@ from pathlib import Path
class Muell:
def __init__(self):
def __init__(self, config_file='.muell.yaml'):
# Variablen
self.__config_file = config_file
self.__config = self.__readConfig()
self.__key = 'e21758b9c711463552fb9c70ac7d4273'
self.__modus = 'd6c5855a62cf32a4dadbc2831f0f295f'
@@ -60,6 +62,8 @@ class Muell:
'User-Agent': 'Mozilla/5.0 (Linux; x68_64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
'Content-Type': 'application/x-www-form-urlencoded',
}
kommune = self.__config['kommune']
self.__csv = f'muell-{kommune}-{self.__current_year}.csv'
# Methoden
def __get_index_positions(self, list_of_elems, element):
@@ -73,11 +77,11 @@ class Muell:
return index_pos_list
def __readConfig(self):
''' Liest ~/.muell.yaml und speichert Daten in Dictionary '''
''' Liest Konfigdatei und speichert Daten in Dictionary '''
home = Path.home() # home ist ohne / am Ende!
config = {}
with open(f'{home}/.muell.yaml', 'r') as config_file:
with open(f'{home}/{self.__config_file}', 'r') as config_file:
config = yaml.safe_load(config_file)
return config
@@ -104,13 +108,14 @@ class Muell:
return name, value
def __read_file(self, jahr):
''' Liest Daten aus muell<jahr>.csv und liefert ggf. Ergebnis zurück'''
def __read_file(self):
''' Liest Daten aus self.__csv und liefert ggf. Ergebnis zurück'''
antwort_liste = list()
headline = list()
try:
with open(f'muell{jahr}.csv', 'r', encoding="latin1") as f:
with open(self.__csv, 'r', encoding="latin1") as f:
csv_reader = csv.reader(f, delimiter=';')
lines = 0
antwort_liste = []
@@ -128,13 +133,12 @@ class Muell:
return (antwort_liste, headline)
def get_data(self):
(antwort_liste, headline) = self.__read_file(self.__current_year)
config = self.__readConfig()
(antwort_liste, headline) = self.__read_file()
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'] = self.__config['kommune']
self.__postdata['f_id_strasse'] = self.__config['strasse']
(name, value) = self.__start()
if name != None and value != None:
@@ -145,10 +149,10 @@ class Muell:
content = resp.content
antwort = str(content)
f = open(f'muell{self.__current_year}.csv', 'wb')
f = open(self.__csv, 'wb')
f.write(content)
f.close()
(antwort_liste, headline) = self.__read_file(self.__current_year)
(antwort_liste, headline) = self.__read_file()
tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y')
index = set();
@@ -184,7 +188,7 @@ class Muell:
if ausgabe is not None:
# Jetzt den Telegram Bot ansprechen
try:
os.system(f'echo "{ausgabe}" | {config["telegram"]["pathBot"]} -u {config["telegram"]["tgReceiver"]}')
os.system(f'echo "{ausgabe}" | {self.__config["telegram"]["pathBot"]} -u {self.__config["telegram"]["tgReceiver"]}')
except Exception as e:
os.system(f'logger "Fehler Muellbot Telegram: {e}"')
@@ -194,7 +198,7 @@ class Muell:
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 "{ausgabe}"')
os.system(f'{self.__config["signal"]["path"]} -a {self.__config["signal"]["account"]} send -g {self.__config["signal"]["group_id"]} -m "{ausgabe}"')
except Exception as e:
os.system(f'logger "Fehler Muellbot Signal: {e}"')
+23 -3
View File
@@ -1,9 +1,29 @@
#!/usr/bin/python3
import argparse
from Muell import Muell
if __name__ == '__main__':
muell = Muell()
parser = argparse.ArgumentParser(
prog='muell',
description='Benachrichtigt Empfänger über anstehende Müllabfuhr'
)
parser.add_argument('-c', '--config', default='.muell.yaml', help='Name der Konfigdatei. Diese muss sich im Benutzerverzeichnis befinden!')
parser.add_argument('-t', '--telegram', action='store_true', help='Ausgabe auf Telegram')
parser.add_argument('-s', '--signal', action='store_true', help='Ausgabe auf Signal')
parser.add_argument('-k', '--konsole', action='store_true', help='Ausgabe auf Konsole')
args = parser.parse_args()
muell = Muell(args.config)
ausgabe = muell.get_data()
muell.ausgabe_telegram(ausgabe)
muell.ausgabe_signal(ausgabe)
if args.telegram == True:
muell.ausgabe_telegram(ausgabe)
if args.signal == True:
muell.ausgabe_signal(ausgabe)
if args.konsole == True:
muell.ausgabe_text(ausgabe)