|
1 | 1 | import ezgmail
|
2 | 2 |
|
3 | 3 | def attachmentdownload(resulthreads):
|
| 4 | + # Two Objects used in code are GmailThread and GmailMessage |
| 5 | + # 1. GmailThread - Represents conversation threads |
| 6 | + # 2. GmailMessage - Represents individual emails within Threads |
| 7 | + countofresults = len(resulthreads) |
4 | 8 | try:
|
5 | | - for i in range(len(resulthreads)): |
6 | | - if len(resulthreads[i].messages) > 1: |
| 9 | + for i in range(countofresults): |
| 10 | + if len(resulthreads[i].messages) > 1:# checks whether the count of messages in threads is greater than 1 |
7 | 11 | for j in range(len(resulthreads[i].messages)):
|
8 | | - resulthreads[i].messages[j].downloadAllAttachments() |
| 12 | + resulthreads[i].messages[ |
| 13 | + j].downloadAllAttachments() # downloads attachment(s) for individual messages |
9 | 14 | else:
|
10 | | - resulthreads[i].messages[0].downloadAllAttachments() |
| 15 | + resulthreads[i].messages[0].downloadAllAttachments() # downloads attachment(s) for single message |
| 16 | + print("Download compelete. Please check your root directory.") |
11 | 17 | except:
|
12 | 18 | raise Exception("Error occured while downloading attachment(s).")
|
13 | | - finally: |
14 | | - print("Download compelete. Please check your root directory.") |
| 19 | + |
15 | 20 |
|
16 | 21 | if __name__ == '__main__':
|
17 | 22 | query = input("Enter search query: ")
|
18 | | - newquery = query + " + has:attachment" |
19 | | - resulthreads = ezgmail.search(newquery) |
| 23 | + newquery = query + " + has:attachment"# appending to make sure the result threads always has an attachment |
| 24 | + resulthreads = ezgmail.search(newquery)# search functions accepts all the operators described at https://support.google.com/mail/answer/7190?hl=en |
20 | 25 |
|
21 | 26 | if len(resulthreads) == 0:
|
22 | | - print("Results have no attachments") |
| 27 | + print("Result has no attachments")# Executed if results don't have attachment |
23 | 28 | else:
|
24 | | - print("Results: ") |
| 29 | + print("Result(s): ") |
25 | 30 | for threads in resulthreads:
|
26 | | - print(f"Email Subject: {threads.messages[0].subject}") |
| 31 | + print(f"Email Subject: {threads.messages[0].subject}")# prints the subject line of email thread in results |
27 | 32 | try:
|
28 | | - ask = input("Do you want to download attachment(s) in results (Yes/No)? ") |
| 33 | + ask = input( |
| 34 | + "Do you want to download attachment(s) in result(s) (Yes/No)? ") # Allows user to decide whether they want to download attachment(s) or not |
29 | 35 | if ask == "Yes":
|
30 | | - attachmentdownload(resulthreads) |
| 36 | + attachmentdownload(resulthreads)# calls the function that downloads attachment(s) |
31 | 37 | else:
|
32 | 38 | print("Program exited")
|
33 | 39 | except:
|
|
0 commit comments