-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (105 loc) · 4.03 KB
/
main.py
File metadata and controls
136 lines (105 loc) · 4.03 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
"""
Simple Gmail Reader - Quick access to Gmail
What it does:
- Connects to your Gmail
- Shows last 5 emails
- Displays subject, sender and date
Setup (one time only):
1. Download credentials.json from Google Cloud Console
2. Put it in this folder
3. Run script - browser will open for authorization
"""
import os
import json
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
# Read-only access to Gmail (safe!)
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
def get_gmail_service():
"""Gets access to Gmail"""
# Check if there's already a saved token
if os.path.exists("token.json"):
print("Found saved authorization token...")
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
else:
creds = None
# If no token or expired
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print("Refreshing token...")
creds.refresh(Request())
else:
# First authorization needed
if not os.path.exists("credentials.json"):
print("ERROR: credentials.json file not found!")
print("Download it from Google Cloud Console and put it in this folder")
return None
print("First authorization...")
print("Browser will open - allow access to Gmail")
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save token for next runs
with open("token.json", "w", encoding="utf-8") as f:
f.write(creds.to_json())
print("Token saved!")
# Create Gmail service
return build("gmail", "v1", credentials=creds)
def get_emails_data(service, count=5):
"""Gets emails data and returns as list"""
# Get list of message IDs
result = service.users().messages().list(userId="me", maxResults=count).execute()
messages = result.get("messages", [])
if not messages:
return []
emails_data = []
for message in messages:
msg = service.users().messages().get(
userId="me",
id=message["id"],
format="metadata",
metadataHeaders=["Subject", "From", "Date"]
).execute()
headers = msg.get("payload", {}).get("headers", [])
subject = "No subject"
sender = "Unknown"
date = "No date"
for header in headers:
if header["name"] == "Subject":
subject = header["value"] or "No subject"
elif header["name"] == "From":
sender = header["value"] or "Unknown"
elif header["name"] == "Date":
date = header["value"] or "No date"
email_data = {
"id": message["id"],
"threadId": msg.get("threadId"),
"subject": subject,
"sender": sender,
"date": date,
"snippet": msg.get("snippet", "")
}
emails_data.append(email_data)
return emails_data
def main():
"""Main function"""
print("Starting Gmail Reader...")
print("=" * 50)
service = get_gmail_service()
if not service:
print("\nFailed to connect to Gmail")
print("Check credentials.json file")
return
print("Successfully connected to Gmail!")
emails_data = get_emails_data(service, count=5)
if emails_data:
# Save to JSON file
with open("emails.json", "w", encoding="utf-8") as f:
json.dump(emails_data, f, ensure_ascii=False, indent=2)
print(json.dumps(emails_data, ensure_ascii=False, indent=2))
print(f"\nSaved {len(emails_data)} emails to emails.json")
else:
print("No emails found")
if __name__ == "__main__":
main()