all repos — donkey @ dc80dc1a0d50b02336a12040d4dd62baa2f20000

The main backend for forlater.email

app/main.py (view raw)

 1from flask import Flask, request, current_app as app
 2
 3from .html import process_urls, fetch_page, fetch_plaintext
 4from .mail import send_mail
 5
 6
 7@app.route("/webhook", methods=["POST"])
 8def webhook():
 9    mail = request.json
10    parts = mail["Parts"]
11    try:
12        p = parts[0]["text/plain"]
13        urls = process_urls(p)
14    except KeyError:
15        p = parts[0]["text/html"]
16        urls = process_urls(p, html=True)
17
18    for u in urls:
19        pageinfo = fetch_page(u)
20        text = fetch_plaintext(pageinfo["html"])
21        send_mail(
22            mail["From"].strip(),
23            mail["ReplyTo"].strip(),
24            u + ": " + pageinfo["title"].strip(),
25            pageinfo["html"].strip(),
26            text,
27        )
28    return "ok"