diff --git a/application/TODO b/application/TODO index 8d6ea0c..cb6637a 100644 --- a/application/TODO +++ b/application/TODO @@ -1,5 +1,6 @@ TODO: - check if first run (api call) and show google calendar login button * schedule pypi every().day() or smth like that at (time to print)-5 minutes to update calendar and get ready to print new daily list -* parse calendar list to database format and call save_todos() with list of Todos -- Start uvicorn on device startup (using gunicorn + sysmtectl) \ No newline at end of file +- parse calendar list to database format and call save_todos() with list of Todos +- Start uvicorn on device startup (using gunicorn + sysmtectl) +* remove from recurring list api call \ No newline at end of file diff --git a/application/main.py b/application/main.py index 9d0b800..c3e7da4 100644 --- a/application/main.py +++ b/application/main.py @@ -11,6 +11,7 @@ from .utils import VerifyToken from .database import TodoDatabase from .thermal_print import ThermalPrinter from .sync_calendar import SyncData +from .tododatatypes import * import errno import os @@ -31,35 +32,35 @@ printer = ThermalPrinter(data) -class Todo(BaseModel): - time: str - task: str - recurring: bool +# class Todo(BaseModel): +# time: str +# task: str +# recurring: bool -class TodoList(BaseModel): - date: str #Always selected date OLD TEXT: #Either current date in %Y-%m-%d format or "recurring" for a recurring task - todos: list[Todo] +# class TodoList(BaseModel): +# date: str #Always selected date OLD TEXT: #Either current date in %Y-%m-%d format or "recurring" for a recurring task +# todos: list[Todo] -class TodoDate(BaseModel): - date: str - # date: str = datetime.today().strftime('%Y-%m-%d') +# class TodoDate(BaseModel): +# date: str +# # date: str = datetime.today().strftime('%Y-%m-%d') -class PrintAction(BaseModel): - date: str = datetime.today().strftime('%Y-%m-%d') - action: str #Options: - # all (pulls current todos from date, needs date) - # sudoku (prints a random sudoku) - # todos (prints only dates todos) - # wordsearch (prints a random wordsearch) - # quote (prints a random quote) - # greeting (prints a greeting) - # sentence (prints a custom message) - sentence: str = "" +# class PrintAction(BaseModel): +# date: str = datetime.today().strftime('%Y-%m-%d') +# action: str #Options: +# # all (pulls current todos from date, needs date) +# # sudoku (prints a random sudoku) +# # todos (prints only dates todos) +# # wordsearch (prints a random wordsearch) +# # quote (prints a random quote) +# # greeting (prints a greeting) +# # sentence (prints a custom message) +# sentence: str = "" -class GoogleUpdate(BaseModel): - code: str +# class GoogleUpdate(BaseModel): +# code: str # @app.get("/api/public") # def public(): diff --git a/application/sync_calendar.py b/application/sync_calendar.py index 5c82d05..ee5b042 100644 --- a/application/sync_calendar.py +++ b/application/sync_calendar.py @@ -8,10 +8,11 @@ from googleapiclient.errors import HttpError import os import json import datetime - +import schedule from .database import TodoDatabase +from .tododatatypes import Todo data = TodoDatabase() @@ -52,6 +53,7 @@ class SyncData(): self.get_new_creds() self.get_calendar_events() + schedule.every(1).day.at("07:55").do(self.get_calendar_events) def get_new_creds(self, code=""): @@ -127,11 +129,11 @@ class SyncData(): day_end_for = day_end.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S.%fZ') # print(day_start.tzname()) - print(day_start) - print(day_end) - print(day_start.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S.%fZ')) - print(day_end.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S.%fZ')) - print(datetime.utcnow().isoformat() + 'Z') + # print(day_start) + # print(day_end) + # print(day_start.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + # print(day_end.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + # print(datetime.utcnow().isoformat() + 'Z') try: @@ -148,13 +150,33 @@ class SyncData(): print('No upcoming events found.') return + print(str(len(events)) + " events found") + # Prints the start and name of the next 10 events # event format: {'kind': 'calendar#event', 'etag': '"3386046190326000"', 'id': '0migcv6t729ph1kirhiauej3eh', 'status': 'confirmed', 'htmlLink': 'https://www.google.com/calendar/event?eid=MG1pZ2N2NnQ3MjlwaDFraXJoaWF1ZWozZWggYmFtLmltLnNhbUBt', 'created': '2023-08-26T04:11:35.000Z', 'updated': '2023-08-26T04:11:35.163Z', 'summary': 'test', 'creator': {'email': 'bam.im.sam@gmail.com', 'self': True}, 'organizer': {'email': 'bam.im.sam@gmail.com', 'self': True}, 'start': {'dateTime': '2023-08-27T14:30:00-04:00', 'timeZone': 'America/Toronto'}, 'end': {'dateTime': '2023-08-27T15:30:00-04:00', 'timeZone': 'America/Toronto'}, 'iCalUID': '0migcv6t729ph1kirhiauej3eh@google.com', 'sequence': 0, 'reminders': {'useDefault': True}, 'eventType': 'default'} + formatted_todo_list = [] + # print(events) for event in events: - print(event) + # print(event) start = event['start'].get('dateTime', event['start'].get('date')) end = event['end'].get('dateTime', event['end'].get('date')) - print(start, "->", end, event['summary']) + # print(event) + # print(start, "->", end, event['summary']) + start_parsed = datetime.strptime(start, "%Y-%m-%dT%H:%M:%S%z").strftime("%-I:%M%p") #2023-08-26T15:30:00-04:00 + end_parsed = datetime.strptime(end, "%Y-%m-%dT%H:%M:%S%z").strftime("%-I:%M%p") #2023-08-26T15:30:00-04:00 + # print("parsed: ", start_parsed, "to", end_parsed, event['summary']) + + if 'summary' in event: + # {"time": start_parsed + " to " + end_parsed, "task": event['summary'], "recurring": False} + formatted_todo_list.append(Todo(time=start_parsed + " to " + end_parsed, task=event['summary'], recurring=False)) + # formatted_todo_list.append({"time": start_parsed + " to " + end_parsed, "task": event['summary'], "recurring": False}) + else: + formatted_todo_list.append(Todo(time=start_parsed + " to " + end_parsed, task='No Title', recurring=False)) + # formatted_todo_list.append({"time": start_parsed + " to " + end_parsed, "task": 'No Title', "recurring": False}) + + # print(formatted_todo_list) + data.save_todos(day_start.strftime("%Y-%m-%d"), formatted_todo_list) + except HttpError as error: print('An error occurred: %s' % error) diff --git a/application/thermal_print.py b/application/thermal_print.py index 127ee22..03becb4 100644 --- a/application/thermal_print.py +++ b/application/thermal_print.py @@ -35,6 +35,7 @@ import usb from wonderwords import RandomWord from datetime import datetime import textwrap +import schedule from .sudoku_generator import * @@ -56,6 +57,7 @@ class ThermalPrinter(): self.p = Dummy() print("Try running `export DYLD_LIBRARY_PATH=/opt/homebrew/lib` if on m1 mac. source: https://github.com/pyusb/pyusb/issues/355#issuecomment-1062798576") # raise + schedule.every().day.at("08:00").do(self.print_default) def print_custom(self, text=""): self.p.set(align="left") diff --git a/application/tododatatypes.py b/application/tododatatypes.py new file mode 100644 index 0000000..4af0976 --- /dev/null +++ b/application/tododatatypes.py @@ -0,0 +1,32 @@ +from pydantic import BaseModel +from datetime import datetime + +class Todo(BaseModel): + time: str + task: str + recurring: bool + +class TodoList(BaseModel): + date: str #Always selected date OLD TEXT: #Either current date in %Y-%m-%d format or "recurring" for a recurring task + todos: list[Todo] + + +class TodoDate(BaseModel): + date: str + # date: str = datetime.today().strftime('%Y-%m-%d') + + +class PrintAction(BaseModel): + date: str = datetime.today().strftime('%Y-%m-%d') + action: str #Options: + # all (pulls current todos from date, needs date) + # sudoku (prints a random sudoku) + # todos (prints only dates todos) + # wordsearch (prints a random wordsearch) + # quote (prints a random quote) + # greeting (prints a greeting) + # sentence (prints a custom message) + sentence: str = "" + +class GoogleUpdate(BaseModel): + code: str \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1ce1cfe..34a8a6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,4 +22,6 @@ wonderwords google-api-python-client google-auth google-auth-oauthlib -google-auth-httplib2 \ No newline at end of file +google-auth-httplib2 +gunicorn +schedule \ No newline at end of file