| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,10 +24,11 @@ def clean(text): | |||
| 24 | 24 | return "".join(c if c.isalnum() else "_" for c in text) | |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | - def parse_parts(service, parts, folder_name): | ||
| 27 | + def parse_parts(service, parts, folder_name, message): | ||
| 28 | 28 | """ | |
| 29 | 29 | Utility function that parses the content of an email partition | |
| 30 | 30 | """ | |
| 31 | + print("called parse_parts with folder_name:", folder_name) | ||
| 31 | 32 | if parts: | |
| 32 | 33 | for part in parts: | |
| 33 | 34 | filename = part.get("filename") | |
@@ -39,7 +40,8 @@ def parse_parts(service, parts, folder_name): | |||
| 39 | 40 | if part.get("parts"): | |
| 40 | 41 | # recursively call this function when we see that a part | |
| 41 | 42 | # 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) | ||
| 43 | 45 | if mimeType == "text/plain": | |
| 44 | 46 | # if the email part is text plain | |
| 45 | 47 | if data: | |
@@ -66,15 +68,15 @@ def parse_parts(service, parts, folder_name): | |||
| 66 | 68 | print("Saving the file:", filename, "size:", get_size_format(file_size)) | |
| 67 | 69 | attachment_id = body.get("attachmentId") | |
| 68 | 70 | 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() | ||
| 70 | 72 | data = attachment.get("data") | |
| 71 | 73 | filepath = os.path.join(folder_name, filename) | |
| 72 | 74 | if data: | |
| 73 | 75 | with open(filepath, "wb") as f: | |
| 74 | 76 | f.write(urlsafe_b64decode(data)) | |
| 75 | 77 | ||
| 76 | 78 | ||
| 77 | - def read_message(service, message_id): | ||
| 79 | + def read_message(service, message): | ||
| 78 | 80 | """ | |
| 79 | 81 | This function takes Gmail API `service` and the given `message_id` and does the following: | |
| 80 | 82 | - Downloads the content of the email | |
@@ -83,12 +85,13 @@ def read_message(service, message_id): | |||
| 83 | 85 | - Downloads text/html content (if available) and saves it under the folder created as index.html | |
| 84 | 86 | - Downloads any file that is attached to the email and saves it in the folder created | |
| 85 | 87 | """ | |
| 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() | ||
| 87 | 89 | # parts can be the message body, or attachments | |
| 88 | 90 | payload = msg['payload'] | |
| 89 | 91 | headers = payload.get("headers") | |
| 90 | 92 | parts = payload.get("parts") | |
| 91 | 93 | folder_name = "email" | |
| 94 | + has_subject = False | ||
| 92 | 95 | if headers: | |
| 93 | 96 | # this section prints email basic info & creates a folder for the email | |
| 94 | 97 | for header in headers: | |
@@ -101,6 +104,8 @@ def read_message(service, message_id): | |||
| 101 | 104 | # we print the To address | |
| 102 | 105 | print("To:", value) | |
| 103 | 106 | if name.lower() == "subject": | |
| 107 | + # make our boolean True, the email has "subject" | ||
| 108 | + has_subject = True | ||
| 104 | 109 | # make a directory with the name of the subject | |
| 105 | 110 | folder_name = clean(value) | |
| 106 | 111 | # we will also handle emails with the same subject name | |
@@ -119,8 +124,12 @@ def read_message(service, message_id): | |||
| 119 | 124 | if name.lower() == "date": | |
| 120 | 125 | # we print the date when the message was sent | |
| 121 | 126 | 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) | ||
| 124 | 133 | print("="*50) | |
| 125 | 134 | ||
| 126 | 135 | if __name__ == "__main__": | |
| Back | FazBrowse Home | New Git URL |
0 commit comments