-
Notifications
You must be signed in to change notification settings - Fork 0
/
S_07_user_gcal_push.py
82 lines (59 loc) · 2.7 KB
/
S_07_user_gcal_push.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
# creds vs token.json
# catch right json file for current user
import os.path
from datetime import date, datetime, timedelta
import json
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
from currentUser import user_id
from config import db_path, SCOPES, credentials_path, current_date_str
from functions import extract_from_db_users_by_id, view_db
def push_calendar_entries():
print('Wohoo, you made it to notebook 7.')
# Define the filename with the current date
filename = f"data/outputs/calendar/{user_id}_created_{current_date_str}.json"
event_file_path = filename
with open(event_file_path, 'r') as event_file:
events = json.load(event_file)
user_token = extract_from_db_users_by_id("refresh_token")
if not user_token:
raise ValueError(f"Refresh token not found for user {user_id}")
with open(credentials_path, 'r') as file:
credentials_data = json.load(file)
client_id = credentials_data['installed']['client_id']
client_secret = credentials_data['installed']['client_secret']
token_uri = credentials_data['installed']['token_uri']
creds = None
creds = Credentials(
token=None, # No access token at the moment
refresh_token=user_token,
client_id=client_id, # Replace with actual client ID
client_secret=client_secret, # Replace with actual client secret
token_uri=token_uri
)
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
# Your existing logic here to push calendar events...
print(f"Credentials obtained for user {user_id}")
# Initialize the Calendar API service
service = build('calendar', 'v3', credentials=creds)
# Load events from the JSON file
with open(event_file_path, 'r') as event_file:
events = json.load(event_file)
for event in events:
# Check if 'summary' and 'description' are provided; if not, set default values
if not event.get('summary'):
event['summary'] = 'No Title'
if not event.get('description'):
event['description'] = 'No Description'
try:
# Insert each event into the calendar
created_event = service.events().insert(calendarId='primary', body=event).execute()
print(f'Event created: {created_event.get("htmlLink")}')
except Exception as e:
print(f'An error occurred while creating the event "{event["summary"]}": {e}')
if __name__ == "__main__":
push_calendar_entries()