| 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import imaplib |
| 5 | |
| 6 | import mailsecrets |
| 7 | |
| 8 | def getmails(username, password, server): |
| 9 | imap = imaplib.IMAP4_SSL(server, 993) |
| 10 | imap.login(username, password) |
| 11 | imap.select('INBOX') |
| 12 | ustatus, uresponse = imap.uid('search', None, 'UNSEEN') |
| 13 | if ustatus == 'OK': |
| 14 | unread_msg_nums = uresponse[0].split() |
| 15 | else: |
| 16 | unread_msg_nums = [] |
| 17 | |
| 18 | fstatus, fresponse = imap.uid('search', None, 'FLAGGED') |
| 19 | if fstatus == 'OK': |
| 20 | flagged_msg_nums = fresponse[0].split() |
| 21 | else: |
| 22 | flagged_msg_nums = [] |
| 23 | |
| 24 | return [len(unread_msg_nums), len(flagged_msg_nums)] |
| 25 | |
| 26 | ping = os.system("ping " + mailsecrets.server + " -c1 > /dev/null 2>&1") |
| 27 | if ping == 0: |
| 28 | mails = getmails(mailsecrets.username, mailsecrets.password, mailsecrets.server) |
| 29 | text = '' |
| 30 | alt = '' |
| 31 | |
| 32 | if mails[0] > 0: |
| 33 | text = alt = str(mails[0]) |
| 34 | if mails[1] > 0: |
| 35 | alt = str(mails[1]) + " " + alt |
| 36 | else: |
| 37 | exit(1) |
| 38 | |
| 39 | print('{"text":"' + text + '", "alt": "' + alt + '"}') |
| 40 | |
| 41 | else: |
| 42 | exit(1) |