from flask import Flask, request, current_app as app from .html import process_urls, fetch_page, fetch_plaintext from .mail import send_mail @app.route("/webhook", methods=["POST"]) def webhook(): mail = request.json parts = mail["Parts"] try: p = parts[0]["text/plain"] urls = process_urls(p) except KeyError: p = parts[0]["text/html"] urls = process_urls(p, html=True) for u in urls: pageinfo = fetch_page(u) text = fetch_plaintext(pageinfo["html"]) send_mail( mail["From"].strip(), mail["ReplyTo"].strip(), u + ": " + pageinfo["title"].strip(), pageinfo["html"].strip(), text, ) return "ok"