File size: 2,781 Bytes
550665c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
.. _free_busy:

Free busy
=========

With `gcsa` you can retrieve the free/busy information of the calendars and groups.

To do so, create a :py:class:`~gcsa.google_calendar.GoogleCalendar` instance (see :ref:`getting_started` to get your
credentials):


.. code-block:: python

    from gcsa.google_calendar import GoogleCalendar

    gc = GoogleCalendar()


Then to retrieve a free/busy information of the calendar for the following two weeks starting now use
:py:meth:`~gcsa.google_calendar.GoogleCalendar.get_free_busy`:

.. code-block:: python

    free_busy = gc.get_free_busy()

this will return a :py:class:`~gcsa.free_busy.FreeBusy` object. If only one calendar has been requested (like in
the example above, only "primary" calendar's information has been requested), you can iterate over

:py:class:`~gcsa.free_busy.FreeBusy` object directly:





.. code-block:: python



    for start, end in free_busy:

        print(f'Busy from {start} to {end}')



To request group(s) or different calendar(s) (other than one specified as default during `GoogleCalendar` creation),

use `resource_ids` argument of :py:meth:`~gcsa.google_calendar.GoogleCalendar.get_free_busy`:





.. code-block:: python



    free_busy = gc.get_free_busy('[email protected]')



    for start, end in free_busy:

        print(f'Busy from {start} to {end}')



or



.. code-block:: python



    free_busy = gc.get_free_busy(

        [

            'primary',

            '[email protected]',

            'group_id'

        ]

    )



    print('Primary calendar:')

    for start, end in free_busy.calendars['primary']:

        print(f'Busy from {start} to {end}')



    print('Secondary calendar:')

    for start, end in free_busy.calendars['[email protected]']:

        print(f'Busy from {start} to {end}')



    print('Group info:')

    for calendar in free_busy.groups['group_id']:

        print(f'{calendar}:')

        for start, end in free_busy.calendars[calendar]:

            print(f'Busy from {start} to {end}')



Some calendars or groups in the request might cause errors. By default `gcsa` will

raise :py:class:`~gcsa.free_busy.FreeBusyQueryError` in case of any errors. But you can ignore them with `ignore_errors`

argument:



.. code-block:: python



    free_busy = gc.get_free_busy(

        resource_ids=[

            'primary',

            '[email protected]',

            'group_id'

        ],

        ignore_errors=True

    )



In that case, all the errors can be found in :py:class:`~gcsa.free_busy.FreeBusy`'s `groups_errors` and
`calendars_errors` fields:

.. code-block:: python

    print(free_busy.groups_errors)
    print(free_busy.calendars_errors)