-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel_manager.py
More file actions
46 lines (39 loc) · 1.41 KB
/
label_manager.py
File metadata and controls
46 lines (39 loc) · 1.41 KB
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
from googleapiclient.discovery import Resource
PHISHING_LABEL_NAME = "⚠️ PHISHING ALERT"
def get_or_create_label(service: Resource, label_name: str = PHISHING_LABEL_NAME) -> str:
"""
Checks if a Gmail label exists. Creates it if not.
Returns:
str: The label ID
"""
label_id = None
try:
response = service.users().labels().list(userId='me').execute()
labels = response.get('labels', [])
for label in labels:
if label['name'].lower() == label_name.lower():
label_id = label['id']
break
if not label_id:
label_body = {
'name': label_name,
'labelListVisibility': 'labelShow',
'messageListVisibility': 'show'
}
created_label = service.users().labels().create(userId='me', body=label_body).execute()
label_id = created_label['id']
except Exception as e:
print(f"[!] Error getting/creating label: {e}")
return label_id
def apply_label(service: Resource, msg_id: str, label_id: str):
"""
Applies a Gmail label to a specific email message.
"""
try:
service.users().messages().modify(
userId='me',
id=msg_id,
body={'addLabelIds': [label_id]}
).execute()
except Exception as e:
print(f"[!] Error applying label to message {msg_id}: {e}")