blib/lib/WWW/Hashdb.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 71 CC 3 SCAR 16.7
CC
3
Cov
0%
search
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.3
1 package WWW::Hashdb;
2  
3 use warnings;
4 use strict;
5  
6 use Moose;
7 use Params::Validate;
8 use WWW::Mechanize;
9 use Web::Scraper;
10  
11 =head1 NAME
12  
13 WWW::Hashdb - search by http://hashdb.com/.
14  
15 =head1 VERSION
16  
17 Version 0.01
18  
19 =cut
20  
21 our $VERSION = '0.01';
22  
23 =head1 SYNOPSIS
24  
25 use WWW::Hashdb;
26 use XXX;
27  
28 my $hashdb = WWW::Hashdb->new( limit => 10 );
29 my @items = $hashdb->search("BURST CITY");
30 XXX @items;
31  
32 =head1 EXPORT
33  
34 none.
35  
36 =head1 FUNCTIONS
37  
38 =head2 limit
39  
40 =cut
41  
42 has 'limit' => ( is => 'rw', isa => 'Int', default => 10 );
43  
44 =head2 search
45  
46 =cut
47  
48 sub search {
49 my ($self, $word) = validate_pos(@_, 1, 1);
50  
51 # http://hashdb.com/search.php?q=SYMPHONY+X&p=1&s=u
52 # q = query, words
53 # p = page
54 # s = ???
55  
56 my @items;
57  
58 {
59 my @pages = do {
60 my $first = URI->new("http://hashdb.com/search.php");
61 $first->query_form( { q => $word, p => 1 } );
62 $first;
63 };
64  
65 #warn $uri->as_string;
66 my $item = scraper {
67 process "p.searchList";
68 process 'input', ignore => '@value';
69 };
70 my $lists = scraper {
71 # <a href="search.php?q=aho&amp;p=2&amp;s=u" title="次のページへ" accesskey="N">次へ(<span class="underline">N</span>)</a>
72  
73 process 'a[accesskey="N"]',
74 "next" => '@href';
75 process 'form[name="download"]>p',
76 "items[]" => $item;
77 #result 'items';
78 };
79  
80 while (my $uri = shift @pages) {
81 my $items_ref = $lists->scrape( $uri );
82  
83 push @items, @{$items_ref->{items} || []};
84  
85 last if $self->limit > 0 and @items >= $self->limit;
86 push @pages, $items_ref->{next};
87 }
88 }
89  
90 for my $item (@items) {
91 my @parts = split(/,/, $item->{ignore});
92 # 先頭のフィールド「名前」には「,」がデリミタとしてではなく表れる場合がある。
93 until (@parts == 8) {
94 $parts[0] = join(',', @parts[0 .. 1]);
95 splice(@parts, 1, 1);
96 }
97  
98 # (アニメ)[ラストエグザイル LASTEXILE] (OP) Cloud Age Symphony.mp3,kzELjn4dD0,0,0,6d3d7136e9a7753108aa44e4a20526b7,0,1,0
99 $item->{name} = $parts[0];
100 $item->{trip} = $parts[1];
101 $item->{hash} = $parts[4];
102 $item->{fetch} = $item->{hash} ? join(",", "", "", 0, 0, $item->{hash}, 0) : undef;
103 }
104  
105 return @items;
106 }
107  
108 =head1 AUTHOR
109  
110 Tomohiro Hosaka, C<< <bokutin at cpan.org> >>
111  
112 =head1 COPYRIGHT & LICENSE
113  
114 Copyright 2008 Tomohiro Hosaka, all rights reserved.
115  
116 This program is free software; you can redistribute it and/or modify it
117 under the same terms as Perl itself.
118  
119 =cut
120  
121 1;