Monday 16 September 2019

GMAIL, subject checking using python.Using IMAP (Python3)


GMAIL, subject checking using python. I thought it would make an interesting post, and I will tell you why. When I was playing with the gmail notifier (about which I posted on earlier), I thought, if I can basically check if a new message has , and if it has, switch on a LED, I can pretty much make everything switch on (or off) over the internet when sending an email. I could for instance switch on an alarm system by sending an email to a gmail account with the code "344alarm2014-on" the python code would pull the email, check the subject an by some logical check switch on the device through GPIO. I can think of dozens of other applications, but let's stick with this one. Let's also stick with switching a LED on or off, to test the code. After that, whatever you want the code to invoke can be bolted on.




#source:http://bitsofpy.blogspot.com.au/2010/05/python-and-gmail-with-imap.html

import imaplib
username="someone@gmail.com"
password="somedude"

imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
imap_server.login(username, password)

imap_server.select('INBOX')


# Count the unread emails
status, response = imap_server.status('INBOX', "(UNSEEN)")
unreadcount = int(response[0].split()[2].strip(').,]'))
print unreadcount

#Now I'm not saying this is a particularly nice way of doing #this, but if you print the response and reverse engineer 
#it you will see how I arrived with that string parsing. 
#Regex would be another option, but I try avoid that unless it is #required. 
#Now lets get a list of the identifiers for each unread message, #I'm going to call it email_ids:

# Search for all new mail
status, email_ids = imap_server.search(None, '(UNSEEN)')
print (email_ids)

def get_emails(email_ids):
    data = []
    for e_id in email_ids:
        _, response = imap_server.fetch(e_id, '(UID BODY[TEXT])')
        data.append(response[0][1])
    return data

def get_subjects(email_ids):
    subjects = []
    for e_id in email_ids:
        _, response = imap_server.fetch(e_id, '(body[header.fields (subject)])')
        subjects.append( response[0][1][9:] )
    return subjects

#And I often search for emails from someone in particular, I can #do that easily from Python as well:

def emails_from(name):
    '''Search for all mail from name'''
    status, response = imap_server.search(None, '(FROM"%s")'%name)
    email_ids = [e_id for e_id in response[0].split()]
    print ('Number of emails from %s: %i. IDs: %s' % (name, len(email_ids), email_ids))
    return email_ids



something worth checking:
#!/usr/bin/env python

import subprocess, feedparser

DEBUG = 1

USERNAME = "myusername"     # just the part before the @ sign, add yours here
PASSWORD = "mypassword"     

NEWMAIL_OFFSET = 0        # my unread messages goes to zero, yours might not
MAIL_CHECK_FREQ = 60      # check mail every 60 seconds


while True:

        newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom/sunset")["feed"]["fullcount"])

        if newmails > NEWMAIL_OFFSET:
                subprocess.call(["python relay-test.py",],shell=True)
        else:
                pass

        time.sleep(MAIL_CHECK_FREQ)


background info:


http://bruno.im/2010/jul/29/europython-talk-python-and-imap-protocol/

https://pythonadventures.wordpress.com/tag/gmail/


gmail python library:

https://github.com/charlierguo/gmail

Python 3, using Split (and Tuples)


Split [generic definition and capabilities]




The simplest way to use split is by defining a line of something, then split the words up, implicitly, using a space as delimiter:


>>> line="just some typing as an illustration"
>>> line.split()
['just', 'some', 'typing', 'as', 'an', 'illustration']

using str.split(" ") would have the same result; the sentence is turned into a list.


line="just some sentence to illustrate something"
words_list=line.split()
print(words_list)      

Or, for example print(word-list[2][1])  to print from word 2  (typing) the letter at position 1 (y) (remember couting starts at 0)





So I have created a csv file, using xls, see below. The good things about csv file is that it is built up by continuous rows, delimited by a comma. It is therefore no more than logical to use the split command to separate the data again.



summing up the company name, number of shares and price.




# File containing lines of the form "name,shares,price"
filename = "shares.csv"
portfolio = []
for line in open(filename):
    fields = line.split(",") # Split each line into a list using "," as the delimiter.
    name = fields[0] # Extract and convert individual fields
    shares = int(fields[1])
    price = float(fields[2])
    stock = (name,shares,price) # Create a tuple (name, shares, price)
    portfolio.append(stock) # Append to list of records



