-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
48 lines (32 loc) · 1.31 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
import os
from dotenv import load_dotenv
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 gmail_api.gmail_service import GmailService
from pipelines.pipeline import pipeline
from utils.configuration import Config
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
def create_service():
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=8080, prompt="consent", access_type="offline")
if creds.expired and creds.refresh_token:
creds.refresh(Request())
with open("token.json", "w") as token:
token.write(creds.to_json())
return build("gmail", "v1", credentials=creds)
def main():
load_dotenv()
Config.load()
Config.user_upstage_api_key = os.getenv("UPSTAGE_API_KEY")
gmail_service = GmailService(create_service())
_, report = pipeline(gmail_service)
print("============ FINAL REPORT=============")
print(report)
print("======================================")
if __name__ == "__main__":
main()