public
Created

Google Calendar submissions for Birmingham.pm

  • Download Gist
gcal.pl
Perl
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#!/usr/bin/perl -w
use strict;
 
#----------------------------------------------------------
# Additional Modules
 
use Crypt::SSLeay;
use Data::Dumper;
use DateTime;
use Getopt::ArgvFile default => 1;
use Getopt::Long;
use IO::File;
use Net::Google::Calendar;
 
#----------------------------------------------------------
# Variables
 
my %calendars = (
# birmingham calendars
'Barbie Perl Monger' => 1, # mine
'Birmingham Perl Mongers' => 1, # ours
'techevent@googlemail.com' => 0, # not ours
 
# perl calendars
"Perl 'Local' Community Calendar" => 0, # not ours
 
# other calendars - big events
# 'The Perl Review: Perl Community Events' => 0, # not ours
 
);
my (%options,%settings,@myevents);
 
#----------------------------------------------------------
# Code
 
init();
 
my $cal = Net::Google::Calendar->new;
unless($cal->login($settings{guser}, $settings{gpass})) {
die "Failed Google login\n";
}
 
for my $title (keys %calendars) {
print "Calendar ... $title\n";
my $c;
for ($cal->get_calendars) {
# print $_->title."\n";
# print $_->id."\n\n";
$c = $_ if ($_->title eq $title);
}
 
if($c) {
$cal->set_calendar($c);
my @events = $cal->get_events( 'start-min' => DateTime->now, 'max-results' => 100 );
myevents($title,\@events,\@myevents);
}
}
 
#----------------------------------------------------------
# Functions
 
sub init {
GetOptions( \%options,
'file|f=s',
'user|u=s',
'pass|p=s'
) or usage();
 
usage('No source file provided') unless($options{file});
usage('No user name provided') unless($options{user});
usage('No password provided') unless($options{pass});
 
$settings{guser} = rot13($options{user});
$settings{gpass} = rot13($options{pass});
 
my $fh = IO::File->new($options{file}) or die "Cannot open file [$options{file}]: $!\n";
while(<$fh>) {
next if(/^#/ || /^\s*$/);
chomp;
my @field = split('¬');
push @myevents, {
start => $field[0],
end => $field[1],
location => $field[2], # html_format($field[2]),
author => $field[3],
title => $field[4],
content => $field[5], # html_format($field[5]),
timezone => $field[6]
};
}
$fh->close;
}
 
sub myevents {
my ($mycal,$events,$myevents) = @_;
 
#print "MINE=".scalar(@$myevents).", LIVE=".scalar(@$events)."\n";
#for my $event (@$events) {
# my ($start,$end) = $event->when;
# $start =~ s/[:-]//g;
# $end =~ s/[:-]//g;
# printf "TEST:%s,%s,%s\n",$event->title, $start, $end;
#}
 
for my $myevent (@$myevents) {
my $found;
for my $event (@$events) {
my ($start,$end) = $event->when;
$start =~ s/[:-]//g;
$end =~ s/[:-]//g;
 
#printf "LIVE:%s,%s,%s\n",$event->title, $start, $end;
#printf "MINE:%s,%s,%s\n",$myevent->{title}, $myevent->{start}, $myevent->{end};
if( $event->title eq $myevent->{title}
&& substr($start,0,8) eq substr($myevent->{start},0,8)
&& substr($end,0,8) eq substr($myevent->{end},0,8)
) {
$found = $event;
last;
}
}
 
# found a matching entry
# ... and it's our calendar (cal==1)
# ... ... or we're allowed to update (cal==2)
# ... ... or I'm the organsier
# then update it!
if($found &&
($calendars{$mycal} ||
$myevent->{author} =~ /barbie\@missbarbell.co.uk/)) {
 
print "Updating entry ... $myevent->{title}\n";
my $r = $cal->delete_entry($found);
if($r) {
$r = $cal->add_entry(mkentry($myevent));
}
print "... FAILED: $@\n" unless($r);
 
# no matching entry found
# ... and it's our calendar (cal==1)
# ... ... or we're allowed to update (cal==2)
# ... ... or I'm the organsier
# then add it!
} elsif(!$found &&
($calendars{$mycal} ||
$myevent->{author} =~ /barbie\@missbarbell.co.uk/)) {
 
print "Adding entry ... $myevent->{title}\n";
my $r = $cal->add_entry(mkentry($myevent));
print "... FAILED: $@\n" unless($r);
 
# ignore these events
} else {
print "Ignored entry ... $myevent->{title}, $myevent->{start}, $myevent->{end}\n";
}
}
}
 
sub mkentry {
my $myevent = shift;
my $e = shift || Net::Google::Calendar::Entry->new();
 
my @date = $myevent->{start} =~ /^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)/;
my $start = DateTime->new(
year => $date[0],
month => $date[1],
day => $date[2],
hour => $date[3],
minute => $date[4],
second => $date[5],
time_zone => $myevent->{timezone}
);
 
@date = $myevent->{end} =~ /^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)/;
my $end = DateTime->new(
year => $date[0],
month => $date[1],
day => $date[2],
hour => $date[3],
minute => $date[4],
second => $date[5],
time_zone => $myevent->{timezone}
);
 
$myevent->{content} =~ s/<[^>]+>//g;
 
$e->title($myevent->{title});
$e->content($myevent->{content});
$e->location($myevent->{location});
$e->when($start,$end);
$e->status('confirmed');
$e->transparency('opaque');
$e->visibility('public');
 
my ($name,$email) = $myevent->{author} =~ /^(.*?)(?: <([^>]+)>)?/;
my $a = Net::Google::Calendar::Person->new();
$a->name($name) if($name);
$a->email($email) if($email);
$e->author($a) if($a);
 
return $e;
}
 
sub rot13 {
my $str = shift;
$str =~ tr/a-zA-Z/n-za-mN-ZA-M/;
return $str;
}
 
sub html_format {
my $html = shift;
$html =~ s!(http://[^\s]+)!<a href="$1">$1</a>!g;
return $html;
}
 
sub usage {
my $msg = shift || '';
print "$msg\n\n" if($msg);
print "Usage: $0 -u=<user> -p=<password> -f=<file>\n";
exit 0;
}
 
__END__
 
# start time, end time, venue + link, organiser, title, description, timezone
20120808T180000¬20120808T230000¬The Dragon Inn, http://www.jdwetherspoon.co.uk/pubs/pub-details.php?PubNumber=5609¬Barbie <barbie@missbarbell.co.uk>¬Birmingham.pm Social Meeting¬See our social meetings pages for further details, http://birmingham.pm.org/event/267¬Europe/London

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.