Created
April 10, 2015 17:44
-
-
Save anandology/fcdbe15d3ef2163f76a8 to your computer and use it in GitHub Desktop.
Script to find the number of emails in the given gmail account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Script to count the emails received in a gmail account and posts it to a web hook. | |
| """ | |
| import imaplib | |
| import optparse | |
| import json | |
| import urllib2 | |
| def count_emails(username, password, folder="INBOX"): | |
| """Returns the number of emails in specified folder. | |
| """ | |
| client = imaplib.IMAP4_SSL('imap.gmail.com','993') | |
| client.login(username, password) | |
| count = client.select(folder) | |
| return count[1][0] | |
| def main(): | |
| p = optparse.OptionParser() | |
| p.add_option("--email", help="E-mail address of the gmail account to find the count") | |
| p.add_option("--password", help="Password of the gmail account") | |
| p.add_option("--password-file", help="Path to the file containing the password") | |
| p.add_option("--label", help="Name of the label/folder to find message count (default: %default)", default="INBOX") | |
| p.add_option("--post-hook", help="URL of the webhook to POST the count") | |
| options, args = p.parse_args() | |
| if not options.email: | |
| raise Exception("Please provide the email address to check the count") | |
| if not options.password and not options.password_file: | |
| raise Exception("Please provide either password or password-file.") | |
| username = options.email | |
| if options.password: | |
| pw = options.password | |
| else: | |
| pw = open(options.password_file).read().strip() | |
| count = count_emails(username, pw, options.label) | |
| print count | |
| if options.post_hook: | |
| jsontext = json.dumps({"email_count": count}) | |
| req = urllib2.Request(options.post_hook, jsontext, headers={"Content-Type": "application/json"}) | |
| urllib2.urlopen(req).read() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment