Guest User

Show unread E-Mails on Raspberry Pi

a guest
Feb 15th, 2016
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. # Importing required Libraries
  2. import imaplib, time, RPi.GPIO as GPIO
  3.  
  4. # Variables
  5. username = "YOUR GOOGLE ACCOUNT EMAIL"
  6. password = "YOUR GOOGLE ACCOUNT PASSWORD"
  7.  
  8. def init():
  9.     print("[*] Initialising Connection to IMAP-Server...")
  10.     mail = imaplib.IMAP4_SSL('imap.gmail.com')
  11.     mail.login(username, password)
  12.     print("[+] Selecting Inbox...")
  13.  
  14.     print("[*] Initialising GPIOs...")
  15.     GPIO.setmode(GPIO.BCM)
  16.     GPIO.setup(17, GPIO.OUT)
  17.     GPIO.setup(27, GPIO.OUT)
  18.     GPIO.setup(22, GPIO.OUT)
  19.    
  20.     return mail
  21.  
  22. def stage1():
  23.     print("[+] Selected STAGE1")
  24.     GPIO.output(17, 1)
  25.  
  26. def stage2():
  27.     print("[+] Selected STAGE2")
  28.     GPIO.output(27, 1)
  29.  
  30. def stage3():
  31.     print("[+] Selected STAGE3")
  32.     GPIO.output(22, 1)
  33.  
  34. def dark():
  35.     GPIO.output(17, 0)
  36.     GPIO.output(27, 0)
  37.     GPIO.output(22, 0)
  38.  
  39. def retrMail(obj):
  40.     print("[*] Retrieving new unread Mails... ", end="")
  41.     obj.select("inbox")
  42.     return len(obj.search(None, 'Unseen')[1][0].split())
  43.  
  44. def analyze(new):
  45.     if new >= 1 and new <= 3:
  46.         dark()
  47.         stage1()
  48.     elif new >= 4 and new <= 10:
  49.         dark()
  50.         stage1()
  51.         stage2()
  52.     elif new >= 10:
  53.         dark()
  54.         stage1()
  55.         stage2()
  56.         stage3()
  57.     else:
  58.         print("[-] No unread Mails. Switching to dark")
  59.         dark()
  60.  
  61. def main():
  62.     obj = init()
  63.     new = retrMail(obj)
  64.     print(new)
  65.     analyze(new)
  66.  
  67.     try:
  68.         while 1:
  69.             time.sleep(660)     # 11 Minutes = 660 Seconds
  70.             new = retrMail(obj)
  71.             print(new)
  72.             analyze(new)
  73.     except KeyboardInterrupt:
  74.         GPIO.cleanup()
  75.         exit(0)
  76.  
  77. main()
Add Comment
Please, Sign In to add comment