| 1 | package WWW::Oxontime; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.014000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use parent qw/Exporter/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use constant +{ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | NEXTBUS_FROM_HEADINGTON_CAMPUS => 'headington', | |||||||||
| 10 | NEXTBUS_FROM_HARCOURT_HILL => 'harcourt', | ||||||||||
| 11 | NEXTBUS_FROM_MARSTON_ROAD => 'marston', | ||||||||||
| 12 | NEXTBUS_FROM_WHEATLEY_CAMPUS => 'wheatley', | ||||||||||
| 13 | NEXTBUS_FROM_CRESCENT_HALL => 'crescent', | ||||||||||
| 14 | NEXTBUS_FROM_PAUL_KENT_HALL => 'paulkent', | ||||||||||
| 15 | NEXTBUS_FROM_SLADE_PARK => 'sladepark', | ||||||||||
| 16 | NEXTBUS_FROM_CITY_CENTRE => 'citycentre', | ||||||||||
| 17 | 1 | }; | |||||||||
| 18 | |||||||||||
| 19 | 1 | my @CONSTANTS = | |||||||||
| 20 | qw/NEXTBUS_FROM_HEADINGTON_CAMPUS | ||||||||||
| 21 | NEXTBUS_FROM_HARCOURT_HILL | ||||||||||
| 22 | NEXTBUS_FROM_MARSTON_ROAD | ||||||||||
| 23 | NEXTBUS_FROM_WHEATLEY_CAMPUS | ||||||||||
| 24 | NEXTBUS_FROM_CRESCENT_HALL | ||||||||||
| 25 | NEXTBUS_FROM_PAUL_KENT_HALL | ||||||||||
| 26 | NEXTBUS_FROM_SLADE_PARK | ||||||||||
| 27 | NEXTBUS_FROM_CITY_CENTRE/; | ||||||||||
| 28 | |||||||||||
| 29 | 1 | our $VERSION = '0.002'; | |||||||||
| 30 | 1 | our @EXPORT_OK = (qw/stops_for_route departures_for_stop nextbus_from_to/, @CONSTANTS); | |||||||||
| 31 | 1 | our @EXPORT = (); | |||||||||
| 32 | 1 | our %EXPORT_TAGS = (all => [@EXPORT_OK], constants => [@CONSTANTS]); | |||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | use HTML::TreeBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | our $STOPS_URL = 'http://www.buscms.com/Nimbus/operatorpages/widgets/departureboard/ssi.aspx?method=updateRouteStops&routeid=%d&callback=cb&_=%d'; | |||||||||
| 40 | 1 | our $DEPARTS_URL = 'http://www.buscms.com/api/REST/html/departureboard.aspx?clientid=Nimbus&stopid=%d&format=jsonp&cachebust=123&sourcetype=siri&requestor=Netescape&includeTimestamp=true&_=%d'; | |||||||||
| 41 | 1 | our $DEPART_TIME_FORMAT = '%d/%m/%Y %T'; | |||||||||
| 42 | 1 | our $NEXTBUS_URL = 'http://nextbus.brookes.ac.uk/%s?format=json&%s'; | |||||||||
| 43 | |||||||||||
| 44 | 1 | our $ht = HTTP::Tiny->new(agent => "WWW-Oxontime/$VERSION"); | |||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub stops_for_route { | ||||||||
|
Subroutine
stops_for_route: called CC=3 SCAR=15.7
Pod
documented
| |||||||||||
| 47 | 1 | my ($route_id) = @_; | |||||||||
| 48 | 1 | my $url = sprintf $STOPS_URL, int $route_id, time; | |||||||||
| 49 | 1 | my $result = $ht->get($url); | |||||||||
| 50 | 1 | ▶ | die $result->{reason} unless $result->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | my $json = $result->{content}; | |||||||||
| 52 | 0 | $json = substr $json, 3, (length $json) - 5; | |||||||||
| 53 | 0 | my $stops = decode_json($json)->{stops}; | |||||||||
| 54 | 0 | ▶ | wantarray ? @$stops : $stops | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub departures_for_stop { | ||||||||
|
Subroutine
departures_for_stop: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 58 | 0 | my ($stop_id) = @_; | |||||||||
| 59 | 0 | my $url = sprintf $DEPARTS_URL, int $stop_id, time; | |||||||||
| 60 | 0 | my $result = $ht->get($url); | |||||||||
| 61 | 0 | ▶ | die $result->{reason} unless $result->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | my $content = $result->{content}; | |||||||||
| 63 | 0 | $content =~ s/\s/ /g; # replaces tabs with spaces | |||||||||
| 64 | 0 | $content = JSON->new->allow_nonref(1)->decode(qq/"$content"/); | |||||||||
| 65 | 0 | my $html = HTML::TreeBuilder->new_from_content($content); | |||||||||
| 66 | |||||||||||
| 67 | 0 | my @lines = $html->look_down(class => qr/\browServiceDeparture\b/); | |||||||||
| 68 | my @result = map { | ||||||||||
| 69 | 0 | my @cells = $_->find('td'); | |||||||||
| 70 | 0 | my $departs = $cells[2]->attr('data-departureTime'); | |||||||||
| 71 | +{ | ||||||||||
| 72 | 0 | service => $cells[0]->as_trimmed_text, | |||||||||
| 73 | destination => $cells[1]->as_trimmed_text, | ||||||||||
| 74 | departs => Time::Piece->strptime($departs, $DEPART_TIME_FORMAT), | ||||||||||
| 75 | } | ||||||||||
| 76 | } @lines; | ||||||||||
| 77 | 0 | ▶ | wantarray ? @result : \@result | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub nextbus_from_to { | ||||||||
|
Subroutine
nextbus_from_to: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 81 | 0 | my ($from, $to) = @_; | |||||||||
| 82 | 0 | my $data = $ht->www_form_urlencode({ destination => $to}); | |||||||||
| 83 | 0 | my $url = sprintf $NEXTBUS_URL, $from, $data; | |||||||||
| 84 | 0 | my $result = $ht->get($url); | |||||||||
| 85 | 0 | ▶ | die $result->{reason} unless $result->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | my $content = $result->{content}; | |||||||||
| 87 | 0 | my $departures = JSON->new->decode($content)->{departures}; | |||||||||
| 88 | 0 | ▶ | wantarray ? @$departures : $departures | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 1 | 1; | |||||||||
| 92 | __END__ | ||||||||||