-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
116 lines (90 loc) · 3.45 KB
/
mail.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import requests
import json
import feedparser as fp
import notify2
import os
import datetime
DATA_DIR = os.path.expanduser('~/.data/')
with open(os.path.expanduser("~/scripts/.env")) as r:
auth = json.load(r)
if 'outgoing' in auth:
auth.pop('outgoing')
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR, exist_ok=True)
EMAIL_JSON = os.path.join(DATA_DIR, 'email.json')
BLACKLIST_EMAILS_JSON = os.path.join(DATA_DIR, 'email-blacklist.json')
EMAIL_TXT = os.path.join(DATA_DIR, 'email.txt')
def read_json(JSON):
if os.path.exists(JSON):
with open(JSON, 'r') as r:
_dict = json.load(r)
else:
_dict = dict()
return _dict
emails_dict = read_json(EMAIL_JSON)
blacklist = set(read_json(BLACKLIST_EMAILS_JSON))
COLORS = {
'reset': '$color',
'red': '${color ff0000}',
'blue': '${color 0000ff}',
'green': '${color 00ff00}',
'bluish': '${color 0011ff}'
}
NO_OF_MAILS = 3
def shorten(string):
if len(string) > 16:
return string[:13] + "..."
return string + ' ' * (16 - len(string))
def format_conky(address, count, mails):
mails_str = '\n * '.join([f'{t} -{s}' for t, s in mails])
return (COLORS['red'] + f'{count} new mails' + COLORS['green'] +
f'({"█"*6+address[6:]})' + COLORS['reset'] + '\n * ' + mails_str +
'\n')
def format_notice(address, count, mails):
mails_str = '\n * '.join([f'{t} -{s}' for t, s in mails])
return f'{count} new mails ({address})', ' * ' + mails_str
def send_notification(title, contents, url=None):
notify2.init('Email Check')
n = notify2.Notification(title, message=contents)
n.set_urgency(notify2.URGENCY_CRITICAL)
# if url is not None:
# def call_b(msg, action, url):
# webbrowser.open_new_tab(url)
# n.add_action('open-firefox', 'view', call_b, url)
n.show()
new_emails_dict = dict()
dt = datetime.datetime.now().strftime('%H:%M %b-%d')
txt = '${color e43526}EMAILS: (' + dt + ')${color}\n'
for my_mail, my_pass in auth.items():
new_emails_dict[my_mail] = dict()
# url = f'https://{my_mail}:{my_pass}@mail.google.com/mail/feed/atom/^smartlabel_personal'
url = f'https://{my_mail}:{my_pass}@mail.google.com/mail/feed/atom/' + '^sq_ig_i_personal'
try:
mail = fp.parse(requests.get(url).text)
except requests.HTTPError:
raise SystemExit('No internet')
new_emails_dict[my_mail]['count'] = mail.feed.fullcount
new_emails_dict[my_mail]['mails'] = [m['id'] for m in mail.entries
if len({a.email for a in m.authors}.intersection(blacklist)) == 0]
mails = []
i = 0
for e in mail.entries:
if i >= NO_OF_MAILS:
break
elif e.id not in new_emails_dict[my_mail]['mails']:
continue
title = e.title
sender = ";".join(a.name for a in e.authors)
mails.append((shorten(title), shorten(sender)))
i += 1
history = emails_dict.get(my_mail)
if history is None or (history['count'] < mail.feed.fullcount and
new_emails_dict[my_mail]['mails'][0] not in
history['mails']):
t, c = format_notice(my_mail, mail.feed.fullcount, mails)
send_notification(t, c, url=mail.feed.link)
txt += format_conky(my_mail, mail.feed.fullcount, mails)
with open(EMAIL_TXT, 'w') as writer:
writer.write(txt)
with open(EMAIL_JSON, 'w') as writer:
json.dump(new_emails_dict, writer)