Verwaltung der Konfigdatei flexibler gemacht, CSV-Datei hat nun Kommune im Namen
This commit is contained in:
+16
-12
@@ -30,9 +30,11 @@ from pathlib import Path
|
|||||||
|
|
||||||
class Muell:
|
class Muell:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, config_file='.muell.yaml'):
|
||||||
|
|
||||||
# Variablen
|
# Variablen
|
||||||
|
self.__config_file = config_file
|
||||||
|
self.__config = self.__readConfig()
|
||||||
self.__key = 'e21758b9c711463552fb9c70ac7d4273'
|
self.__key = 'e21758b9c711463552fb9c70ac7d4273'
|
||||||
self.__modus = 'd6c5855a62cf32a4dadbc2831f0f295f'
|
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',
|
'User-Agent': 'Mozilla/5.0 (Linux; x68_64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
}
|
}
|
||||||
|
kommune = self.__config['kommune']
|
||||||
|
self.__csv = f'muell-{kommune}-{self.__current_year}.csv'
|
||||||
|
|
||||||
# Methoden
|
# Methoden
|
||||||
def __get_index_positions(self, list_of_elems, element):
|
def __get_index_positions(self, list_of_elems, element):
|
||||||
@@ -73,11 +77,11 @@ class Muell:
|
|||||||
return index_pos_list
|
return index_pos_list
|
||||||
|
|
||||||
def __readConfig(self):
|
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!
|
home = Path.home() # home ist ohne / am Ende!
|
||||||
config = {}
|
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)
|
config = yaml.safe_load(config_file)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
@@ -104,13 +108,14 @@ class Muell:
|
|||||||
|
|
||||||
return name, value
|
return name, value
|
||||||
|
|
||||||
def __read_file(self, jahr):
|
def __read_file(self):
|
||||||
''' Liest Daten aus muell<jahr>.csv und liefert ggf. Ergebnis zurück'''
|
''' Liest Daten aus self.__csv und liefert ggf. Ergebnis zurück'''
|
||||||
|
|
||||||
antwort_liste = list()
|
antwort_liste = list()
|
||||||
headline = list()
|
headline = list()
|
||||||
|
|
||||||
try:
|
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=';')
|
csv_reader = csv.reader(f, delimiter=';')
|
||||||
lines = 0
|
lines = 0
|
||||||
antwort_liste = []
|
antwort_liste = []
|
||||||
@@ -128,13 +133,12 @@ class Muell:
|
|||||||
return (antwort_liste, headline)
|
return (antwort_liste, headline)
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
(antwort_liste, headline) = self.__read_file(self.__current_year)
|
(antwort_liste, headline) = self.__read_file()
|
||||||
config = self.__readConfig()
|
|
||||||
|
|
||||||
if not antwort_liste:
|
if not antwort_liste:
|
||||||
# Keine vernünftigen Daten, ergo mal gucken, was die Webseite ergibt
|
# Keine vernünftigen Daten, ergo mal gucken, was die Webseite ergibt
|
||||||
self.__postdata['f_id_kommune'] = config['kommune']
|
self.__postdata['f_id_kommune'] = self.__config['kommune']
|
||||||
self.__postdata['f_id_strasse'] = config['strasse']
|
self.__postdata['f_id_strasse'] = self.__config['strasse']
|
||||||
|
|
||||||
(name, value) = self.__start()
|
(name, value) = self.__start()
|
||||||
if name != None and value != None:
|
if name != None and value != None:
|
||||||
@@ -145,10 +149,10 @@ class Muell:
|
|||||||
content = resp.content
|
content = resp.content
|
||||||
|
|
||||||
antwort = str(content)
|
antwort = str(content)
|
||||||
f = open(f'muell{self.__current_year}.csv', 'wb')
|
f = open(self.__csv, 'wb')
|
||||||
f.write(content)
|
f.write(content)
|
||||||
f.close()
|
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')
|
tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y')
|
||||||
index = set();
|
index = set();
|
||||||
|
|||||||
Reference in New Issue
Block a user