Call the tuple for instance:

>>>portfolio[0]
('Philips', 100, 100.5)



Python Google/Gmail API

In order to have something tap into your Google API, you need to set up OAuth. Now before I continue, I just to be able to tap into my Gmail account, using the python imaplib, I am no longer able to access the account through python anymore.  Google claims that the Gmail API is no substitution for imap, but i am not sure. So anyone who has some insights on imaplib and gmail and if it still works without OAuth, please leave a comment below.

Yes, so out of necessity I started looking into OAuth, python and Gmail, and it actually does not look that bad.


What is OAuth?

OAuth is essentially a way, to avoid having to write all sorts of authentication code into your application (in our case the pythin script).





I dont want to dwell on Oauth to much, as this post is more of an instruction on how to access the google GMAIL API. 



Instructions

1-Open google account or use existing one and go to Developers section.

2-https://developers.google.com/gmail/api/

3-Go to the Guides TAB (see below), which pretty much tells you everything I was gonna tell you but without the spelling mistakes.




4- run the pip install:


pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib


5-download the client configuration file, which contains the client ID and token. Put this in same directory as the python file you are about to add.

6. Create python script:


from __future__ import print_functionimport pickleimport os.pathfrom googleapiclient.discovery import buildfrom google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)

    # Call the Gmail API
    results = service.users().labels().list(userId='me').execute()
    labels = results.get('labels', [])

    if not labels:
        print('No labels found.')
    else:
        print('Labels:')
        for label in labels:
            print(label['name'])
if __name__ == '__main__':
    main()
from __future__ import print_functionimport pickleimport os.pathfrom googleapiclient.discovery import buildfrom google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] def main():     """Shows basic usage of the Gmail API.     Lists the user's Gmail labels.     """     creds = None     # The file token.pickle stores the user's access and refresh tokens, and is     # created automatically when the authorization flow completes for the first     # time.     if os.path.exists('token.pickle'):         with open('token.pickle', 'rb') as token:             creds = pickle.load(token)     # If there are no (valid) credentials available, let the user log in.     if not creds or not creds.valid:         if creds and creds.expired and creds.refresh_token:             creds.refresh(Request())         else:             flow = InstalledAppFlow.from_client_secrets_file(                 'credentials.json', SCOPES)             creds = flow.run_local_server(port=0)         # Save the credentials for the next run         with open('token.pickle', 'wb') as token:             pickle.dump(creds, token)     service = build('gmail', 'v1', credentials=creds)     # Call the Gmail API     results = service.users().labels().list(userId='me').execute()     labels = results.get('labels', [])     if not labels:         print('No labels found.')     else:         print('Labels:')         for label in labels:             print(label['name']) if __name__ == '__main__':     main()
from __future__ import print_functionimport pickleimport os.pathfrom googleapiclient.discovery import buildfrom google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] def main():     """Shows basic usage of the Gmail API.     Lists the user's Gmail labels.     """     creds = None     # The file token.pickle stores the user's access and refresh tokens, and is     # created automatically when the authorization flow completes for the first     # time.     if os.path.exists('token.pickle'):         with open('token.pickle', 'rb') as token:             creds = pickle.load(token)     # If there are no (valid) credentials available, let the user log in.     if not creds or not creds.valid:         if creds and creds.expired and creds.refresh_token:             creds.refresh(Request())         else:             flow = InstalledAppFlow.from_client_secrets_file(                 'credentials.json', SCOPES)             creds = flow.run_local_server(port=0)         # Save the credentials for the next run         with open('token.pickle', 'wb') as token:             pickle.dump(creds, token)     service = build('gmail', 'v1', credentials=creds)     # Call the Gmail API     results = service.users().labels().list(userId='me').execute()     labels = results.get('labels', [])     if not labels:         print('No labels found.')     else:         print('Labels:')         for label in labels:             print(label['name']) if __name__ == '__main__':     main()

Now when you run the script, it will generate an authorization link:






This link is basically a confirmation authorization, i.e. grant authorization to the application to access the API. I will prompt you to grant access to the python "application" to be able to access the gmail account.



This is a once off, after this, tokens will be exchanged.

For access to your API and to down oad the client json file:


https://console.developers.google.com


This particular python code, prints all the labels in the gmail account.