-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournalify-email.py
More file actions
executable file
·41 lines (31 loc) · 1.03 KB
/
journalify-email.py
File metadata and controls
executable file
·41 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# ]
# ///
import sys, email, email.utils, pathlib, os
def main(args):
if len(args) != 1:
raise TypeError("need 1 and only 1 argument")
emlpath = pathlib.Path(args[0])
with open(emlpath, "rb") as fo:
msg = email.message_from_bytes(fo.read())
date_str = "{:%Y%m%dT%H%M%S%z}".format(email.utils.parsedate_to_datetime(msg["date"]))
from_name, from_address = email.utils.parseaddr(msg["from"])
subject_str = msg["subject"]
journal_entry_name = "{date} [{sender_name} {sender_address}] {subject}".format(
date=date_str,
sender_name=from_name,
sender_address=from_address,
subject=subject_str,
)
journal_entry_path = emlpath.parent / journal_entry_name
journal_entry_path.mkdir()
destemlpath = journal_entry_path / emlpath.name
emlpath.rename(destemlpath)
print(destemlpath)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))