File size: 1,806 Bytes
f84033a
02ccde5
 
 
 
a377469
02ccde5
a377469
 
 
 
 
 
 
02ccde5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f84033a
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
#calendar_reader.py
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import datetime
import pytz
from smolagents import tool

@tool
def get_todays_events() -> list[str]:
    """
        Get today's events from Google Calendar in formatted string form.
        Returns a list of event descriptions for the current day.
    """
    creds = Credentials.from_authorized_user_file('data/token.json',['https://www.googleapis.com/auth/calendar.readonly'])
    service = build('calendar', 'v3', credentials=creds)

    now = datetime.datetime.now(pytz.timezone("Europe/Istanbul"))
    start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
    end_of_day = now.replace(hour=23, minute=59, second=59, microsecond=999999)

    events_result = service.events().list(
        calendarId='primary',
        timeMin=start_of_day.isoformat(),
        timeMax=end_of_day.isoformat(),
        singleEvents=True,
        orderBy='startTime'
    ).execute()

    events = events_result.get('items', [])
    unique = set()
    formatted_events = []

    for event in events:
        summary = event.get('summary', 'İsimsiz Etkinlik')
        start = event['start'].get('dateTime')
        end = event['end'].get('dateTime')
        if not start or not end:
            continue  # tüm günlük veya eksik bilgiler varsa geç
        if (summary, start) in unique:
            continue  # yinelenenleri atla
        unique.add((summary, start))

        start_dt = datetime.datetime.fromisoformat(start)
        end_dt = datetime.datetime.fromisoformat(end)
        formatted = f"{summary}. ({start_dt.strftime('%H : %M')} - {end_dt.strftime('%H : %M')} / {start_dt.strftime('%d - %m - %Y')})"
        formatted_events.append(formatted)

    return formatted_events