Amazing-Python-Scripts

Форк
0
48 строк · 2.1 Кб
1
import ezgmail
2

3

4
def attachmentdownload(resulthreads):
5
    # Two Objects used in code are GmailThread and GmailMessage
6
    # 1.  GmailThread - Represents conversation threads
7
    # 2.  GmailMessage - Represents individual emails within Threads
8
    countofresults = len(resulthreads)
9
    try:
10
        for i in range(countofresults):
11
            # checks whether the count of messages in threads is greater than 1
12
            if len(resulthreads[i].messages) > 1:
13
                for j in range(len(resulthreads[i].messages)):
14
                    resulthreads[i].messages[
15
                        j].downloadAllAttachments()  # downloads attachment(s) for individual messages
16
            else:
17
                # downloads attachment(s) for single message
18
                resulthreads[i].messages[0].downloadAllAttachments()
19
        print("Download compelete. Please check your root directory.")
20
    except:
21
        raise Exception("Error occured while downloading attachment(s).")
22

23

24
if __name__ == '__main__':
25
    query = input("Enter search query: ")
26
    # appending to make sure the result threads always has an attachment
27
    newquery = query + " + has:attachment"
28
    # search functions accepts all the operators described at https://support.google.com/mail/answer/7190?hl=en
29
    resulthreads = ezgmail.search(newquery)
30

31
    if len(resulthreads) == 0:
32
        # Executed if results don't have attachment
33
        print("Result has no attachments:")
34
    else:
35
        print("Result(s) with attachments:")
36
        for threads in resulthreads:
37
            # prints the subject line of email thread in results
38
            print(f"Email Subject: {threads.messages[0].subject}")
39
        try:
40
            ask = input(
41
                "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
42
            if ask == "Yes":
43
                # calls the function that downloads attachment(s)
44
                attachmentdownload(resulthreads)
45
            else:
46
                print("Program exited")
47
        except:
48
            print("Something went wrong")
49

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.