hyperskip (2220B)
1 #!/usr/bin/env python
2 import argparse
3 import datetime
4 import hashlib
5 import sys
6 import urllib.parse
7
8
9 def parse_comment(lines):
10 """Takes in a email file and outputs the comment
11 """
12 comment = {}
13 for i, line in enumerate(lines):
14 if line.startswith("Subject:"):
15 comment["uri"] = line.replace("Subject: RE:", "").strip()
16 elif line.startswith("From:"):
17 email = line.replace("From: ", "").replace(">", "").split(" <")[1].strip()
18 comment["email_hash"] = hashlib.sha256(str.encode(email)).hexdigest()[-10:]
19 elif line.startswith("Date:"):
20 # Expected format: Sun, 5 Apr 2020 21:38:47 -0500
21 comment["time"] = datetime.datetime.strptime(
22 line.replace("Date: ", "").strip(), "%a, %d %b %Y %H:%M:%S %z"
23 ).isoformat()
24 elif line.startswith("name="):
25 full_content = line
26 # Stop at first empty line
27 while line.strip():
28 i += 1
29 if i >= len(lines):
30 break
31 line = lines[i]
32 full_content += line
33 # Get rid of line concatination and Gmail's "=3D"
34 full_content = (
35 full_content.replace("=3D", "=").replace("\r", "").replace("=\n", "")
36 )
37 name, content = full_content.split("&")
38 comment["name"] = name.split("=")[1]
39 comment["content"] = urllib.parse.unquote_plus(content.split("=")[1])
40 return comment
41
42
43 def print_to_toml(comment):
44 """Print comment to TOML format
45 """
46 print("[[comments]]")
47 for key in ["name", "email_hash", "time", "uri"]:
48 if key == "time":
49 print(f"{key} = {comment[key]}")
50 else:
51 print(f'{key} = "{comment[key]}"')
52 print(f'content = """\n{comment["content"].strip()}\n"""')
53
54
55 if __name__ == "__main__":
56 parser = argparse.ArgumentParser(
57 description="Convert Hyperskip formatted email to compatible TOML format"
58 )
59 parser.add_argument(
60 "infile", nargs="?", type=argparse.FileType("r"), default=sys.stdin
61 )
62 args = parser.parse_args()
63 print_to_toml(parse_comment(args.infile.readlines()))