diff --git a/README.md b/README.md index 70fa353..184618c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ py-gmail-unread =============== + +I wanted to inject the number of unread messages I have into my ZSH prompt and +obviously I needed a way to get the unread count. This script should be set up +in cron to run periodically as it's a bit too slow for inline calls. Depends on +the EMAIL environment variable as well as your password being loaded up into +the OSX keychain under the name "email" using your password as the account. The +script saves the unread count out to /tmp/GMAIL_UNREAD and then you can do +whatever the fuck you want do to with it, as long as it's not illegal. diff --git a/gmail-unread.py b/gmail-unread.py new file mode 100755 index 0000000..96e27d1 --- /dev/null +++ b/gmail-unread.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +from __future__ import print_function +import imaplib +import os + +email = os.environ['EMAIL'] +password = os.popen('security find-generic-password -w -s email -a "' + email + '"').read().replace('\n', '') + +imap = imaplib.IMAP4_SSL('imap.gmail.com', '993') +imap.login(email, password) +imap.select() + +fp = open('/tmp/GMAIL_UNREAD', 'w') +print(len(imap.search(None,'UnSeen')[1][0].split()), file=fp)