| 1 | #!/usr/bin/env perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use File::Basename; |
|---|
| 6 | use Web::Scraper; |
|---|
| 7 | use URI; |
|---|
| 8 | use DateTime; |
|---|
| 9 | use YAML; |
|---|
| 10 | |
|---|
| 11 | my $filename = basename(__FILE__); |
|---|
| 12 | my $url = $ARGV[0] |
|---|
| 13 | or die "Usage: $filename url\n"; |
|---|
| 14 | |
|---|
| 15 | my $result = scraper { |
|---|
| 16 | process '//title', 'title' => 'TEXT'; |
|---|
| 17 | process '//table[@class="information_trafic01"]//tr[position() != 1]', |
|---|
| 18 | 'entry[]' => scraper { |
|---|
| 19 | process '//th[@class="trafficinfo"]/text()', 'date' => ['TEXT', \&mk_date]; |
|---|
| 20 | process '//td[@class="trafficinfo"][1]/text()', 'title' => 'TEXT'; |
|---|
| 21 | process '//td[@class="trafficinfo"][2]/text()', 'body' => 'TEXT'; |
|---|
| 22 | }; |
|---|
| 23 | }->scrape( URI->new($url) ); |
|---|
| 24 | |
|---|
| 25 | $result->{link} = $url; |
|---|
| 26 | |
|---|
| 27 | binmode STDOUT, ":utf8"; |
|---|
| 28 | print YAML::Dump $result; |
|---|
| 29 | |
|---|
| 30 | sub mk_date { |
|---|
| 31 | my $input = shift; |
|---|
| 32 | return unless ($input =~ m!(\d+)/(\d+) (\d+):(\d+)!); |
|---|
| 33 | |
|---|
| 34 | my $month = $1; |
|---|
| 35 | my $day = $2; |
|---|
| 36 | my $hour = $3; |
|---|
| 37 | my $minute = $4; |
|---|
| 38 | |
|---|
| 39 | my $today = DateTime->now(time_zone => 'Asia/Tokyo')->truncate(to => 'day'); |
|---|
| 40 | my $this = $today->clone->set(month => $month, day => $day, |
|---|
| 41 | hour => $hour, minute => $minute |
|---|
| 42 | ); |
|---|
| 43 | my $last = $this->clone->subtract(years => 1); |
|---|
| 44 | my $next = $this->clone->add(years => 1); |
|---|
| 45 | my @date = sort { DateTime::Duration->compare($a->[1], $b->[1], $today) } |
|---|
| 46 | map { [$_->[0], $_->[1]->is_positive ? $_->[1] : $_->[1]->inverse ] } |
|---|
| 47 | map { [$_, $today - $_] } ($this, $last, $next); |
|---|
| 48 | |
|---|
| 49 | return $date[0]->[0]->iso8601(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|