97 lines
2.5 KiB
Python
Executable file
97 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import imaplib
|
|
import os
|
|
import sys
|
|
import socket
|
|
from pathlib import Path
|
|
|
|
ICON = ""
|
|
CONFIG_FILE = os.path.expanduser("~/.private/config/email")
|
|
CACHE_FILE = "/tmp/polybar-mail"
|
|
|
|
COLOR_HL = "3E86B8"
|
|
COLOR_NONE = "22e5e8e6"
|
|
COLOR_ERR = "CC626A"
|
|
|
|
#
|
|
def internet_is_on():
|
|
try:
|
|
host = socket.gethostbyname("www.google.com")
|
|
socket.create_connection((host, 80), 2)
|
|
return True
|
|
except:
|
|
pass
|
|
return False
|
|
|
|
|
|
def unread_count(address, password):
|
|
imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
|
|
imap.login(address, password)
|
|
imap.select('INBOX')
|
|
|
|
status, resp = imap.search(None, 'UNSEEN')
|
|
imap.close()
|
|
imap.logout()
|
|
|
|
return len(resp[0].split())
|
|
|
|
|
|
def ding():
|
|
os.system("/usr/bin/aplay -N -q '{}'".format(os.path.expanduser("~/.config/polybar/sounds/robinhood76-music-box.wav")))
|
|
|
|
|
|
# def unread_messages(address, password):
|
|
# try:
|
|
# imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
|
|
# imap.login(address, password)
|
|
# imap.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:
|
|
# imap.close()
|
|
# imap.logout()
|
|
|
|
#
|
|
def main():
|
|
if os.path.isfile(CONFIG_FILE) and internet_is_on():
|
|
with open(CONFIG_FILE, 'r') as fp:
|
|
accounts = [line.strip().split(",") for line in fp if line]
|
|
|
|
cache = Path(CACHE_FILE)
|
|
cache_text = cache.read_text()
|
|
last_total = int(cache.read_text() if cache_text else 0)
|
|
|
|
total = 0
|
|
for addr, pwd in accounts:
|
|
total += unread_count(addr, pwd)
|
|
|
|
if total != last_total:
|
|
ding()
|
|
|
|
color = COLOR_HL if total > 0 else COLOR_NONE
|
|
if total == 0:
|
|
print("%{{u#{}}}%{{F#{}}}{} ".format(color, color, ICON))
|
|
else:
|
|
print("%{{u#{}}}%{{F#{}}}{} {}".format(color, color, ICON, total))
|
|
|
|
cache.write_text(str(total))
|
|
else:
|
|
print("%{{F#{}}}{} !".format(COLOR_ERR, ICON))
|
|
sys.exit(1)
|
|
|
|
|
|
#
|
|
if __name__ == '__main__':
|
|
main()
|