use v6; my $requested_date = Date.today; if @*ARGS[0] { $requested_date = Date.new(@*ARGS[0]); } # first, obtain the current calendar from the Geneva web site run("curl -s http://www.ville-geneve.ch/plan-ville/sports/piscine-vernets/ -o /tmp/webpage.html"); my $htmldoc = open "/tmp/webpage.html", :r; my $filetodownload; for $htmldoc.lines -> $line { if $line ~~ / 'href="' (<[a..z A..Z \/ \_]>* 'Horaires' .*? ) '"' / { $filetodownload = $/[0]; } } close $htmldoc; unless $filetodownload { die "Could not find current calendar file."; } # Now, download and convert the PDF calendar my $pdfname = $filetodownload.split(/\//)[* - 1]; if !($pdfname.IO ~~ :e) { # Download pdf from site run("curl -s -O http://www.ville-geneve.ch$filetodownload"); } run("pdftotext -enc UTF-8 $pdfname /tmp/horaire.txt"); # Open the converted PDF my $input = open "/tmp/horaire.txt", :r; # Be liberal with the accents, so we accept months according to their # first few letters my @month_starts = ; my @current_dates; my $collect_mode = ; my Int $i = 0; my %vernets; my %varembe; for $input.lines -> $line { when $line ~~ / ^^ Semaine \s+ du \s+ $=(\d+) \s+ $=((\w+) \s+)? $=((\d+) \s+)? au \s+ $=(\d+) \s+ $=(\w+) \s+ $=(\d+) $$/ { my $startmonth = $; unless $startmonth { $startmonth = $; } my $startyear = $; unless $startyear { $startyear = $; } my $startmonthindex = 0; my $endmonthindex = 0; for @month_starts Z 1 .. Inf -> $mname, $index { if $startmonth.substr(0,$mname.chars) eq $mname { $startmonthindex = $index; } if $.substr(0,$mname.chars) eq $mname { $endmonthindex = $index; } } my $startdate = Date.new("$startyear",$startmonthindex,"$"); my $enddate = Date.new("$",$endmonthindex,"$"); @current_dates = $startdate .. $enddate; } when $line ~~ /PISCINE .* PISCINE .*/ { $collect_mode = ; $i = 0; } when $line ~~ /PISCINE \s DES \s VER.*/ { $collect_mode = ; $i = 0; } when $line ~~ /PISCINE \s DE \s VA.*/ { $collect_mode = ; $i = 0; } when $line ~~ /PAT / { $collect_mode = ; } when $line ~~ m/ ^^ $=([ | 'Ferm' . | \d+h\d+ \s+ '-' \s+ \d+h\d+ ]) \s+ $=([ | 'Ferm' . | \d+h\d+ \s+ '-' \s+ \d+h\d+ ]) $$ / { if $collect_mode eq { %vernets{@current_dates[$i]} = "$"; %varembe{@current_dates[$i]} = "$"; $i++; } } when $line ~~ m/ ^^ $=([ | 'Ferm' . | \d+h\d+ \s+ '-' \s+ \d+h\d+ ]) / { given $collect_mode { when { %vernets{@current_dates[$i]} = "$"; } when { %varembe{@current_dates[$i]} = "$"; } } $i++; } } say "Opening hours for Geneva swimming pools"; say "Date: $requested_date"; say "================"; say "Piscine des Vernets: " ~ %vernets{$requested_date}; say "Piscine de Varembe : " ~ %varembe{$requested_date};