summaryrefslogtreecommitdiff
path: root/fetch-calendar
blob: 7789fc6bd57335aeb362217791eea2d854d5fee8 (plain)
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
#!/usr/bin/env perl 
use warnings;
use 5.020;
use open ':std',':locale';
use Net::Google::DataAPI::Auth::OAuth2;
use Net::OAuth2::AccessToken;
use Config::General;
use Net::Google::CalendarV3;
use POSIX 'strftime';
use Data::Printer;
 
sub Net::Google::CalendarV3::get_all_calendars {
    my ($self) = @_;
    my $res = $self->_service->get('/users/me/calendarList', { } );
    die $res->error unless $res->success;
    $self->calendars($res->res->{items});
}
 
my $config_file = Config::General->new('google-to-text.conf');
my %config = $config_file->getall;
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
  client_id => $config{oauth2}->{client_id},
  client_secret => $config{oauth2}->{client_secret},
  scope => ['https://www.googleapis.com/auth/calendar.readonly'],
);
my $token = Net::OAuth2::AccessToken->session_thaw(
    $config{oauth2}->{token},
    profile => $oauth2->oauth2_webserver,
);
my $gcal = Net::Google::CalendarV3->new({
    oauth2_access_token => $token->access_token,
});
 
$gcal->get_all_calendars();
for my $calendar (@{$gcal->calendars}) {
    say sprintf 'Calendar %s (%s / %s)',
        $calendar->id,$calendar->summary,$calendar->description//'';
    $gcal->set_calendar($calendar);
    for my $event ($gcal->get_events(
        timeMin => strftime('%FT%TZ',gmtime),
        timeMax => strftime('%FT%TZ',gmtime(time+86400*7)),
        orderBy => 'startTime',
        singleEvents => 'True',
    )) {
        say sprintf " > %s %s - %s\n >> %s\n",
            $event->summary,
            ($event->start->get)[0],($event->end->get)[0],
            $event->description//'';
    }
}