-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemblcal.pl
More file actions
executable file
·231 lines (177 loc) · 5.75 KB
/
emblcal.pl
File metadata and controls
executable file
·231 lines (177 loc) · 5.75 KB
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
228
229
230
#!/usr/bin/env perl
#use Data::Dump qw/dump/;
#use Data::Dumper;
use Net::Google::Calendar;
use LWP::Simple;
use DateTime;
use DateTime::Duration;
use Getopt::Long;
use Term::ReadKey;
sub password {
print "Enter your password: ";
ReadMode 'noecho';
my $password = ReadLine 0;
chomp $password;
ReadMode 'normal';
return $password;
}
# Overwrite with any command line ops
my %ops;
my $result = GetOptions(\%ops,
'sleepmin|n=i',
'sleepmax|x=i',
'user|u=s',
'password|p=s',
);
# Sleep between $sleepmin and $sleepmax seconds between creating events
our $sleepmin = $ops{'sleepmin'} || 10;
our $sleepmax = $ops{'sleepmax'} || 30;
# Google Calendar account
our $username = $ops{'user'} or die("Need --user <user\@gmail.com>\n");
$username .= '@gmail.com' unless $username =~ /\@/;
our $password = $ops{'pass'} || password();
# Private XML URL to Google Calendar
# Replace 'basic' with 'full' at the end of URL
our $calurl = 'http://www.google.com/calendar/feeds/itgi86v0fbqdjocb9ats8jjmnk%40group.calendar.google.com/private-c50fb8900627befa8fe49c4f3bc7149b/full';
# Name of Google Calendar (deprecated)
our $calname = 'EMBL HD';
# Timezone of the Google Calendar
our $timezone = 'Europe/Berlin';
# EMBL HD seminar events page
# Past events (2009)
# our $pageurl = 'http://www.embl.de/research/seminars/index.php?p_eventType=SEM&p_outstation=HD&p_timeRange=PAST&p_type=1&p_byWhat=year&p_year=2009&submit_seminars=+#';
# Future events (default)
our $pageurl = "http://www.embl.de/research/seminars";
# EMBL returns different results when not coming from intranet
#our $pageurl = "http://intranet.embl.de/research/seminars";
our %months = (
'January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
'September' => 9,
'October' => 10,
'November' => 11,
'December' => 12,
);
# Flush stdout
$| = 1;
################################################################################
print "Connecting to Google Calendar ...";
my $emblcal = initgcal();
print "\n";
print "Parsing EMBL Events ...\n";
vialwp($pageurl, $emblcal);
`date >> ~/.emblcal.log`;
exit;
################################################################################
sub initgcal {
our $calurl;
our $calname;
our $username;
our $password;
$calname = $_[0] if $_[0];
# Create calendar by URL
my $cal = new Net::Google::Calendar(
url => $calurl,
);
$cal->login($username, $password);
return $cal;
# Scan calendars and match the right name (deprectated)
my @cals = $cal->get_calendars;
my ($embl) = grep { $_->title eq $calname } @cals;
$cal->set_calendar($embl);
return $cal;
}
sub vialwp {
my ($url, $cal) = @_;
our %months;
# TODO DEL
my ($dstart, $dend) = create_dates("wednesday 5 January 2011 11:00");
post_event($cal, "title", $dstart, $dend, "where", "speaker");
my $doc = get($url) or warn("Cannot fetch URL: $url");
return unless $doc;
my $re = '<div class="seminars_details_text".*?>(.*?)</div>';
while ($doc =~ /$re/gsm) {
my $m = $1;
$m =~ s/<br>/\n/g;
$m =~ s/<.*?>//g;
$m =~ s/^Host:.*?$//m;
$m =~ s/[\"\t]//g;
$m =~ s/\n+/\n/g;
my (undef, @fields) = split "\n", $m;
my $date = shift @fields;
my $title = shift @fields;
my $where = pop @fields;
my $speaker = join(' ', @fields);
my ($dstart, $dend) = create_dates($date);
next unless $dstart;
# Too far away?
# return if $dstart > DateTime->now + new DateTime::Duration(months=>1);
post_event($cal, $title, $dstart, $dend, $where, $speaker);
}
}
sub create_dates {
my ($date) = @_;
our %months;
our $timezone;
return if $date =~ /cancelled/i;
return if $date =~ /postponed/i;
$date =~ s/,//g;
my ($wday, $nday, $month, $year, $tstart) = split ' ', $date;
my $nmonth = $months{$month};
my ($hstart, $minute) = split ':', $tstart;
my $dstart = new DateTime(
year=>$year,month=>$nmonth,day=>$nday,
hour=>$hstart,minute=>$minute,
);
$dstart->set_time_zone($timezone) if $timezone;
my $dend = $dstart + new DateTime::Duration(hours => 1);
return ($dstart, $dend);
}
sub post_event {
my ($cal, $what, $dstart, $dend, $where, $who) = @_;
my $entry;
# When searching, use UTC time
our $timezone;
my $tz = DateTime::TimeZone->new( name => $timezone);
my $offset = DateTime::Duration->new(
seconds=>$tz->offset_for_datetime($dstart));
($entry) =
$cal->get_events('start-min'=>$dstart-$offset,
'start-max'=>$dend-$offset);
my $exists = 0;
if ($entry) {
$exists = 1;
print "Existing event\n";
} else {
print "New event\n";
$entry = Net::Google::Calendar::Entry->new;
}
$entry->title("$what, $who");
# $entry->content($who);
$entry->location($where);
# $entry->transparency('transparent');
$entry->status('confirmed');
# $dstart->set_time_zone('Europe/Berlin');
# $dend->set_time_zone('Europe/Berlin');
$entry->when($dstart, $dend);
print join("\n", ($what, $who, $dstart, $dend, $where)), "\n";
my $success = 0;
if ($exists) {
print "Updating: ";
$success = $cal->update_entry($entry);
} else {
print "Adding: ";
$success = $cal->add_entry($entry);
}
print $success ? "Success\n" : "Failed\n";
print "\n";
# Sleep between $sleepmin and $sleepmax seconds
sleep $sleepmin + int rand ($sleepmax-$sleepmin);
}
__END__