diff options
Diffstat (limited to 'user-profiles/feeds/imm-notmuch-insert.py')
-rw-r--r-- | user-profiles/feeds/imm-notmuch-insert.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/user-profiles/feeds/imm-notmuch-insert.py b/user-profiles/feeds/imm-notmuch-insert.py new file mode 100644 index 00000000..855f3a83 --- /dev/null +++ b/user-profiles/feeds/imm-notmuch-insert.py | |||
@@ -0,0 +1,37 @@ | |||
1 | #!@python@/bin/python | ||
2 | |||
3 | import json | ||
4 | import sys | ||
5 | import subprocess | ||
6 | from io import StringIO | ||
7 | from email.message import EmailMessage | ||
8 | import configparser | ||
9 | from os import environ | ||
10 | from dateutil.parser import isoparse | ||
11 | |||
12 | def main(): | ||
13 | notmuchConfig = configparser.ConfigParser() | ||
14 | notmuchConfig.read(environ.get('NOTMUCH_CONFIG')) | ||
15 | |||
16 | callbackMessage = json.load(sys.stdin) | ||
17 | |||
18 | msg = EmailMessage() | ||
19 | authors = ', '.join(map(lambda author: author['name'], callbackMessage['feed_item']['authors'])) | ||
20 | msg['From'] = f"{callbackMessage['feed_definition']['title']} ({authors}) <imm@imm.invalid>" | ||
21 | msg['To'] = f"{notmuchConfig['user']['name']} <{notmuchConfig['user']['primary_email']}>" | ||
22 | msg['Subject'] = callbackMessage['feed_item']['title'] | ||
23 | msg['Message-ID'] = f"{callbackMessage['feed_item']['identifier']@imm.invalid}" | ||
24 | for link in callbackMessage['feed_item']['links']: | ||
25 | msg.add_header('Link', link) | ||
26 | if 'date' in callbackMessage['feed_item']: | ||
27 | date = isoparse(callbackMessage['feed_item']['date']) | ||
28 | msg['Date'] = date.strftime('%a, %e %b %Y %T %z') | ||
29 | |||
30 | subprocess.run( | ||
31 | args=['notmuch', 'insert'], | ||
32 | check=True, | ||
33 | stdin=StringIO(bytes(msg)) | ||
34 | ) | ||
35 | |||
36 | if __name__ == '__main__': | ||
37 | sys.exit(main()) | ||