It is currently May 5th, 2024, 7:09 pm

Unable to get todays events from Google Calendar

Get help with creating, editing & fixing problems with skins
Polymath_88
Posts: 2
Joined: April 25th, 2024, 6:44 pm
Location: Oregon. USA

Unable to get todays events from Google Calendar

Post by Polymath_88 »

Thank you for taking the time to read this post!

I'm working on my first Rainmeter skin after being a user for a few years. I want a widget that displays todays events from my calendar. I am using Python to connect to google calendar, I made a project with the Google Developer Console and got the permissions (maybe not the right one, I think I do, but maybe worth looking into).

Right now the skin is a blank box, when I update Text under EventMeter it updates correctly to reflect what I've hardcoded in, but otherwise I can't even get anything in the Log..

The other night I made a test event in my calendar and that was picked up, but weirdly enough that was the only event it picked up the next day as well (talking about the same event from the day before!)

Here's my .ini:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
PythonScriptPath="C:\\Users\\Victo\\Code\\Projects\\rainmeter\\RainmeterGoogleCalendar\\Python\\google_calendar.py"

[MeasurePython]
Measure=Plugin
Plugin=RunCommand
Parameter=python "C:\\Users\\Victo\\Code\\Projects\\rainmeter\\RainmeterGoogleCalendar\\Python\\google_calendar.py"
OutputType=ANSI
UpdateDivider=600  ; Update every 10 minutes
DynamicVariables=1
FinishAction=[!Log "MeasurePython Output: %1"][!UpdateMeter EventMeter][!Redraw]

[EventMeter]
Meter=String
MeasureName=MeasurePython
X=0
Y=0
W=300
H=25
Padding=5,5,5,5
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
Text="%1"
LeftMouseUpAction=["https://calendar.google.com"]
DynamicVariables=1
And here's the python:

Code: Select all

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from datetime import datetime, timedelta, timezone
import pytz
import pickle
import os

SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']

def authenticate_google_api():
    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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds

def fetch_events_for_today():
    """
    Fetch events for the current day from Google Calendar, adapting to the calendar's timezone.
    """
    service = build('calendar', 'v3', credentials=authenticate_google_api())
    
    # Fetch the primary calendar's timezone
    calendar = service.calendars().get(calendarId='primary').execute()
    timezone = calendar['timeZone']
    local_tz = pytz.timezone(timezone)

    local_now = datetime.now(local_tz)
    start_of_day = local_now.replace(hour=0, minute=0, second=0, microsecond=0)
    end_of_day = start_of_day + timedelta(days=1) - timedelta(seconds=1)

    start_of_day = start_of_day.isoformat()
    end_of_day = end_of_day.isoformat()

    # Fetch the events within this time range
    events_result = service.events().list(calendarId='primary', timeMin=start_of_day, timeMax=end_of_day,
                                          singleEvents=True, orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        return "No events found for today."

    events_for_display = []
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        summary = event.get('summary', 'No Title')
        events_for_display.append(f"{start}: {summary}")

    return '\n'.join(events_for_display)

if __name__ == "__main__":
    print(fetch_events_for_today())
User avatar
Yincognito
Rainmeter Sage
Posts: 7199
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Unable to get todays events from Google Calendar

Post by Yincognito »

There's a related thread here, maybe it will help?
https://forum.rainmeter.net/viewtopic.php?t=27623
It works with Lua instead of Python and I don't know of its operational status, but I suppose the principles are the same. :???:

Personally, I don't use it, nor Google Calendar, so it might be better to discuss these with someone more familiar with them - either here or in that thread...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
Polymath_88
Posts: 2
Joined: April 25th, 2024, 6:44 pm
Location: Oregon. USA

Re: Unable to get todays events from Google Calendar

Post by Polymath_88 »

Yincognito wrote: April 26th, 2024, 9:40 am There's a related thread here, maybe it will help?
https://forum.rainmeter.net/viewtopic.php?t=27623
It works with Lua instead of Python and I don't know of its operational status, but I suppose the principles are the same. :???:

Personally, I don't use it, nor Google Calendar, so it might be better to discuss these with someone more familiar with them - either here or in that thread...
Thank you for the tip! I appreciate you taking the time to point me in a direction!! :D :D :rosegift:
User avatar
Yincognito
Rainmeter Sage
Posts: 7199
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Unable to get todays events from Google Calendar

Post by Yincognito »

Polymath_88 wrote: April 26th, 2024, 7:45 pm Thank you for the tip! I appreciate you taking the time to point me in a direction!! :D :D :rosegift:
No problem, you're welcome - hopefully that direction will be productive for you! ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth