forked from marin117/Gmail-deleter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleter.py
178 lines (166 loc) · 7.46 KB
/
deleter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys
from argparse import ArgumentParser
from itertools import tee
from gmail_deleter import GmailHandler
def run(args):
gmail = GmailHandler(
credentials_filepath=args.credentials_file, token_filepath=args.token_file
)
while True:
print(
"\n1. Print authenticated user's email address and total number of messages in the mailbox."
"\n2. Print a list of all labels associated with the authenticated user's mailbox."
"\n3. Delete messages with a specified label."
"\n4. Delete messages with a specified Gmail search filter (e.g. 'from:[email protected]')"
"\n5. Delete all messages in Trash."
"\n6. Delete all messages in Spam."
"\n7. Quit."
"\n"
)
try:
choice = int(input("Choose from the listed options: "))
if choice == 1:
profile = gmail.get_user_profile(user_id="me")
print(
f"\n[*] You are authenticated as {profile['emailAddress']}"
f"\n[+] Found {profile['messagesTotal']} messages in the mailbox"
)
elif choice == 2:
labels = gmail.get_labels(user_id="me")
print(
f"\n[+] Found the following labels in the mailbox:\n"
f"{[i['id'] for i in labels]}\n"
)
elif choice == 3:
labels = input(
"\nPlease specify which label(s) you want to delete (comma separated if more than one): "
)
labels = labels.upper().split(",")
messages = gmail.list_messages_with_label(
user_id="me", label_ids=labels
)
delete_type = str(
input("\nSoft (move to Trash) or Hard (permanent) Delete: ")
)
try:
if "soft" in delete_type.lower():
for message in messages:
delete = gmail.delete_message(
user_id="me", msg_id=message["id"], soft_delete=True
)
print(
f"\n[!] Soft deleted messages that were labeled with the following labels: {labels}"
)
elif "hard" in delete_type.lower():
message_ids, message_ids_2 = tee(i["id"] for i in messages)
if sum(1 for _ in message_ids) < 1000:
batch_delete = gmail.batch_delete_messages(
user_id="me", msg_ids=list(message_ids_2)
)
else:
for id in message_ids_2:
delete = gmail.delete_message(
user_id="me", msg_id=id, soft_delete=False
)
print(
f"\n[!] Hard deleted messages that were labeled with the following labels: {labels}"
)
except ValueError:
print("Invalid input! Try again.")
elif choice == 4:
query = input(
"\nPlease specify a Gmail search filter for messages you want to delete: "
)
query_messages = gmail.list_messages_matching_query(
user_id="me", query=query
)
delete_type = str(
input("\nSoft (move to Trash) or Hard (permanent) Delete: ")
)
try:
if "soft" in delete_type.lower():
for message in query_messages:
delete = gmail.delete_message(
user_id="me", msg_id=message["id"], soft_delete=True
)
print(
f"\n[!] Soft deleted messages that matched query: {query}"
)
elif "hard" in delete_type.lower():
query_msg_ids, query_msg_ids_2 = tee(
i["id"] for i in query_messages
)
if sum(1 for _ in query_msg_ids) < 1000:
batch_delete = gmail.batch_delete_messages(
user_id="me", msg_ids=list(query_msg_ids_2)
)
else:
for id in query_msg_ids_2:
delete = gmail.delete_message(
user_id="me", msg_id=id, soft_delete=False
)
print(
f"\n[!] Hard deleted messages that matched query: {query}"
)
except ValueError:
print("Invalid input! Try again.")
elif choice == 5:
trash = gmail.list_messages_with_label(
user_id="me", label_ids=["TRASH"]
)
trash_ids, trash_ids_2 = tee(i["id"] for i in trash)
if sum(1 for _ in trash_ids) < 1000:
batch_delete = gmail.batch_delete_messages(
user_id="me", msg_ids=list(trash_ids_2)
)
else:
for id in trash_ids_2:
delete = gmail.delete_message(
user_id="me", msg_id=id, soft_delete=False
)
print("\n[!] Trash has been emptied!")
elif choice == 6:
spam = gmail.list_messages_with_label(user_id="me", label_ids=["SPAM"])
spam_ids, spam_ids_2 = tee(i["id"] for i in spam)
if sum(1 for _ in spam_ids) < 1000:
batch_delete = gmail.batch_delete_messages(
user_id="me", msg_ids=list(spam_ids_2)
)
else:
for id in spam_ids_2:
delete = gmail.delete_message(
user_id="me", msg_id=id, soft_delete=False
)
print("\n[!] Spam has been emptied!")
elif choice == 7:
sys.exit(1)
else:
print("\n[*] Invalid choice! Please try again...")
except ValueError:
print("\n[*] Invalid input! Please select from the following options...")
def main():
parser = ArgumentParser(
prog="Gmail Message Deleter",
description="No longer is there a need to manually delete your Gmail messages page by page! Programmatically clean your Gmail inbox and delete messages en masse by specific labels or Gmail search filter syntax!",
)
parser.add_argument(
"-c",
"--credentials",
dest="credentials_file",
help="location of credentials.json file (required for first run!)",
type=str,
)
parser.add_argument(
"-t",
"--token",
dest="token_file",
help="location of token.json file (once obtained, can be used to authenticate without supplying credentials.json)",
type=str,
)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
run(args)
if __name__ == "__main__":
main()