| 1 | package WWW::PTV::Route; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use WWW::PTV::TimeTable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use HTML::TreeBuilder; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | our $STOP = {}; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our @ATTR = qw( id direction_out direction_in direction_out_link direction_in_link | |||||||||||||||||||||||||||||||||||||||||
| 14 | description_out description_in name operator operator_ph ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | foreach my $attr ( @ATTR ) { | |||||||||||||||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 20 | *{ __PACKAGE__ .'::'. $attr } = sub { | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my( $self, $val ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $self->{$attr} = $val if $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | return $self->{$attr} | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 20 | } | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my( $class, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $args{id} or croak 'Constructor failed: mandatory id argument not supplied'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | foreach my $attr ( @ATTR ) { $self->{$attr} = $args{$attr} } | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $self->{uri} = $args{uri}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $args{ua} ? weaken( $self->{ua} = $args{ua} ) : croak 'Mandatory argument ua not supplied'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return $self | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub get_inbound_tt { $_[0]->__get_tt( 'in' ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_inbound_tt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub get_outbound_tt { $_[0]->__get_tt( 'out' ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_outbound_tt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | sub __get_tt { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my( $self, $direction ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__get_tt: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return unless $direction =~ /(in|out)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $tt = $self->__request( ( $direction eq 'out' ? $self->{direction_out_link} : $self->{direction_in} ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $t = HTML::TreeBuilder->new_from_content( $tt ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | #my $tt = HTML::TreeBuilder->new_from_file( './metro_bus_route_235_tt_out_full' ); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $t = $tt; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | for ( $t->look_down( _tag => 'meta' ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if( ( defined $_->attr( 'http-equiv' ) ) and ( $_->attr( 'http-equiv' ) eq 'refresh' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_->attr("http-equiv") and $_->attr("http-equiv") eq "refresh"
MC/DC0%0/2
defined $_->attr("http-equiv") and $_->attr("http-equiv") eq "refresh"
defined $_->attr("http-equiv")
$_->attr("http-equiv") eq "refresh"
Truth table0%0/3
A and B
A= defined $_->attr("http-equiv")B= $_->attr("http-equiv") eq "refresh"
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ( my $url = $_->attr( 'content' ) ) =~ s/^.*url=//; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $url .= '&itdLPxx_scrollOffset=118'; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | print "Getting: $url\n"; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $t = HTML::TreeBuilder->new_from_content( $self->__request( $self->{uri}.'/tt/'.$url ) ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | last | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $tt = $t->look_down( _tag => 'img', title => 'Expand' ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $tt && $tt->attr( 'onclick' ) && $tt->attr( 'onclick' ) =~ /TTB_REQUEST/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$tt and $tt->attr("onclick") and $tt->attr("onclick") =~ /TTB_REQUEST/
$tt and $tt->attr("onclick")
MC/DC0%0/3
$tt and $tt->attr("onclick") and $tt->attr("onclick") =~ /TTB_REQUEST/
$tt
$tt->attr("onclick")
$tt->attr("onclick") =~ /TTB_REQUEST/
Truth table0%0/4
A and B and C
A= $ttB= $tt->attr("onclick")C= $tt->attr("onclick") =~ /TTB_REQUEST/
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ( $tt = $tt->attr( 'onclick' ) ) =~ s/^.*\('//; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $tt =~ s/'.*$//; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $t = HTML::TreeBuilder->new_from_content( $self->__request( "http://tt.ptv.vic.gov.au/tt/$tt" ) ) | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $t = $t->look_down( _tag => 'div', id => qr/tt(Bus|Tram|Train)/ ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my @stops = $t->look_down( _tag => 'div', class => qr/^ma_stop/ ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my @stop_names = map { $_->as_text } @stops; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my @stop_links = map { my ($r) = $_->look_down( _tag => 'a' )->attr( 'href' ) =~ /.*\/(\d*$)/ } @stops; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $stop_times; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $c = 0; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | foreach my $t ( $t->look_down( _tag => 'div', class => qr/^ttBodyN?TP$/ ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | #my @s = map { $_->as_text } $t->look_down( _tag => 'span' ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $s; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | foreach my $t ( $t->look_down( _tag => 'span' ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my ( $h, $m ) = split /:/, $t->as_text; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | push @{ $s }, $h and next if $h !~ /\d/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$h =~ /\d/ || push(@{$s;}, $h) && (next)
push @{$s;}, $h and next
MC/DC0%0/2
$h =~ /\d/ || push(@{$s;}, $h) && (next)
$h =~ /\d/
push @{$s;}, $h
Truth table0%0/3
A || B
A= $h =~ /\d/B= push @{$s;}, $h
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $is_pm = $t->look_down( _tag => 'b' ); | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | push @{ $s }, ( $h == 12 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | ? ( $is_pm ? "$h:$m" : "00:$m" ) | ||||||||||||||||||||||||||||||||||||||||||
| 89 | : ( $is_pm ? $h + 12 .":$m" : "$h:$m" ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $self->{ STOP }->{ $stop_links[$c] }->{ $direction } = $s; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->{ STOP }->{ $stop_links[$c] }->{ name } = $stop_names[$c]; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $c++; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | push @{ $stop_times }, $s | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $ret = WWW::PTV::TimeTable->new( \@stop_names, \@stop_links, $stop_times ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $self->{timetable}->{$direction} = $ret; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | sub __request { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my($self,$uri) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__request: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | my $res = ( $uri !~ /^http:/ | ||||||||||||||||||||||||||||||||||||||||||
| 107 | ? $self->{ua}->get( $self->{uri} . $uri ) | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | : $self->{ua}->get( $uri ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return $res->content if $res->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | croak 'Unable to retrieve content: ' . $res->status_line | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | sub get_stop_names_and_ids { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my ($self, $direction) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_stop_names_and_ids: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $direction ||= 'out'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$direction ||= "out"
MC/DC0%0/1
$direction ||= "out"
$direction
Truth table0%0/2
A
A= $direction
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $self->{timetable}->{$direction} || $self->__get_tt($direction); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return $self->{timetable}->{$direction}->stop_names_and_ids; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||||||||||||