Created
October 16, 2013 13:01
-
-
Save gms8994/7007314 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| use strict; | |
| use warnings; | |
| use Data::Dumper; | |
| use HTML::Entities; | |
| use iCal::Parser; | |
| use LWP::UserAgent::Cached; | |
| use Text::Table; | |
| my $cache_dir = '/tmp/lwp-cache'; | |
| my @calendars = qw| | |
| http://www.google.com:80/finance/events?q=NYSE:KO&output=ical | |
| |; | |
| my $now = DateTime->now( time_zone => 'America/New_York' ); | |
| my $tb = Text::Table->new({ }, { align => "right" }); | |
| my $parser = iCal::Parser->new(); | |
| foreach my $calendar_url (@calendars) { | |
| $parser->parse_strings(get($calendar_url)); | |
| } | |
| my $combined = $parser->calendar; | |
| my @events_list; | |
| my $events = $combined->{events}; | |
| my %summaries; | |
| my %summaries_seen; | |
| foreach my $year (keys %{$events}) { | |
| foreach my $month (keys %{$events->{$year}}) { | |
| foreach my $day (keys %{$events->{$year}{$month}}) { | |
| foreach my $uuid (keys %{$events->{$year}{$month}{$day}}) { | |
| my $event = $events->{$year}{$month}{$day}{$uuid}; | |
| my $summary = $event->{SUMMARY}; | |
| my $start = $event->{DTSTART}; | |
| my $end = $event->{DTEND}; | |
| my $delta = $start->delta_days(DateTime->today()); | |
| next if (DateTime->compare($start, DateTime->today()) == -1); | |
| next if ($delta->in_units("days") > 14); | |
| $summaries{$summary}++; | |
| push(@events_list, [ $summary, $start, $end ]); | |
| } | |
| } | |
| } | |
| } | |
| foreach my $event (sort { DateTime->compare($a->[1], $b->[1]) } @events_list) { | |
| next if ($summaries_seen{$event->[0]}); | |
| my $summary = $event->[0]; | |
| if ($summaries{$summary} > 1) { | |
| $summary .= ' (multiple)'; | |
| } | |
| my $delta = $event->[1]->subtract_datetime($now); | |
| my ($days, $hours, $minutes) = $delta->in_units('days', 'hours', 'minutes'); | |
| my $delta_text = 'd'; | |
| if ($days == 0) { | |
| $delta = $delta->in_units('hours'); | |
| $delta_text = 'h'; | |
| } else { | |
| $delta = $delta->in_units('days'); | |
| } | |
| $tb->load([ decode_entities($summary), $delta . $delta_text ]); | |
| $summaries_seen{$event->[0]}++; | |
| } | |
| print $tb; | |
| sub get { | |
| my ($url) = @_; | |
| mkdir($cache_dir) unless -d $cache_dir; | |
| my $ua = new LWP::UserAgent::Cached(cache_dir => $cache_dir); | |
| $ua->show_progress(1); | |
| my $response = $ua->get($url); | |
| if ($response->is_success()) { | |
| return $response->content(); | |
| } else { | |
| warn $response->status_line; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment