You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.2 KiB
150 lines
4.2 KiB
4 years ago
|
#!/usr/bin/python
|
||
|
|
||
|
'''
|
||
|
Um die Werte für kommune und strasse zu erfahren, rufe https://www.egst.de/de/abfallabholung/ per
|
||
|
Browser auf und ermittle die gesendeten Daten in der Entwicklerkonsole deines Browsers.
|
||
|
|
||
|
Die Angaben hier im Skript stehen für Hörstel als Kommune und Im Wiesengrund als Strasse
|
||
|
|
||
|
Um die Daten an Telegram zu senden, musst du dir ein Skript/Programm schreiben, dass die API
|
||
|
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>
|
||
|
kommune: 2601
|
||
|
strasse: 2146
|
||
|
pathBot: '~/bin/YMBot'
|
||
|
tgBotOwner: <hier DEINE Telegram-ID eintragen>
|
||
|
'''
|
||
|
|
||
|
import re
|
||
|
import requests
|
||
|
import httplib2
|
||
|
import urllib
|
||
|
import yaml
|
||
|
|
||
|
from datetime import datetime, timedelta
|
||
|
from pathlib import Path
|
||
|
|
||
|
# Funktionen
|
||
|
def get_index_positions(list_of_elems, element):
|
||
|
''' Returns the indexes of all occurrences of give element in
|
||
|
the list- listOfElements '''
|
||
|
index_pos_list = []
|
||
|
for i in range(len(list_of_elems)):
|
||
|
if list_of_elems[i] == element:
|
||
|
index_pos_list.append(i)
|
||
|
|
||
|
return index_pos_list
|
||
|
|
||
|
def readConfig():
|
||
|
''' Liest ~/.muell.yaml und speichert Daten in Dictionary '''
|
||
|
|
||
|
home = Path.home() # home ist ohne / am Ende!
|
||
|
config = {}
|
||
|
with open(f'{home}/.muell.yaml', 'r') as config_file:
|
||
|
config = yaml.load(config_file, Loader = yaml.FullLoader)
|
||
|
|
||
|
return config
|
||
|
|
||
|
def init(key, modus, host, headers):
|
||
|
''' Initialisiert das System und sucht entsprechende Daten heraus '''
|
||
|
|
||
|
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(' ')
|
||
|
|
||
|
name = None
|
||
|
value = None
|
||
|
for i in datum:
|
||
|
i = i.split('=')
|
||
|
if i[0] == 'name':
|
||
|
name = i[1][1:-1]
|
||
|
if i[0] == 'value':
|
||
|
value = i[1][1:-1]
|
||
|
|
||
|
return name, value
|
||
|
|
||
|
# Variablen
|
||
|
key = 'e21758b9c711463552fb9c70ac7d4273'
|
||
|
modus = 'd6c5855a62cf32a4dadbc2831f0f295f'
|
||
|
|
||
|
host = 'api.abfall.io'
|
||
|
|
||
|
url = f'https://{host}/?key={key}&modus={modus}&waction=export_csv'
|
||
|
export_als = f"{{'action':'{url}','target':''}}"
|
||
|
|
||
|
current_year = datetime.today().year
|
||
|
zeitraum = f'{current_year}0101-{current_year}1231'
|
||
|
|
||
|
postdata = {
|
||
|
'f_id_abfalltyp_0': '50',
|
||
|
'f_id_abfalltyp_1': '161',
|
||
|
'f_id_abfalltyp_2': '53',
|
||
|
'f_id_abfalltyp_3': '187',
|
||
|
'f_id_abfalltyp_4': '169',
|
||
|
'f_abfallarten_index_max': '5',
|
||
|
'f_abfallarten': '50,161,53,187,169',
|
||
|
'f_zeitraum': zeitraum,
|
||
|
'f_export_als': export_als,
|
||
|
}
|
||
|
|
||
|
headers = {
|
||
|
'User-Agent': 'Mozilla/5.0 (Linux; x68_64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
|
||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||
|
}
|
||
|
|
||
|
muell_arten = [
|
||
|
'Biomüll',
|
||
|
'Gelbe Tonne',
|
||
|
'Restmüll',
|
||
|
'Papiermüll',
|
||
|
'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
|
||
|
|
||
|
daten = set(antwort_liste)
|
||
|
|
||
|
for datum in daten:
|
||
|
datum = datum.split(';')
|
||
|
l_muell = get_index_positions(datum, tomorrow)
|
||
|
if len(l_muell) > 0:
|
||
|
wird = 'wird' if len(l_muell) < 2 else 'werden'
|
||
|
tonnen = []
|
||
|
for i in l_muell:
|
||
|
tonnen.append(muell_arten[i])
|
||
|
|
||
|
if len(tonnen) > 0:
|
||
|
tonnen = ' und '.join(tonnen)
|
||
|
print(f'Morgen {wird} {tonnen} abgeholt')
|
||
|
break
|