Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- Erstmal nur als Konzept ...
- """
- import time
- import csv
- import datetime
- from collections import namedtuple
- from functools import wraps
- from io import StringIO
- import requests
- from bs4 import BeautifulSoup
- Felder = "datum tag plz stadt uhrzeit ort art".split()
- Result = namedtuple("Termine", Felder)
- def cache(ttl):
- def wrapper(func):
- result = None
- last = 0
- @wraps(func)
- def inner(*args, **kwargs):
- nonlocal result
- nonlocal last
- if result is None or time.monotonic() > last:
- result = func(*args, **kwargs)
- last = time.monotonic() + ttl
- return result
- return inner
- return wrapper
- @cache(3600)
- def get_demo_termine():
- time.sleep(2)
- url = "https://demo.terminkalender.top/"
- try:
- req = requests.get(url)
- except Exception as e:
- print(repr(e))
- return
- return BeautifulSoup(req.content, "html.parser")
- def demo_termine():
- bs = get_demo_termine()
- for row in bs.find_all("tr"):
- row = row.find_all("td")
- iterator = map(lambda x: x.text.strip(), row)
- try:
- result = Result(*iterator)
- except TypeError:
- continue
- datum_zeit = result.datum + "T" + result.uhrzeit.partition(" ")[0]
- try:
- date = datetime.datetime.fromisoformat(datum_zeit)
- except ValueError:
- continue
- result = Result(date, *result[1:])
- yield result
- def get_cities():
- return sorted({result.stadt.title() for result in demo_termine()})
- def next_demos():
- jetzt = datetime.datetime.now()
- return [termin for termin in demo_termine() if jetzt < termin.datum]
- def by_date(termin):
- return termin.datum
- def print_next_demos():
- for termin in sorted(set(next_demos()), key=by_date):
- print(
- f"{termin.datum:%d.%m} | {termin.datum:%H:%M} | {termin.plz:<5} | {termin.stadt:<25} | {termin.ort:<31} | {termin.art}"
- )
- def as_csv():
- io = StringIO()
- writer = csv.writer(io)
- writer.writerow(Felder)
- for termin in sorted(next_demos(), key=by_date):
- writer.writerow([termin.datum.isoformat(), *termin[1:]])
- return io.getvalue()
- if __name__ == "__main__":
- print_next_demos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement