diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afed073 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.csv diff --git a/python/muell.py b/python/muell.py index ba19ee9..d550375 100644 --- a/python/muell.py +++ b/python/muell.py @@ -69,6 +69,19 @@ def init(key, modus, host, headers): return name, value +def read_file(jahr): + ''' Liest Daten aus muell.csv und liefert ggf. Ergebnis zurück''' + + antwort_liste = None + try: + f = open(f'muell{jahr}.csv', 'rb') + antwort = str(f.read()) + antwort_liste = antwort[antwort.find('Biom'):].strip("'").replace('\\n', '\n').split('\n') + except FileNotFoundError: + pass + + return antwort_liste + # Variablen key = 'e21758b9c711463552fb9c70ac7d4273' modus = 'd6c5855a62cf32a4dadbc2831f0f295f' @@ -106,35 +119,31 @@ muell_arten = [ 'Schadstoffmobil', ] -tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y') - # __main__ -config = readConfig() -postdata['f_id_kommune'] = config['kommune'] -postdata['f_id_strasse'] = config['strasse'] - -(name, value) = init(key, modus, host, headers) -if name != None and value != None: - postdata[name] = value - -http = httplib2.Http() -(resp, content) = http.request(url, "POST", headers = headers, body = urllib.parse.urlencode(postdata)) - -antwort = str(content) -antwort_liste = antwort[antwort.find('Biom'):].strip("'").replace('\\n', '\n').split('\n') - -if antwort_liste[0] == '': - # Keine vernünftigen Daten, ergo mal gucken, was in der Datei steht - try: - f = open('muell.csv', 'rb') - antwort = str(f.read()) - antwort_liste = antwort[antwort.find('Biom'):].strip("'").replace('\\n', '\n').split('\n') - except FileNotFoundError: - pass +antwort_liste = read_file(current_year) +if antwort_liste == None: + # Keine vernünftigen Daten, ergo mal gucken, was die Webseite ergibt + config = readConfig() + postdata['f_id_kommune'] = config['kommune'] + postdata['f_id_strasse'] = config['strasse'] + + (name, value) = init(key, modus, host, headers) + if name != None and value != None: + postdata[name] = value + + http = httplib2.Http() + (resp, content) = http.request(url, "POST", headers = headers, body = urllib.parse.urlencode(postdata)) + + antwort = str(content) + f = open(f'muell{current_year}.csv', 'wb') + f.write(content) + f.close() + antwort_liste = antwort[antwort.find('Biom'):].strip("'").replace('\\n', '\n').split('\n') daten = set(antwort_liste) for datum in daten: + tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y') datum = datum.split(';') l_muell = get_index_positions(datum, tomorrow) if len(l_muell) > 0: