import os import re from urllib.parse import parse_qs from urllib.request import urlopen import sys mentioned = None env = os.environ if env['REQUEST_METHOD'] == "POST": #qs = parse_qs(env['QUERY_STRING']) qs = parse_qs(sys.stdin.read()) # print(qs) if not "source" in qs or not "target" in qs: print("Status: 400 Bad Request") print() print("source or target not found") exit() if type(qs['source'][0]) != str or type(qs['target'][0]) != str: print("Status: 400 Bad Request") print() print("Invalid source or target") exit() source = qs['source'][0] target = qs['target'] s = source.split("#:~:text=") if len(s[1]) < 1: print("Status: 400 Bad Request") print("Content-Type: text/plain") print() print("Bad source") exit() print(s[0]) res = urlopen(s[0]) res = res.read().decode() for r in res.split("\n"): if r.startswith("#"): r = " ".join(r.split()) match = re.match(r"\#\ (\w*)\ \=\ (.*)",r) if match: if match.group(1) == "nick": mentioned = match.group(2) r = r.split("\t") if r[0] == s[1] and f"@" in r[1]: with open("webmentions.txt","a",encoding="utf8") as f: date = datetime.datetime.now().isoformat() f.write(date,"\t",f"{mentioned} mentioned you:\u2028{r[1]}\n",end="") print("Status: 200 OK") print("Content-Type: text/plain") print("X-Mentioned: ",mentioned) print("X-Mentioned-Content: ",r[1]) print() print("OK") exit() else: print("Status: 400 Bad Request") print("Content-Type: text/plain") print() print("Can't find source twt or there is no mention in it")