-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnseenMail.py
More file actions
executable file
·56 lines (48 loc) · 1.88 KB
/
Copy pathUnseenMail.py
File metadata and controls
executable file
·56 lines (48 loc) · 1.88 KB
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
#!/usr/bin/env python3
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import imaplib
import os
import configparser
dirname = os.path.split(os.path.abspath(__file__))[0]
accounts = configparser.RawConfigParser()
accounts.read(os.path.abspath(dirname + '/accounts.ini'))
strFormatted = ""
def check_imap(imap_account):
if imap_account["useSSL"] == "true":
client = imaplib.IMAP4_SSL(imap_account["host"], int(imap_account["port"]))
else:
client = imaplib.IMAP4(imap_account["host"], int(imap_account["port"]))
client.login(imap_account["login"], imap_account["password"])
if "folder" in imap_account:
client.select(imap_account["folder"])
else:
client.select()
return len(client.search(None, 'UNSEEN')[1][0].split())
def check_gmail(gmail_account):
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
credential_file = 'gmail/' + gmail_account + '.json'
store = file.Storage(credential_file)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets('gmail/client_secret.json', SCOPES)
credentials = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=credentials.authorize(Http()))
results = service.users().messages().list(userId='me', q="is:unread").execute()
return len(results["messages"])
for account in accounts:
currentAccount = accounts[account]
if account == "DEFAULT":
continue
if not currentAccount["icon"]:
icon = accounts["DEFAULT"]["icon"]
else:
icon = currentAccount["icon"]
if currentAccount['protocol'] == "GmailAPI":
unread = check_gmail(account)
else:
unread = check_imap(currentAccount)
strFormatted += icon + " " + str(unread) + " "
print(strFormatted)