| 17 | | my ( $class, %param ) = @_; |
| 18 | | $param{url} ||= delete $param{cybozu_url}; |
| 19 | | bless \%param, $class; |
| | 17 | my $class = shift; |
| | 18 | my (%param) = @_; |
| | 19 | my $cal = bless \%param, $class; |
| | 20 | $cal->{url} ||= delete $cal->{cybozu_url}; |
| | 21 | $cal->driver_init; |
| | 22 | $cal; |
| | 23 | } |
| | 24 | |
| | 25 | sub driver_init { |
| | 26 | my $cal = shift; |
| | 27 | my $driver = $cal->{calendar_driver} || 'ApiCalendar'; |
| | 28 | $driver = 'WWW::CybozuOffice6::CalendarDriver::' . $driver |
| | 29 | if $driver !~ m/^WWW::CybozuOffice6::CalendarDriver::/; |
| | 30 | eval "use $driver;"; |
| | 31 | $cal->{calendar_driver} = $driver; |
| | 32 | $cal; |
| 26 | | my $date_range = $cal->{date_range} || 30; |
| 27 | | |
| 28 | | my $ua = LWP::UserAgent->new; |
| 29 | | my $auth_param = { |
| 30 | | _System => 'login', |
| 31 | | _Login => 1, |
| 32 | | defined $cal->{username} ? ( _Account => $cal->{username} ) : (), |
| 33 | | defined $cal->{userid} ? ( _Id => $cal->{userid} ) : (), |
| 34 | | Password => $cal->{password} || '', |
| 35 | | }; |
| 36 | | |
| 37 | | my $res = $ua->post( $cal->{url} . '?page=SyncCalendar', $auth_param ); |
| 38 | | confess 'Failed to access SyncCalendar API: ' . $res->status_line |
| 39 | | unless $res->is_success; |
| 40 | | |
| 41 | | my $content = $res->content; |
| 42 | | from_to( $content, $cal->{input_encoding} || 'shiftjis', 'utf8' ); |
| 43 | | my @lines = grep /^\d+,ts\.\d+,/, split( /\r?\n/, $content ); |
| 44 | | $cal->{response} = \@lines; |
| 45 | | |
| 46 | | scalar @lines ? \@lines : undef; |
| | 39 | $cal->{calendar_driver}->request($cal); |
| 72 | | |
| 73 | | my $csv; |
| 74 | | if ( eval('require Text::CSV_XS') ) { |
| 75 | | $csv = Text::CSV_XS->new( { binary => 1 } ); |
| 76 | | } |
| 77 | | elsif ( eval('require Text::CSV') ) { |
| 78 | | $csv = Text::CSV->new(); |
| 79 | | } |
| 80 | | else { |
| 81 | | confess 'Text::CSV_XS or Text::CSV package is required'; |
| 82 | | } |
| 83 | | |
| 84 | | my @items; |
| 85 | | for my $line ( $cal->response ) { |
| 86 | | $csv->parse($line) |
| 87 | | or confess 'Failed to parse CSV input'; |
| 88 | | my @fields = $csv->fields; |
| 89 | | my $num_fields = @fields - 1; |
| 90 | | next if $num_fields < 13; |
| 91 | | $fields[1] =~ s/^ts\.//; # remove rubbish |
| 92 | | |
| 93 | | # Cybozu Calendar CSV Format |
| 94 | | # GENERIC | RECCURENT |
| 95 | | # [ 0] id? | id? |
| 96 | | # [ 1] created | created |
| 97 | | # [ 2] <BLANK> x start_date / end_date |
| 98 | | # [ 3] start_date x initial start_date? |
| 99 | | # [ 4] end_date x until_date |
| 100 | | # [ 5] start_time | start_time |
| 101 | | # [ 6] end_time | end_time |
| 102 | | # [ 7] <BLANK> | freq |
| 103 | | # [ 8] <BLANK> | freq_value |
| 104 | | # [ 9] ??? | ??? |
| 105 | | # [10] ??? | ??? |
| 106 | | # [11] abbrev | abbrev |
| 107 | | # [12] summary | summary |
| 108 | | # [13] description | description |
| 109 | | |
| 110 | | my %param; |
| 111 | | @param{ |
| 112 | | qw(id created start_time end_time freq freq_value abbrev summary description) |
| 113 | | } = @fields[ 0, 1, 5 .. 8, 11 .. 13 ]; |
| 114 | | $param{time_zone} = $cal->{time_zone} || 'Asia/Tokyo'; |
| 115 | | |
| 116 | | my $item; |
| 117 | | if ( !$param{freq} ) { |
| 118 | | @param{qw(start_date end_date)} = @fields[ 3, 4 ]; |
| 119 | | $item = WWW::CybozuOffice6::Calendar::Event->new(%param); |
| 120 | | } |
| 121 | | else { |
| 122 | | @param{qw(start_date end_date until_date)} = @fields[ 2, 2, 4 ]; |
| 123 | | if ( $num_fields > 13 ) { |
| 124 | | my @exdates = @fields[ 14 .. $num_fields ]; |
| 125 | | $param{exdates} = \@exdates; |
| 126 | | } |
| 127 | | my $freq = $param{freq}; |
| 128 | | if ( $freq =~ /^[1-5]$/ ) { |
| 129 | | $param{freq} = 'm'; |
| 130 | | my @week_str = ( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' ); |
| 131 | | $param{freq_value} = $freq . $week_str[ $param{freq_value} ]; |
| 132 | | } |
| 133 | | $item = WWW::CybozuOffice6::Calendar::RecurrentEvent->new(%param); |
| 134 | | } |
| 135 | | |
| 136 | | next unless $item; |
| 137 | | $item->comment($line); # save the CSV line as for debug info. |
| 138 | | push @items, $item; |
| 139 | | } |
| 140 | | wantarray ? @items : $items[0]; |
| | 65 | $cal->{calendar_driver}->get_items($cal); |