Browse Source

Einlesesen der CSV-Datei mit Modul CSV

develop
Hauke Zühl 3 years ago
parent
commit
ce2889991a
Signed by: hauke
GPG Key ID: 7E70BF5E52D4CA72
  1. 36
      python/muell.py

36
python/muell.py

@ -10,13 +10,14 @@ Um die Daten an Telegram zu senden, musst du dir ein Skript/Programm schreiben,
von Telegram verwendet und Daten an dich oder deine Gruppe senden kann!
Beispiel für eine Konfigurationsdatei:
tgReceiver: <hier die ID der Gruppe/ des Teilnhemers eintragen>
tgReceiver: <hier die ID der Gruppe/ des Teilnehmers eintragen>
kommune: 2601
strasse: 2146
pathBot: '~/bin/YMBot'
tgBotOwner: <hier DEINE Telegram-ID eintragen>
'''
import csv
import re
import requests
import httplib2
@ -28,7 +29,7 @@ from pathlib import Path
# Funktionen
def get_index_positions(list_of_elems, element):
''' Returns the indexes of all occurrences of give element in
''' Returns the indexes of all occurrences of given element in
the list- listOfElements '''
index_pos_list = []
for i in range(len(list_of_elems)):
@ -53,7 +54,7 @@ def init(key, modus, host, headers):
url = f'https://{host}/?key={key}&modus={modus}&waction=init'
http = httplib2.Http()
(resp, content) = http.request(url, "POST", headers = headers, body = urllib.parse.urlencode(postdata))
# print(content)
result = re.findall(r"<input .*?name=.*? value=.*?/>", str(content))
datum = result[1].split(' ')
@ -74,9 +75,17 @@ def read_file(jahr):
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')
with open(f'muell{jahr}.csv', 'r', encoding="latin1") as f:
csv_reader = csv.reader(f, delimiter=';')
lines = 0
antwort_liste = []
for row in csv_reader:
if lines == 0:
# print(f'Column names are {", ".join(row)}')
lines += 1
else:
if isinstance(row, list):
antwort_liste.extend(row)
except FileNotFoundError:
pass
@ -119,9 +128,10 @@ muell_arten = [
'Schadstoffmobil',
]
# __main__
antwort_liste = read_file(current_year)
if antwort_liste == None:
if __name__ == '__main__':
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']
@ -138,12 +148,12 @@ if antwort_liste == None:
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)
antwort_liste = read_file(current_year)
for datum in daten:
daten = set(antwort_liste)
# TODO: Leider weiss ich ab hier nur, dass Müll abgeholt wird, leider nicht, welcher :-/
tomorrow = (datetime.now() + timedelta(1)).strftime('%d.%m.%Y')
for datum in daten:
datum = datum.split(';')
l_muell = get_index_positions(datum, tomorrow)
if len(l_muell) > 0:

Loading…
Cancel
Save