-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar_event.py
83 lines (71 loc) · 3.38 KB
/
calendar_event.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
import google.auth
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.auth.transport.requests import Request
import datetime
import pytz
import json
import os
from tdee_operation import tdee
SCOPES = ['https://www.googleapis.com/auth/calendar']
def get_credentials():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
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('client_secret.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())
return creds
def create_event(connection, mycur):
mycur.execute("SELECT userid, MAX(date) as latest_date FROM data GROUP BY userid")
ids = mycur.fetchall()
for i in ids:
mycur.execute("SELECT credential FROM users WHERE userid=%s", (i[0],))
result = mycur.fetchone()
if result:
data = json.loads(result[0])
# Update token.json with the stored credentials
with open('token.json', 'w') as f:
json.dump(data, f)
creds = get_credentials() # This will refresh the token if necessary
# Update the stored credentials in the database if they've changed
new_creds_json = creds.to_json()
if new_creds_json != result[0]:
mycur.execute("UPDATE users SET credential = %s WHERE userid = %s", (new_creds_json, i[0]))
mycur.connection.commit()
# Set up the Google Calendar API client
service = build('calendar', 'v3', credentials=creds)
tdee_vals = tdee(connection, mycur, i[0])
event = {
'summary': 'Reminder: Your TDEE Goal for Today',
'location': '',
'description': f'Your TDEE Goal for today: {tdee_vals[0]}\nYour Average Weight: {tdee_vals[1]}\nYour Average Calories: {tdee_vals[2]}',
'start': {
'dateTime': (datetime.datetime.now(tz=pytz.timezone('US/Eastern')) + datetime.timedelta(hours=8, minutes=0)).isoformat(),
'timeZone': 'US/Eastern',
},
'end': {
'dateTime': (datetime.datetime.now(tz=pytz.timezone('US/Eastern')) + datetime.timedelta(hours=8, minutes=15)).isoformat(),
'timeZone': 'US/Eastern',
},
'reminders': {
'useDefault': True,
},
'colorId': '11', # Sets the event color to a light purple
}
# Create the event in the calendar
try:
event = service.events().insert(calendarId='primary', body=event).execute()
print(f'Event created for user {i[0]}: {event.get("htmlLink")}')
except HttpError as error:
print(f'An error occurred for user {i[0]}: {error}')
else:
print(f"No credentials found for user {i[0]}")