-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail.py
58 lines (55 loc) · 2.28 KB
/
gmail.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
from gemini_agent import summarize_email
from simplegmail import Gmail
from simplegmail.query import construct_query
from notion import insert_to_notion
from get_course_info import get_course_info
gmail = Gmail()
def receive_email():
query_params = {
'newer_than': (30, 'hour'),
'older_than': (0, 'hour'),
# 'unread': True,
'sender': ['e3@nycu.edu.tw'],
'exclude_sender': [['registra@nycu.edu.tw'], ['noreply@nycu.edu.tw']],
'exclude_labels': [['To_Notion']]
}
messages = gmail.get_messages(query=construct_query(query_params))
labels = gmail.list_labels()
# To find a label by the name that you know (just an example):
notion_label = list(filter(lambda x: x.name == 'To_Notion', labels))[0]
print(len(messages))
split_syb = '---------------------------------------------------------------------'
filters = ['\"數位教學平台 E3\"', '\"E3數位教學平台\"', '\"E3 數位教學平台 公告\"']
for message in messages:
# message.mark_as_read()
message.add_label(notion_label)
# filter e3 announcement
SENDER = message.sender.split(" <")
if SENDER[0] in filters:
continue
summary = ""
course_id = ""
email_content = ""
try:
PLAIN = message.plain.split(split_syb)
course_id = PLAIN[0].split()[0]
summary = summarize_email(PLAIN[1])
email_content = PLAIN[1]
except:
course_id = "NAN"
summary = summarize_email(message.plain)
email_content = message.plain
if course_id[0] == '1':
course_name, teacher_name = get_course_info(course_id)
else:
course_name = course_id
teacher_name = 'NAN'
contents = summary.split('\n')
#print('From: ' + SENDER[0])
#print('Subject: ' + message.subject)
#print('Date: ' + message.date)
#print('Course: ' + course_id)
#print('Summary: \n' + summary) # or message.html
#print('------------------------------------')
insert_to_notion(email_content, contents, message.subject, course_name, teacher_name,
SENDER[0][1:-1], message.date)