Created
July 15, 2012 14:17
-
-
Save barbie/3117107 to your computer and use it in GitHub Desktop.
Google Calendar submissions for Birmingham.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment