| 1 | package WWW::Metalgate::Year; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::Types::URI qw(Uri FileUri DataUri); | ||
| 8 | use Encode; | ||
| 9 | use IO::All; | ||
| 10 | use Text::Trim; | ||
| 11 | use Web::Scraper; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | WWW::Metalgate::Year | ||
| 16 | |||
| 17 | =head1 VERSION | ||
| 18 | |||
| 19 | Version 0.02 | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | our $VERSION = '0.02'; | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use WWW::Metalgate::Year; | ||
| 28 | use XXX; | ||
| 29 | |||
| 30 | my $year = WWW::Metalgate::Year->new( year => 2003 ); | ||
| 31 | my @albums = $year->best_albums; | ||
| 32 | YYY @albums; | ||
| 33 | #--- | ||
| 34 | #album: EPICA | ||
| 35 | #artist: KAMELOT | ||
| 36 | #no: 1 | ||
| 37 | #year: 2003 | ||
| 38 | #--- | ||
| 39 | #album: DELIRIUM VEIL | ||
| 40 | #artist: TWILIGHTNING | ||
| 41 | #no: 2 | ||
| 42 | #year: 2003 | ||
| 43 | #--- | ||
| 44 | #... snip ... | ||
| 45 | |||
| 46 | =head1 FUNCTIONS | ||
| 47 | |||
| 48 | =head2 uri | ||
| 49 | |||
| 50 | =head2 year | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | has 'uri' => (is => 'rw', isa => Uri, coerce => 1); | ||
| 55 | has 'year' => (is => 'ro', isa => 'Int', required => 1); | ||
| 56 | |||
| 57 | with 'WWW::Metalgate::Role::Html'; | ||
| 58 | |||
| 59 | =head2 BUILD | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub BUILD { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | # http://www.metalgate.jp/best1992.htm | ||
| 67 | $self->uri( sprintf("http://www.metalgate.jp/best%s.htm", $self->year) ); | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 best_albums | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub best_albums { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | my $album = sub { | ||
| 78 | my $node = shift; | ||
| 79 | return () unless $node->as_text =~ m/No\.\d+/ and $node->find_by_tag_name('img'); | ||
| 80 | |||
| 81 | my ($no, $artist, $album) = ($node->as_text =~ m/No\.(\d+)\s*(.*?)\s*\/\s*(.*?)\s*$/); | ||
| 82 | return { | ||
| 83 | no => $no, | ||
| 84 | album => $album, | ||
| 85 | artist => $artist, | ||
| 86 | year => $self->year, | ||
| 87 | description => trim( $node->right->right->as_text ), | ||
| 88 | #raw => $node->as_text, | ||
| 89 | }; | ||
| 90 | }; | ||
| 91 | my $tables = scraper { | ||
| 92 | process "table", | ||
| 93 | 'tables[]' => $album; | ||
| 94 | }; | ||
| 95 | my $data = $tables->scrape( $self->html ); | ||
| 96 | |||
| 97 | return @{$data->{tables}}; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 best_tunes | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub best_tunes { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | my $tune = sub { | ||
| 108 | my $node = shift; | ||
| 109 | return () unless $node->as_text =~ m/^No\.\d+/ and !$node->find_by_tag_name('img'); | ||
| 110 | |||
| 111 | my ($name, $artist) = split(/\s*\/\s*/, $node->address(".0.1")->as_text); | ||
| 112 | my $description = $node->right->as_text || $node->address(".1")->as_text; | ||
| 113 | return { | ||
| 114 | no => ($node->address(".0.0")->as_text =~ m/(\d+)/), | ||
| 115 | name => trim($name), | ||
| 116 | artist => trim($artist), | ||
| 117 | year => $self->year, | ||
| 118 | description => trim($description), | ||
| 119 | }; | ||
| 120 | }; | ||
| 121 | my $tables = scraper { | ||
| 122 | process "table", | ||
| 123 | 'tables[]' => $tune; | ||
| 124 | }; | ||
| 125 | my $data = $tables->scrape( $self->html ); | ||
| 126 | |||
| 127 | return @{$data->{tables}}; | ||
| 128 | } | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Tomohiro Hosaka, C<< <bokutin at cpan.org> >> | ||
| 133 | |||
| 134 | =head1 COPYRIGHT & LICENSE | ||
| 135 | |||
| 136 | Copyright 2008 Tomohiro Hosaka, all rights reserved. | ||
| 137 | |||
| 138 | This program is free software; you can redistribute it and/or modify it | ||
| 139 | under the same terms as Perl itself. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | 1; |