adding full project without the pi sign edits
This commit is contained in:
+48
-2
@@ -1,8 +1,54 @@
|
||||
from datetime import datetime
|
||||
|
||||
from google.oauth2 import service_account
|
||||
from googleapiclient.discovery import build
|
||||
from googleapiclient.errors import HttpError
|
||||
|
||||
from event import Event
|
||||
from time import sleep
|
||||
|
||||
|
||||
class CalGrab(object):
|
||||
|
||||
def __init__(self):
|
||||
print("Hello")
|
||||
def __init__(self, auth_file, calendars=[], callbacks=[]):
|
||||
self.calendars = calendars
|
||||
self.callbacks = callbacks
|
||||
self.creds = service_account.Credentials.from_service_account_file(
|
||||
auth_file, scopes=['https://www.googleapis.com/auth/calendar.readonly'])
|
||||
try:
|
||||
self.service = build('calendar', 'v3', credentials=self.creds)
|
||||
except HttpError as error:
|
||||
raise error
|
||||
|
||||
def update_at_interval(self, frequency, time_to_update=-1):
|
||||
"""
|
||||
|
||||
:param frequency: Amount of time to wait between updates in seconds
|
||||
:param time_to_update: normally set to -1 aka no limit. Time in minutes to continually ping the api for.
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
start = None
|
||||
while True:
|
||||
now = datetime.utcnow() # 'Z' indicates UTC time
|
||||
now_str = now.isoformat() + 'Z'
|
||||
if start ==None:
|
||||
start = now
|
||||
print('Getting the upcoming 10 events')
|
||||
events_result = self.service.events().list(calendarId='loskorep@productiveedge.com', timeMin=now_str,
|
||||
maxResults=10, singleEvents=True,
|
||||
orderBy='startTime').execute()
|
||||
events = events_result.get('items', [])
|
||||
|
||||
if not events:
|
||||
print('No upcoming events found.')
|
||||
return
|
||||
|
||||
events = [Event.get_from_gcal_api_json(json) for json in events]
|
||||
for callback in self.callbacks:
|
||||
callback(events)
|
||||
if (now-start).total_seconds() > time_to_update:
|
||||
return
|
||||
sleep(frequency)
|
||||
except HttpError as error:
|
||||
print('An error occurred: %s' % error)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
from dateutil.parser import parse
|
||||
|
||||
class Event(object):
|
||||
|
||||
def __init__(self, summary, start_time, end_time):
|
||||
self.summary = summary
|
||||
self.start_time = start_time
|
||||
self.end_time = end_time
|
||||
|
||||
def __str__(self):
|
||||
return f"summary : {self.summary}, end_time : {self.end_time}, start_time : {self.start_time}"
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_from_gcal_api_json(json):
|
||||
print(json['start'].get('dateTime') )
|
||||
return Event(json['summary'], parse(json['start'].get('dateTime')), parse(json['end'].get('dateTime')))
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
from calendar_grabber import CalGrab
|
||||
|
||||
def process_events(events):
|
||||
print("PROCESSING EVENTS")
|
||||
for event in events:
|
||||
print(event)
|
||||
|
||||
def main():
|
||||
cg = CalGrab("./.auth.json", "loskorep@productiveedge.com", [process_events])
|
||||
cg.update_at_interval(5, 15)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,45 @@
|
||||
from datetime import datetime, time
|
||||
|
||||
from pytz import timezone
|
||||
|
||||
from calendar_grabber import CalGrab
|
||||
|
||||
TIMEZONE = timezone('US/Eastern')
|
||||
WORK_START = time(9, 00)
|
||||
WORK_STOP = time(17, 00)
|
||||
|
||||
|
||||
def is_event_active(events, now):
|
||||
for event in events:
|
||||
if event.start_time < now and event.end_time > now:
|
||||
print("EVENT IS ACTIVE")
|
||||
|
||||
|
||||
def is_work_time(now):
|
||||
return now < WORK_STOP and now > WORK_START
|
||||
|
||||
|
||||
def process_events(events):
|
||||
now = datetime.now(tz=TIMEZONE)
|
||||
print("PROCESSING EVENTS")
|
||||
is_working_time = is_work_time(now.time())
|
||||
print(is_working_time)
|
||||
if is_event_active(events, now):
|
||||
if is_working_time:
|
||||
print("Meeting in Progress")
|
||||
else:
|
||||
print("Fuck me -_-")
|
||||
else:
|
||||
if is_working_time:
|
||||
print("Work time - No event")
|
||||
else:
|
||||
print("Looks like 420 to me ayooo")
|
||||
|
||||
|
||||
def main():
|
||||
cg = CalGrab("./.auth.json", "loskorep@productiveedge.com", [process_events])
|
||||
cg.update_at_interval(5, 15)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user