-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (84 loc) · 3.61 KB
/
main.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
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
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient import errors
import os.path
import random
import time
# If modifying these SCOPES, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
def get_service():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('gmail', 'v1', credentials=creds)
return service
def modify_message(service, user_id, message_id):
for attempt in range(5): # retry up to 5 times
try:
service.users().messages().modify(
userId=user_id,
id=message_id,
body={'removeLabelIds': ['UNREAD']}
).execute()
return
except errors.HttpError as error:
if error.resp.status in [502, 503, 504]: # check if error is 502, 503 or 504
time.sleep((2 ** attempt) + random.random()) # exponential backoff with jitter
else:
raise
def main():
start_time = time.time() # Record the start time
service = get_service()
page_token = None
total_marked = 0
request_count = 0
try:
while True:
results = service.users().messages().list(
userId='me',
labelIds=['INBOX'],
q="is:unread",
pageToken=page_token
).execute()
messages = results.get('messages', [])
if not messages:
print("No more unread messages found.")
break
else:
print(f"Retrieved {len(messages)} messages in page {request_count + 1}. Marking messages as read...")
total_marked += len(messages)
for message in messages:
modify_message(service, 'me', message['id'])
request_count += 1
end_time = time.time() # Record the end time
time_spent = end_time - start_time # Calculate the total time spent
print(f"Finished page {request_count}. Total messages marked as read so far: {total_marked} \n Time Spent: {time_spent:.2f}")
page_token = results.get('nextPageToken')
if not page_token:
print("No more pages left to process.")
break
time.sleep(1) # Sleep for 1 second to avoid rate limits
except Exception as e:
print("An error occurred:", e)
end_time = time.time() # Record the end time
time_spent = end_time - start_time # Calculate the total time spent
print(f"Total messages marked as read: {total_marked}")
print(f"Total time spent: {time_spent:.2f} seconds")
if __name__ == '__main__':
main()