Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # Check mail and trigger the blink(1) when new mail is found
  2.  
  3. import os, sys, imaplib, email, getopt, time
  4. from subprocess import Popen, DEVNULL
  5.  
  6. class MailBlinker():
  7.  
  8. def __init__(self, quiet=False):
  9. # GMail username/password
  10. self.username = 'your_gmail_username'
  11. self.password = 'your_gmail_password'
  12.  
  13. # Location of the blink1control-tool
  14. # (make sure API is enabled in blink1Control GUI)
  15. self.blinker_tool = 'C:/Program Files/Blink1Control/blink1control-tool.exe'
  16.  
  17. # How many seconds between checks?
  18. self.mail_check_interval = 30
  19.  
  20. # Should access be read-only? (If False, then blink(1) will only be triggered once, the first time
  21. # a new message is seen. If True, blink(1) will see the new message every time it checks until the
  22. # mail is marked as "seen" in another mail reader.)
  23. self.inbox_read_only = True
  24.  
  25. # The mail connection handler
  26. self.mailconn = None
  27.  
  28. # Should we print any output?
  29. self.quiet = quiet
  30.  
  31. ########################################################################
  32.  
  33. # Connect to GMail
  34. def connect(self):
  35. self.mailconn = imaplib.IMAP4_SSL('imap.gmail.com')
  36. (retcode, capabilities) = self.mailconn.login(self.username, self.password)
  37. self.mailconn.select('inbox', readonly=self.inbox_read_only)
  38.  
  39. # Check for mail
  40. def check_mail(self):
  41. # Connect to GMail (fresh connection needed every time)
  42. self.connect()
  43.  
  44. num_new_messages=0
  45. (retcode, messages) = self.mailconn.search(None, '(UNSEEN)')
  46. if retcode == 'OK':
  47.  
  48. for num in messages[0].split() :
  49. num_new_messages += 1
  50. if not self.quiet:
  51. print(' Processing #{}'.format(num_new_messages))
  52. typ, data = self.mailconn.fetch(num,'(RFC822)')
  53. for response_part in data:
  54. if isinstance(response_part, tuple):
  55. original = email.message_from_bytes(response_part[1])
  56. if not self.quiet:
  57. print(" ", original['From'])
  58. print(" ", original['Subject'])
  59. typ, data = self.mailconn.store(num,'+FLAGS','\\Seen')
  60.  
  61. return num_new_messages
  62.  
  63. # Blink!
  64. def blink(self):
  65. cmd = "{} --rgb=#FFFFFF --blink 10 -m 150 -t 300".format(self.blinker_tool) # -t should be about 2x the speed of -m
  66. if self.quiet:
  67. stdout = DEVNULL
  68. stderr = DEVNULL
  69. else:
  70. stdout = None # to screen
  71. stderr = None # to screen
  72. Popen(cmd, stdout=stdout, stderr=stderr)
  73.  
  74. ########################################################################
  75.  
  76. if __name__ == '__main__':
  77.  
  78. # Specify -q for quiet operation (no output)
  79. opts, args = getopt.getopt(sys.argv[1:], 'q')
  80. quiet = False
  81. for opt, arg in opts:
  82. if opt == '-q':
  83. quiet = True
  84.  
  85. mb = MailBlinker(quiet)
  86. while True:
  87. if not quiet:
  88. print("Checking... ", end='')
  89. num_new_messages = mb.check_mail()
  90. if not quiet:
  91. print("{} new messages".format(num_new_messages))
  92. if num_new_messages > 0:
  93. mb.blink()
  94. time.sleep(mb.mail_check_interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement