-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_parser.py
More file actions
398 lines (320 loc) · 14.1 KB
/
Copy pathgmail_parser.py
File metadata and controls
398 lines (320 loc) · 14.1 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
Gmail Mail Parser
==================
Parses all Gmail emails and categorizes them by labels (Inbox, Spam, Promotions, etc.)
Extracts: Sender, Subject, Date, Snippet, Body, Labels
"""
import os
import json
import base64
from datetime import datetime
from typing import Dict, List, Optional
from email.utils import parseaddr
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# Gmail API Scopes - readonly access to emails
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
# Output directory for parsed emails
OUTPUT_DIR = 'parsed_emails'
class GmailParser:
"""Gmail email parser that fetches and categorizes all emails."""
def __init__(self):
self.service = None
self.credentials = None
def authenticate(self) -> bool:
"""
Authenticate with Gmail API using OAuth2.
Returns True if authentication successful.
"""
creds = None
# Check for existing token
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If no valid credentials, let user log in
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print("🔄 Refreshing expired credentials...")
creds.refresh(Request())
else:
if not os.path.exists('credentials.json'):
print("❌ Error: 'credentials.json' not found!")
print("📋 Please follow the setup instructions in SETUP.md")
return False
print("🔐 Opening browser for Gmail authentication...")
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save credentials for next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
print("✅ Credentials saved to token.json")
self.credentials = creds
self.service = build('gmail', 'v1', credentials=creds)
print("✅ Successfully authenticated with Gmail API!")
return True
def get_labels(self) -> Dict[str, str]:
"""Fetch all Gmail labels and return id->name mapping."""
try:
results = self.service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
return {label['id']: label['name'] for label in labels}
except HttpError as error:
print(f"❌ Error fetching labels: {error}")
return {}
def fetch_emails(self, max_results: int = 500, label_ids: Optional[List[str]] = None) -> List[Dict]:
"""
Fetch emails from Gmail.
Args:
max_results: Maximum number of emails to fetch
label_ids: Optional list of label IDs to filter by
Returns:
List of email message objects
"""
emails = []
page_token = None
fetched = 0
print(f"\n📧 Fetching up to {max_results} emails...")
try:
while fetched < max_results:
# Build request parameters
params = {
'userId': 'me',
'maxResults': min(100, max_results - fetched),
}
if label_ids:
params['labelIds'] = label_ids
if page_token:
params['pageToken'] = page_token
# Fetch message list
results = self.service.users().messages().list(**params).execute()
messages = results.get('messages', [])
if not messages:
break
# Fetch full message details for each
for msg in messages:
email_data = self._get_email_details(msg['id'])
if email_data:
emails.append(email_data)
fetched += 1
if fetched % 50 == 0:
print(f" 📥 Fetched {fetched} emails...")
# Check for more pages
page_token = results.get('nextPageToken')
if not page_token:
break
except HttpError as error:
print(f"❌ Error fetching emails: {error}")
print(f"✅ Successfully fetched {len(emails)} emails!")
return emails
def _get_email_details(self, msg_id: str) -> Optional[Dict]:
"""
Get detailed information for a single email.
Args:
msg_id: Gmail message ID
Returns:
Dictionary with email details or None if error
"""
try:
message = self.service.users().messages().get(
userId='me',
id=msg_id,
format='full'
).execute()
headers = message.get('payload', {}).get('headers', [])
# Extract header values
subject = self._get_header(headers, 'Subject') or '(No Subject)'
from_header = self._get_header(headers, 'From') or 'Unknown'
to_header = self._get_header(headers, 'To') or ''
date_header = self._get_header(headers, 'Date') or ''
# Parse sender name and email
sender_name, sender_email = parseaddr(from_header)
# Get email body
body = self._get_email_body(message.get('payload', {}))
# Get labels
label_ids = message.get('labelIds', [])
# Determine category
category = self._categorize_email(label_ids)
return {
'id': msg_id,
'subject': subject,
'sender_name': sender_name or sender_email.split('@')[0],
'sender_email': sender_email,
'to': to_header,
'date': date_header,
'timestamp': message.get('internalDate'),
'snippet': message.get('snippet', ''),
'body_preview': body[:500] if body else '',
'labels': label_ids,
'category': category,
'is_unread': 'UNREAD' in label_ids,
'is_starred': 'STARRED' in label_ids,
'has_attachments': self._has_attachments(message.get('payload', {}))
}
except HttpError as error:
print(f"⚠️ Error fetching message {msg_id}: {error}")
return None
def _get_header(self, headers: List[Dict], name: str) -> Optional[str]:
"""Extract a specific header value from headers list."""
for header in headers:
if header['name'].lower() == name.lower():
return header['value']
return None
def _get_email_body(self, payload: Dict) -> str:
"""Extract email body text from payload."""
body = ''
if 'body' in payload and payload['body'].get('data'):
body = base64.urlsafe_b64decode(payload['body']['data']).decode('utf-8', errors='ignore')
elif 'parts' in payload:
for part in payload['parts']:
if part['mimeType'] == 'text/plain':
if part['body'].get('data'):
body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8', errors='ignore')
break
elif part['mimeType'] == 'text/html' and not body:
if part['body'].get('data'):
body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8', errors='ignore')
elif 'parts' in part:
# Handle nested multipart
body = self._get_email_body(part)
if body:
break
return body
def _has_attachments(self, payload: Dict) -> bool:
"""Check if email has attachments."""
if 'parts' in payload:
for part in payload['parts']:
if part.get('filename'):
return True
if 'parts' in part:
if self._has_attachments(part):
return True
return False
def _categorize_email(self, label_ids: List[str]) -> str:
"""Categorize email based on its labels."""
if 'SPAM' in label_ids:
return 'Spam'
elif 'TRASH' in label_ids:
return 'Trash'
elif 'CATEGORY_PROMOTIONS' in label_ids:
return 'Promotions'
elif 'CATEGORY_SOCIAL' in label_ids:
return 'Social'
elif 'CATEGORY_UPDATES' in label_ids:
return 'Updates'
elif 'CATEGORY_FORUMS' in label_ids:
return 'Forums'
elif 'CATEGORY_PERSONAL' in label_ids:
return 'Personal'
elif 'SENT' in label_ids:
return 'Sent'
elif 'DRAFT' in label_ids:
return 'Draft'
elif 'INBOX' in label_ids:
return 'Primary'
else:
return 'Other'
def categorize_emails(self, emails: List[Dict]) -> Dict[str, List[Dict]]:
"""
Organize emails by their category.
Args:
emails: List of email dictionaries
Returns:
Dictionary mapping category names to email lists
"""
categories = {}
for email in emails:
category = email['category']
if category not in categories:
categories[category] = []
categories[category].append(email)
return categories
def save_to_json(self, emails: List[Dict], filename: str = 'all_emails.json'):
"""Save emails to JSON file."""
os.makedirs(OUTPUT_DIR, exist_ok=True)
filepath = os.path.join(OUTPUT_DIR, filename)
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(emails, f, indent=2, ensure_ascii=False)
print(f"💾 Saved {len(emails)} emails to {filepath}")
def save_categorized(self, categorized: Dict[str, List[Dict]]):
"""Save categorized emails to separate JSON files."""
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Save summary
summary = {cat: len(emails) for cat, emails in categorized.items()}
summary_path = os.path.join(OUTPUT_DIR, 'summary.json')
with open(summary_path, 'w', encoding='utf-8') as f:
json.dump(summary, f, indent=2)
# Save each category
for category, emails in categorized.items():
filename = f"{category.lower().replace(' ', '_')}_emails.json"
filepath = os.path.join(OUTPUT_DIR, filename)
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(emails, f, indent=2, ensure_ascii=False)
print(f" 📁 {category}: {len(emails)} emails -> {filename}")
def save_to_csv(self, emails: List[Dict], filename: str = 'all_emails.csv'):
"""Save emails to CSV file using pandas."""
import pandas as pd
os.makedirs(OUTPUT_DIR, exist_ok=True)
filepath = os.path.join(OUTPUT_DIR, filename)
# Prepare data for CSV (exclude complex fields)
csv_data = []
for email in emails:
csv_data.append({
'Date': email['date'],
'Category': email['category'],
'Sender Name': email['sender_name'],
'Sender Email': email['sender_email'],
'Subject': email['subject'],
'Snippet': email['snippet'],
'Unread': email['is_unread'],
'Starred': email['is_starred'],
'Has Attachments': email['has_attachments']
})
df = pd.DataFrame(csv_data)
df.to_csv(filepath, index=False, encoding='utf-8')
print(f"📊 Saved to CSV: {filepath}")
def print_summary(self, categorized: Dict[str, List[Dict]]):
"""Print a summary of categorized emails."""
print("\n" + "="*60)
print("📊 EMAIL SUMMARY")
print("="*60)
total = sum(len(emails) for emails in categorized.values())
# Sort categories by count
sorted_cats = sorted(categorized.items(), key=lambda x: len(x[1]), reverse=True)
for category, emails in sorted_cats:
bar_length = int((len(emails) / total) * 40) if total > 0 else 0
bar = "█" * bar_length
print(f" {category:15} │ {len(emails):5} │ {bar}")
print("-"*60)
print(f" {'TOTAL':15} │ {total:5}")
print("="*60)
def main():
"""Main function to run the Gmail parser."""
print("\n" + "="*60)
print("📧 GMAIL MAIL PARSER")
print("="*60)
parser = GmailParser()
# Step 1: Authenticate
if not parser.authenticate():
return
# Step 2: Fetch all emails
print("\n📥 Fetching emails from all categories...")
emails = parser.fetch_emails(max_results=500)
if not emails:
print("⚠️ No emails found!")
return
# Step 3: Categorize emails
print("\n🏷️ Categorizing emails...")
categorized = parser.categorize_emails(emails)
# Step 4: Print summary
parser.print_summary(categorized)
# Step 5: Save results
print("\n💾 Saving parsed emails...")
parser.save_to_json(emails)
parser.save_categorized(categorized)
parser.save_to_csv(emails)
print("\n✅ Done! Check the 'parsed_emails' folder for results.")
print("="*60 + "\n")
if __name__ == '__main__':
main()