@@ -24,10 +24,11 @@ def clean(text):
2424 return "" .join (c if c .isalnum () else "_" for c in text )
2525
2626
27- def parse_parts (service , parts , folder_name ):
27+ def parse_parts (service , parts , folder_name , message ):
2828 """
2929 Utility function that parses the content of an email partition
3030 """
31+ print ("called parse_parts with folder_name:" , folder_name )
3132 if parts :
3233 for part in parts :
3334 filename = part .get ("filename" )
@@ -39,7 +40,8 @@ def parse_parts(service, parts, folder_name):
3940 if part .get ("parts" ):
4041 # recursively call this function when we see that a part
4142 # has parts inside
42- parse_parts (service , part .get ("parts" ), folder_name )
43+ print ("calling parse_parts with folder_name:" , folder_name )
44+ parse_parts (service , part .get ("parts" ), folder_name , message )
4345 if mimeType == "text/plain" :
4446 # if the email part is text plain
4547 if data :
@@ -66,15 +68,15 @@ def parse_parts(service, parts, folder_name):
6668 print ("Saving the file:" , filename , "size:" , get_size_format (file_size ))
6769 attachment_id = body .get ("attachmentId" )
6870 attachment = service .users ().messages () \
69- .attachments ().get (id = attachment_id , userId = 'me' , messageId = msg ['id' ]).execute ()
71+ .attachments ().get (id = attachment_id , userId = 'me' , messageId = message ['id' ]).execute ()
7072 data = attachment .get ("data" )
7173 filepath = os .path .join (folder_name , filename )
7274 if data :
7375 with open (filepath , "wb" ) as f :
7476 f .write (urlsafe_b64decode (data ))
7577
7678
77- def read_message (service , message_id ):
79+ def read_message (service , message ):
7880 """
7981 This function takes Gmail API `service` and the given `message_id` and does the following:
8082 - Downloads the content of the email
@@ -83,12 +85,13 @@ def read_message(service, message_id):
8385 - Downloads text/html content (if available) and saves it under the folder created as index.html
8486 - Downloads any file that is attached to the email and saves it in the folder created
8587 """
86- msg = service .users ().messages ().get (userId = 'me' , id = message_id ['id' ], format = 'full' ).execute ()
88+ msg = service .users ().messages ().get (userId = 'me' , id = message ['id' ], format = 'full' ).execute ()
8789 # parts can be the message body, or attachments
8890 payload = msg ['payload' ]
8991 headers = payload .get ("headers" )
9092 parts = payload .get ("parts" )
9193 folder_name = "email"
94+ has_subject = False
9295 if headers :
9396 # this section prints email basic info & creates a folder for the email
9497 for header in headers :
@@ -101,6 +104,8 @@ def read_message(service, message_id):
101104 # we print the To address
102105 print ("To:" , value )
103106 if name .lower () == "subject" :
107+ # make our boolean True, the email has "subject"
108+ has_subject = True
104109 # make a directory with the name of the subject
105110 folder_name = clean (value )
106111 # we will also handle emails with the same subject name
@@ -119,8 +124,12 @@ def read_message(service, message_id):
119124 if name .lower () == "date" :
120125 # we print the date when the message was sent
121126 print ("Date:" , value )
122-
123- parse_parts (service , parts , folder_name )
127+ if not has_subject :
128+ # if the email does not have a subject, then make a folder with "email" name
129+ # since folders are created based on subjects
130+ if not os .path .isdir (folder_name ):
131+ os .mkdir (folder_name )
132+ parse_parts (service , parts , folder_name , message )
124133 print ("=" * 50 )
125134
126135if __name__ == "__main__" :
0 commit comments