forked from OSSPhilippines/personal-well-being-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail_extraction_sample.py
34 lines (22 loc) · 996 Bytes
/
gmail_extraction_sample.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
from simplegmail import Gmail
from simplegmail.query import construct_query
import pandas as pd
from simplegmail.gmail_downloader import GmailDownloader
from datetime import datetime
gmail = Gmail()
gmail_downloader = GmailDownloader(gmail)
def format_query(domains, start_date = None, end_date = None):
query = ''
if start_date:
query += f' after:{start_date.strftime("%Y/%m/%d")}'
if end_date:
query += f' before:{end_date.strftime("%Y/%m/%d")}'
query += ' OR '.join([f" from:{domain}" for domain in domains] + [f" to:{domain}" for domain in domains])
return query
## this is for parameter filtering
domains = ["gcash.com", "unionbankph.com"]
start_date = datetime(2023, 2, 1) # Start from Feb 1, 2023
end_date = datetime.now() # Up to the current date
query = format_query(domains, start_date, end_date)
messages = gmail.get_messages(query=query, include_spam_trash=False)
df = gmail_downloader.process_messages(messages)