diff --git a/python/Muell/__init__.py b/python/Muell/__init__.py index fde3a82..bfff09c 100644 --- a/python/Muell/__init__.py +++ b/python/Muell/__init__.py @@ -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.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();