os/+arch (polybar): add notifications mail & updates modules

This commit is contained in:
Henrik Lissner 2017-05-11 09:32:02 +02:00
parent ca1472c5c2
commit b05d268926
2 changed files with 73 additions and 18 deletions

View file

@ -1,30 +1,79 @@
#!/usr/bin/python #!/usr/bin/env python3
import imaplib import imaplib
import email
import os import os
import sys import sys
import re
from shlex import quote
ICON = "" ICON = ""
CONFIG_FILE = os.path.expanduser("~/.private/config/email") CONFIG_FILE = os.path.expanduser("~/.private/config/email")
CACHE_FILE = "/tmp/polybar-mail"
COLOR_HL = "B48EAD" COLOR_HL = "B48EAD"
COLOR_NONE = "65737E" COLOR_NONE = "65737E"
COLOR_ERR = "D08770" COLOR_ERR = "D08770"
n = 0
if os.path.isfile(CONFIG_FILE): #
with open(CONFIG_FILE) as fp: def recent_messages(address, password):
for line in fp:
if line:
addr, pwd = line.strip().split(",")
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993) obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login(addr, pwd) obj.login(address, password)
obj.select() try:
n += len(obj.search(None, 'UnSeen')[1][0].split()) obj.select('Inbox')
ret, resp = obj.search(None, '(UNSEEN)')
if ret == 'OK':
uids = resp[0].split()
if uids:
ret, data = obj.fetch(b",".join(uids), '(BODY.PEEK[])')
if ret == 'OK':
for m in filter(lambda x: len(x) > 1, data):
yield email.message_from_string(m[1].decode('utf-8'))
else:
print("Error: fetch")
else:
print("Error: lookup")
finally:
obj.close() obj.close()
obj.logout() obj.logout()
cl = COLOR_HL if n > 0 else COLOR_NONE
print("%{{u#{}}}%{{F#{}}}{} {}".format(cl, cl, ICON, n)) def notify(sender, subject):
if sender[0] == "?" and "<" in sender:
sender = sender.split("<")[1].split(">")[0]
os.system("/usr/bin/notify-send {} {}".format(quote("<b>{}</b>".format(sender)), quote(subject)))
def ding():
os.system("/usr/bin/aplay -N -q '{}'".format(os.path.expanduser("~/.config/polybar/sounds/robinhood76-music-box.wav")))
#
if __name__ == '__main__':
if os.path.isfile(CONFIG_FILE):
cache = []
if os.path.isfile(CACHE_FILE):
with open(CACHE_FILE, 'r') as fp:
cache = fp.read().strip().split("\n")
with open(CONFIG_FILE, 'r') as fp:
accounts = [line.strip().split(",") for line in fp if line]
total = 0
unique = 0
for addr, pwd in accounts:
for msg in recent_messages(addr, pwd):
total += 1
if msg['Message-Id'] not in cache:
notify(msg['From'], msg['Subject'])
cache.append(msg['Message-Id'].strip())
unique += 1
color = COLOR_HL if total > 0 else COLOR_NONE
print("%{{u#{}}}%{{F#{}}}{} {}".format(color, color, ICON, total))
if unique > 0:
ding()
with open(CACHE_FILE, 'w') as fp:
fp.write("\n".join(cache[-75:]))
else: else:
print("%{{F#{}}}{} -".format(COLOR_ERR, ICON)) print("%{{F#{}}}{} -".format(COLOR_ERR, ICON))
sys.exit(1) sys.exit(1)

View file

@ -2,9 +2,15 @@
pac=$(checkupdates | wc -l) pac=$(checkupdates | wc -l)
aur=$(cower -u | wc -l) aur=$(cower -u | wc -l)
last=$(</tmp/polybar-updates)
check=$((pac + aur)) check=$((pac + aur))
if [[ "$check" != "0" ]]; then if [[ "$check" != "0" ]]; then
echo "$pac %{F#5b5b5b}%{F-} $aur" echo "$pac %{F#5b5b5b}%{F-} $aur"
if [[ $last != "$pac$aur" ]]; then
notify-send "$((pac+aur)) updates available"
aplay -N -q ~/.config/polybar/sounds/robni7-news-ting.wav
echo "$pac$aur" >/tmp/polybar-updates
fi
else else
echo "%{F#5b5b5b}%{F-}" echo "%{F#5b5b5b}%{F-}"
fi fi