teagmail/Gmail.py
def unpackPayload(self, payloads, bodies=True,
                      types=['html', 'plain'], log=False, x=0, depth=0):
        """
        recurses through message parts until it find the types
        specified, then returns the decoded part and appends it to a
        list/dictionary of all messages parts

        Args:
                payloads (dictionary): The returned object from the
                `getPayload` function

                bodies (bool): True to collect html or plain text bodies
                from payload, false for simply headers

                types (:obj:`list` of :obj:`str`): types of bodies to be
                returned, either `html` or `plain`

                log (bool): True to log output to ./logs directory

                *DO NOT ALTER THE FOLLOWING PARAMETERS, THEY ARE REQUIRED
                FOR THE RECURSION*
                x (int): Specifies the index of the payloads
                depth (int): Specifies the Depth of recursion
        """
        if payloads is None:
            return None
        else:
            messageParts = []
            plain = False
            plainText = ''
            for msg in payloads:
                parts = {}
                if bodies:
                    type = msg['mimeType']
                    # Plain text
                    if type == 'text/plain' and 'plain' in types:
                        plainText = self.decodeMSG(msg['body']['data'])
                        if depth == 0:
                            parts['plain'] = plainText[:20]
                        else:
                            plain = True
                    # Html
                    elif type == 'text/html' and 'html' in types:
                        plain = False
                        body = self.decodeMSG(msg['body']['data'])
                        if depth > 0:
                            return {"html": body}
                        parts['html'] = body
                    # Other
                    elif type[10:] in ['mixed', 'alternative',
                                       'related', 'report']:
                        msgParts = msg['parts']
                        parts = self.unpackPayload(msgParts, bodies=True,
                                                   types=types, x=x, depth=depth+1)
                        if depth > 0:
                            return parts
                    # Attatchment
                    elif type[:10] == 'application':
                        pass
                # Headers
                if depth == 0:
                    #print(parts, 'X:', x)
                    parts['id'] = msg['id']
                    header = msg['headers']
                    for head in header:
                        name, value = head['name'], head['value']
                        if name == 'Date':
                            parts['dateTime'] = value
                        if name == 'Subject':
                            parts['Subject'] = value
                        if name == 'From':
                            if '<' in value:
                                FROM, EMAIL = value.split('<')
                                parts['From'] = FROM[:-1]
                                parts['From-email'] = EMAIL[:-1]
                            else:
                                parts['From-email'] = value
                messageParts.append(parts)
                x += 1
            if plain and depth > 0:
                plain = False
                return {"plain": plainText[:20]}  # TODO remove :20 ?
            # adds plain message if no html

            # TODO add msg['type']
                """
        if log:   # TODO add logging library instead
            debug.createLog(dir='logs/bodies')
            debug.writeLog("mockData" if bodies else "headers",
                           messageParts)
            if bodies:
                for (index, msg) in enumerate(messageParts):
                    if 'html' in msg:
                        debug.writeLog(str(index+1), msg['html'],
                                       'bodies/', extension="html")
                    else:
                        debug.writeLog(str(index+1), msg['plain'],
                                       'bodies/', extension="txt")
                    """
            return messageParts

        recurses through message parts until it find the types
        specified, then returns the decoded part and appends it to a
        list/dictionary of all messages parts

        Args:
                payloads (dictionary): The returned object from the
                `getPayload` function

                bodies (bool): True to collect html or plain text bodies
                from payload, false for simply headers

                types (:obj:`list` of :obj:`str`): types of bodies to be
                returned, either `html` or `plain`

                log (bool): True to log output to ./logs directory

                *DO NOT ALTER THE FOLLOWING PARAMETERS, THEY ARE REQUIRED
                FOR THE RECURSION*
                x (int): Specifies the index of the payloads
                depth (int): Specifies the Depth of recursion
        
recurses through message parts until it find the types
specified, then returns the decoded part and appends it to a
list/dictionary of all messages parts

payloads (dictionary): The returned object from the
`getPayload` function

bodies (bool): True to collect html or plain text bodies
from payload, false for simply headers



log (bool): True to log output to ./logs directory


