Advertisement
Guest User

Gmail checker

a guest
May 16th, 2013
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. import email, getpass, imaplib, os, time
  2.  
  3. # Declare functions here
  4. def move_forward():
  5.         print "MOVE FORWARD"
  6. def move_backward():
  7.         print "MOVE BACKWARD"
  8. def move_left():
  9.         print "MOVE LEFT"
  10. def move_right():
  11.         print "MOVE RIGHT"
  12.  
  13. # END of functions declarations
  14.  
  15. user = "USERNAME" #Change this
  16. pwd = "PASSWORD" #Change this
  17.  
  18. m = imaplib.IMAP4_SSL("imap.gmail.com")
  19. m.login(user,pwd)
  20. #m.select("Inbox")
  21. #unread_messages = m.status('INBOX', "(UNSEEN)")
  22. #unreadcount = unread_messages[0].split()[2].strip(').,]')
  23. var = 1
  24. while var == 1:
  25.         os.system('clear')
  26.         m.select("Inbox")
  27.         status, unreadcount = m.status('INBOX', "(UNSEEN)")
  28.         unreadcount = int(unreadcount[0].split()[2].strip(').,]'))
  29.         print "Unread Messages: " + str(unreadcount)
  30.         if unreadcount > 0:
  31.                 items = m.search(None, "UNSEEN")
  32.                 #print items
  33.                 items = str(items[1]).strip('[\']').split(' ')
  34.                 #items = items[1].split(' ')
  35.                 #print items
  36.                 for index, emailid in enumerate(items):
  37.                         #print "emailid: " + emailid
  38.                         resp, data = m.fetch(emailid, "(RFC822)")
  39.                         #print str(data) + "\n"
  40.                         email_body = data[0][1]
  41.                         mail = email.message_from_string(email_body)
  42.  
  43.                         if mail["Subject"] == 'robot instructions':
  44.                                 print "From: " + mail["From"] + "\n"
  45.                                 print "Subject: " + mail["Subject"] + "\n\n"
  46.                                 for part in mail.walk():
  47.                                         if part.get_content_type() == 'text/plain':
  48.                                                 body = part.get_payload()
  49.                                                 #  For each line in message execute instructions
  50.                                                 counter = 1
  51.                                                 for line in body.split('\r\n'):
  52.                                                         counter = counter + 1
  53.                                                         #print "Instruction " + str(counter) + ": " + line
  54.                                                         if line == "forward":
  55.                                                                 move_forward()
  56.                                                         elif line == "backward":
  57.                                                                 move_backward()
  58.                                                         elif line == "left":
  59.                                                                 move_left()
  60.                                                         elif line == "right":
  61.                                                                 move_right()
  62.         time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement