| 1 | package WWW::Scramble; | ||
| 2 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use WWW::Scramble::Entry; | ||
| 4 | use WWW::Scramble::Handler; | ||
| 5 | use WWW::Mechanize; | ||
| 6 | use File::Find::Rule; | ||
| 7 | use YAML; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | WWW::Scramble - Get only needed from the Web page | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.04 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.04'; | ||
| 20 | |||
| 21 | has mech => ( is => 'ro', isa => 'WWW::Mechanize', default => sub { WWW::Mechanize->new } ); | ||
| 22 | has handler => ( | ||
| 23 | is => 'rw', isa => 'WWW::Scramble::Handler', default => sub { WWW::Scramble::Handler->new } | ||
| 24 | ); | ||
| 25 | has assets => ( is => 'rw', isa => 'HashRef[Str]', default => sub { {} } ); | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | Quick summary of what the module does. | ||
| 30 | |||
| 31 | Perhaps a little code snippet. | ||
| 32 | |||
| 33 | use WWW::Scramble; | ||
| 34 | |||
| 35 | my $foo = WWW::Scramble->new(); | ||
| 36 | my $e1 = $foo->fetchfile('a.html', { xtitle => '//title', xcontent => '//body' }); | ||
| 37 | my $e2 = $foo->fetchnews('http://foo.bar.com/foobar.html'); | ||
| 38 | print $e1->title->as_trimmed_text | ||
| 39 | ... | ||
| 40 | |||
| 41 | =head1 FUNCTIONS | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | =head2 BUILD | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub BUILD { | ||
| 50 | my $self = shift; | ||
| 51 | $self->loadassets("./"); | ||
| 52 | }; | ||
| 53 | |||
| 54 | sub _fetch { | ||
| 55 | my ($self, $url, $attr_ref) = @_; | ||
| 56 | $self->mech->get($url); | ||
| 57 | return $self->mech->response->status_line | ||
| 58 | unless $self->mech->success; | ||
| 59 | my %attr; | ||
| 60 | if ($attr_ref and ref $attr_ref eq 'HASH') { | ||
| 61 | %attr = %{$attr_ref}; | ||
| 62 | } else { # no specified hash, so... | ||
| 63 | my %h = %{$self->assets}; | ||
| 64 | for my $key (keys %h) { | ||
| 65 | my $handle = delete $h{$key}->[0]->{handle}; | ||
| 66 | next unless $handle and $url =~ m/$handle/; | ||
| 67 | %attr = %{$h{$key}->[0]}; | ||
| 68 | } | ||
| 69 | }; | ||
| 70 | for my $key (keys %attr) { | ||
| 71 | $self->handler->set_asset($key, $attr{$key}); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 loadassets | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub loadassets { | ||
| 80 | my($self, $assetsdir) = @_; | ||
| 81 | |||
| 82 | my $rule = File::Find::Rule->new; | ||
| 83 | $rule->name("*.yaml"); | ||
| 84 | for my $file ($rule->in($assetsdir)) { | ||
| 85 | my $base = File::Basename::basename($file); | ||
| 86 | my @data = YAML::LoadFile($file); | ||
| 87 | |||
| 88 | $self->assets->{$base} = \@data; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 fetch | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub fetch { | ||
| 97 | my $self = shift; | ||
| 98 | $self->_fetch(@_); | ||
| 99 | return WWW::Scramble::Entry->new ( uri => $self->mech->uri, _rawdata => $self->mech->content, _handler => $self->handler ); | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 fetchfile | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub fetchfile { | ||
| 107 | my ($self, $file, $attr_ref) = @_; | ||
| 108 | $file = 'file://'.$file unless ($file =~ m!^file://!); | ||
| 109 | return $self->fetch($file, $attr_ref); | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 fetchnews | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub fetchnews { | ||
| 117 | my $self = shift; | ||
| 118 | $self->_fetch(@_); | ||
| 119 | return WWW::Scramble::Entry->new ( uri => $self->mech->uri, _rawdata => $self->mech->content, _handler => $self->handler ); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | =head1 AUTHOR | ||
| 124 | |||
| 125 | Cheng-Lung Sung, C<< <clsung at cpan.org> >> | ||
| 126 | |||
| 127 | =head1 BUGS | ||
| 128 | |||
| 129 | Please report any bugs or feature requests to C<bug-www-scramble at rt.cpan.org>, or through | ||
| 130 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Scramble>. I will be notified, and then you'll | ||
| 131 | automatically be notified of progress on your bug as I make changes. | ||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | =head1 SUPPORT | ||
| 137 | |||
| 138 | You can find documentation for this module with the perldoc command. | ||
| 139 | |||
| 140 | perldoc WWW::Scramble | ||
| 141 | |||
| 142 | |||
| 143 | You can also look for information at: | ||
| 144 | |||
| 145 | =over 4 | ||
| 146 | |||
| 147 | =item * RT: CPAN's request tracker | ||
| 148 | |||
| 149 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Scramble> | ||
| 150 | |||
| 151 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 152 | |||
| 153 | L<http://annocpan.org/dist/WWW-Scramble> | ||
| 154 | |||
| 155 | =item * CPAN Ratings | ||
| 156 | |||
| 157 | L<http://cpanratings.perl.org/d/WWW-Scramble> | ||
| 158 | |||
| 159 | =item * Search CPAN | ||
| 160 | |||
| 161 | L<http://search.cpan.org/dist/WWW-Scramble/> | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | |||
| 166 | =head1 ACKNOWLEDGEMENTS | ||
| 167 | |||
| 168 | |||
| 169 | =head1 COPYRIGHT & LICENSE | ||
| 170 | |||
| 171 | Copyright 2009 Cheng-Lung Sung, all rights reserved. | ||
| 172 | |||
| 173 | This program is free software; you can redistribute it and/or modify it | ||
| 174 | under the same terms as Perl itself. | ||
| 175 | |||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | no Moose; | ||
| 180 | __PACKAGE__->meta->make_immutable; | ||
| 181 | 1; # End of WWW::Scramble | ||