recurses through message parts until it find the types specified then returns the decoded part and appends it to a list / dictionary of all messages parts payloads ( dictionary ) : The returned object from the ` getPayload ` function bodies ( bool ) : True to collect html or plain text bodies from payload false for simply headers log ( bool ) : True to log output to . / logs directory
def unpackPayload(self, payloads, bodies=True,
                      types=['html', 'plain'], log=False, x=0, depth=0):
        if payloads is None:
            return None
        else:
            messageParts = []
            plain = False
            plainText = ''
            for msg in payloads:
                parts = {}
                if bodies:
                    type = msg['mimeType']
                    if type == 'text/plain' and 'plain' in types:
                        plainText = self.decodeMSG(msg['body']['data'])
                        if depth == 0:
                            parts['plain'] = plainText[:20]
                        else:
                            plain = True
                    elif type == 'text/html' and 'html' in types:
                        plain = False
                        body = self.decodeMSG(msg['body']['data'])
                        if depth > 0:
                            return {"html": body}
                        parts['html'] = body
                    elif type[10:] in ['mixed', 'alternative',
                                       'related', 'report']:
                        msgParts = msg['parts']
                        parts = self.unpackPayload(msgParts, bodies=True,
                                                   types=types, x=x, depth=depth+1)
                        if depth > 0:
                            return parts
                    elif type[:10] == 'application':
                        pass
                if depth == 0:
                    parts['id'] = msg['id']
                    header = msg['headers']
                    for head in header:
                        name, value = head['name'], head['value']
                        if name == 'Date':
                            parts['dateTime'] = value
                        if name == 'Subject':
                            parts['Subject'] = value
                        if name == 'From':
                            if '<' in value:
                                FROM, EMAIL = value.split('<')
                                parts['From'] = FROM[:-1]
                                parts['From-email'] = EMAIL[:-1]
                            else:
                                parts['From-email'] = value
                messageParts.append(parts)
                x += 1
            if plain and depth > 0:
                plain = False
                return {"plain": plainText[:20]}  
            return messageParts
def unpackPayload ( self , payloads , bodies = True , types = [ 'html' , 'plain' ] , log = False , x = 0 , depth = 0 ) : if payloads is None : return None else : messageParts = [ ] plain = False plainText = '' for msg in payloads : parts = { } if bodies : type = msg [ 'mimeType' ] if type == 'text/plain' and 'plain' in types : plainText = self . decodeMSG ( msg [ 'body' ] [ 'data' ] ) if depth == 0 : parts [ 'plain' ] = plainText [ : 20 ] else : plain = True elif type == 'text/html' and 'html' in types : plain = False body = self . decodeMSG ( msg [ 'body' ] [ 'data' ] ) if depth > 0 : return { "html" : body } parts [ 'html' ] = body elif type [ 10 : ] in [ 'mixed' , 'alternative' , 'related' , 'report' ] : msgParts = msg [ 'parts' ] parts = self . unpackPayload ( msgParts , bodies = True , types = types , x = x , depth = depth + 1 ) if depth > 0 : return parts elif type [ : 10 ] == 'application' : pass if depth == 0 : parts [ 'id' ] = msg [ 'id' ] header = msg [ 'headers' ] for head in header : name , value = head [ 'name' ] , head [ 'value' ] if name == 'Date' : parts [ 'dateTime' ] = value if name == 'Subject' : parts [ 'Subject' ] = value if name == 'From' : if '<' in value : FROM , EMAIL = value . split ( '<' ) parts [ 'From' ] = FROM [ : - 1 ] parts [ 'From-email' ] = EMAIL [ : - 1 ] else : parts [ 'From-email' ] = value messageParts . append ( parts ) x += 1 if plain and depth > 0 : plain = False return { "plain" : plainText [ : 20 ] } """
        if log:   # TODO add logging library instead
            debug.createLog(dir='logs/bodies')
            debug.writeLog("mockData" if bodies else "headers",
                           messageParts)
            if bodies:
                for (index, msg) in enumerate(messageParts):
                    if 'html' in msg:
                        debug.writeLog(str(index+1), msg['html'],
                                       'bodies/', extension="html")
                    else:
                        debug.writeLog(str(index+1), msg['plain'],
                                       'bodies/', extension="txt")
                    """ return messageParts
recurses through message parts until it find the types
specified, then returns the decoded part and appends it to a
list/dictionary of all messages parts
recurses through message parts until it find the types specified then returns the decoded part and appends it to a list / dictionary of all messages parts
"""
        recurses through message parts until it find the types
        specified, then returns the decoded part and appends it to a
        list/dictionary of all messages parts

        Args:
                payloads (dictionary): The returned object from the
                `getPayload` function

                bodies (bool): True to collect html or plain text bodies
                from payload, false for simply headers

                types (:obj:`list` of :obj:`str`): types of bodies to be
                returned, either `html` or `plain`

                log (bool): True to log output to ./logs directory

                *DO NOT ALTER THE FOLLOWING PARAMETERS, THEY ARE REQUIRED
                FOR THE RECURSION*
                x (int): Specifies the index of the payloads
                depth (int): Specifies the Depth of recursion
        """ # Plain text # Html # Other # Attatchment # Headers #print(parts, 'X:', x) # TODO remove :20 ? # adds plain message if no html # TODO add msg['type'] """
        if log:   # TODO add logging library instead
            debug.createLog(dir='logs/bodies')
            debug.writeLog("mockData" if bodies else "headers",
                           messageParts)
            if bodies:
                for (index, msg) in enumerate(messageParts):
                    if 'html' in msg:
                        debug.writeLog(str(index+1), msg['html'],
                                       'bodies/', extension="html")
                    else:
                        debug.writeLog(str(index+1), msg['plain'],
                                       'bodies/', extension="txt")
                    """
self
payloads
bodies
types
log
x
depth