| 1 | package WWW::BBC::TrackListings; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Web::Scraper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use URI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use FindBin::libs; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | use WWW::BBC::Track; | ||
| 11 | |||
| 12 | # ABSTRACT: Get track listings for BBC radio programmes | ||
| 13 | our $VERSION = '0.01'; # VERSION | ||
| 14 | |||
| 15 | has 'url' => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'Str', | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'tracks' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'ArrayRef[WWW::BBC::Track]', | ||
| 23 | traits => ['Array'], | ||
| 24 | handles => { | ||
| 25 | all_tracks => 'elements', | ||
| 26 | }, | ||
| 27 | builder => '_build_tracks', | ||
| 28 | lazy => 1, | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub _build_tracks { | ||
| 32 | my ( $self ) = @_; | ||
| 33 | |||
| 34 | my $tracks = scraper { | ||
| 35 | process "li.track", "tracks[]" => scraper { | ||
| 36 | process ".artist", artist => 'TEXT'; | ||
| 37 | process ".title", title => 'TEXT'; | ||
| 38 | }; | ||
| 39 | }; | ||
| 40 | |||
| 41 | my $res = $tracks->scrape( URI->new( $self->url ) ); | ||
| 42 | |||
| 43 | my @tracks; | ||
| 44 | for my $track ( @{ $res->{tracks} } ) { | ||
| 45 | push @tracks, WWW::BBC::Track->new({ | ||
| 46 | artist => $track->{artist}, | ||
| 47 | title => $track->{title}, | ||
| 48 | }); | ||
| 49 | } | ||
| 50 | |||
| 51 | return \@tracks; | ||
| 52 | } | ||
| 53 | |||
| 54 | __PACKAGE__->meta->make_immutable; | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | |||
| 59 | =pod | ||
| 60 | |||
| 61 | =head1 NAME | ||
| 62 | |||
| 63 | WWW::BBC::TrackListings - Get track listings for BBC radio programmes | ||
| 64 | |||
| 65 | =head1 VERSION | ||
| 66 | |||
| 67 | version 0.01 | ||
| 68 | |||
| 69 | =head1 SYNOPSIS | ||
| 70 | |||
| 71 | my $listings = WWW::BBC::TrackListings->new({ url => 'http://www.bbc.co.uk/programmes/b03c8l9l' }); | ||
| 72 | |||
| 73 | for my $track ( $listings->all_tracks ) { | ||
| 74 | say $track->artist; | ||
| 75 | say $track->title; | ||
| 76 | } | ||
| 77 | |||
| 78 | =head1 DESCRITPION | ||
| 79 | |||
| 80 | Scrape of BBC radio programme pages to generate track listings. | ||
| 81 | |||
| 82 | =head1 METHODS | ||
| 83 | |||
| 84 | =head2 all_tracks | ||
| 85 | |||
| 86 | Returns all L<WWW::BBC::Track> listings of this programme. | ||
| 87 | |||
| 88 | =head1 ATTRIBUTES | ||
| 89 | |||
| 90 | =head2 url | ||
| 91 | |||
| 92 | =head1 AUTHOR | ||
| 93 | |||
| 94 | Adam Taylor <ajct@cpan.org> | ||
| 95 | |||
| 96 | =head1 COPYRIGHT AND LICENSE | ||
| 97 | |||
| 98 | This software is copyright (c) 2013 by Adam Taylor. | ||
| 99 | |||
| 100 | This is free software; you can redistribute it and/or modify it under | ||
| 101 | the same terms as the Perl 5 programming language system itself. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | __END__ | ||