| 1 | package WWW::Flatten; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use 5.010; | ||
| 6 | use Mojo::Base 'WWW::Crawler::Mojo'; | ||
| 7 | use Mojo::Util qw(md5_sum); | ||
| 8 | use Mojolicious::Types; | ||
| 9 | our $VERSION = '0.01'; | ||
| 10 | |||
| 11 | has urls => sub { {} }; | ||
| 12 | has filenames => sub { {} }; | ||
| 13 | has 'basedir'; | ||
| 14 | has is_target => sub { sub { 1 } }; | ||
| 15 | has 'normalize'; | ||
| 16 | has asset_name => sub { asset_number_generator(6) }; | ||
| 17 | has retrys => sub { {} }; | ||
| 18 | has max_retry => 3; | ||
| 19 | has types => sub { | ||
| 20 | my $types; | ||
| 21 | my %cat = %{Mojolicious::Types->new->types}; | ||
| 22 | while (my ($key, $val) = each %cat) { | ||
| 23 | $types->{$_} = $key for (map { s/\;.*$//; lc $_ } @$val); | ||
| 24 | } | ||
| 25 | return $types; | ||
| 26 | }; | ||
| 27 | |||
| 28 | sub asset_number_generator { | ||
| 29 | my $digit = (shift || 6); | ||
| 30 | my $num = 0; | ||
| 31 | return sub { | ||
| 32 | return sprintf("%0${digit}d", $num++); | ||
| 33 | }; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub asset_hash_generator { | ||
| 37 | my $len = (shift || 6); | ||
| 38 | my %uniq; | ||
| 39 | return sub { | ||
| 40 | my $md5 = md5_sum(shift); | ||
| 41 | my $len = $len; | ||
| 42 | my $key; | ||
| 43 | do { $key = substr($md5, 0, $len++) } while (exists $uniq{$key}); | ||
| 44 | $uniq{$key} = undef; | ||
| 45 | return $key; | ||
| 46 | }; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub init { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | for (keys %{$self->filenames}) { | ||
| 53 | $self->enqueue($_); | ||
| 54 | } | ||
| 55 | |||
| 56 | $self->on(res => sub { | ||
| 57 | my ($self, $discover, $job, $res) = @_; | ||
| 58 | |||
| 59 | $discover->(); | ||
| 60 | |||
| 61 | my $uri = $job->resolved_uri; | ||
| 62 | my $cont = $res->body; | ||
| 63 | my $type = $res->headers->content_type; | ||
| 64 | |||
| 65 | if ($type && $type =~ qr{text/(html|xml)}) { | ||
| 66 | my $base = $uri; | ||
| 67 | if (my $base_tag = $res->dom->at('base')) { | ||
| 68 | $base = resolve_href($base, $base_tag->attr('href')); | ||
| 69 | } | ||
| 70 | $cont = $res->dom; | ||
| 71 | $self->flatten_html($cont, $base); | ||
| 72 | $cont = $cont->to_string; | ||
| 73 | } elsif ($type && $type =~ qr{text/css}) { | ||
| 74 | $cont = $self->flatten_css($cont, $uri); | ||
| 75 | } | ||
| 76 | |||
| 77 | my $original = $job->original_uri; | ||
| 78 | |||
| 79 | $self->save($original, $cont); | ||
| 80 | say sprintf('created: %s => %s ', | ||
| 81 | $self->filenames->{$original}, $original); | ||
| 82 | }); | ||
| 83 | |||
| 84 | $self->on(refer => sub { | ||
| 85 | my ($self, $enqueue, $job, $context) = @_; | ||
| 86 | |||
| 87 | return unless ($self->is_target->($job, $context)); | ||
| 88 | |||
| 89 | my $uri = $job->resolved_uri; | ||
| 90 | |||
| 91 | if (my $cb = $self->normalize) { | ||
| 92 | $job->resolved_uri($uri = $cb->($uri)); | ||
| 93 | } | ||
| 94 | |||
| 95 | $self->_regist_asset_name($uri); | ||
| 96 | |||
| 97 | $enqueue->(); | ||
| 98 | }); | ||
| 99 | |||
| 100 | $self->on(error => sub { | ||
| 101 | my ($self, $msg, $job) = @_; | ||
| 102 | say $msg; | ||
| 103 | my $md5 = md5_sum($job->resolved_uri->to_string); | ||
| 104 | if (++$self->retrys->{$md5} < $self->max_retry) { | ||
| 105 | $self->requeue($job); | ||
| 106 | say "Re-scheduled"; | ||
| 107 | } | ||
| 108 | }); | ||
| 109 | |||
| 110 | $self->SUPER::init; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub get_href { | ||
| 114 | my ($self, $base, $uri) = @_; | ||
| 115 | my $fragment = ($uri =~ qr{(#.+)})[0] || ''; | ||
| 116 | my $abs = WWW::Crawler::Mojo::resolve_href($base, $uri); | ||
| 117 | if (my $cb = $self->normalize) { | ||
| 118 | $abs = $cb->($abs); | ||
| 119 | } | ||
| 120 | my $file = $self->filenames->{$abs}; | ||
| 121 | return './'. $file. $fragment if ($file); | ||
| 122 | return $abs. $fragment; | ||
| 123 | } | ||
| 124 | |||
| 125 | my %tag_attributes = %WWW::Crawler::Mojo::tag_attributes; | ||
| 126 | |||
| 127 | sub flatten_html { | ||
| 128 | my ($self, $dom, $base) = @_; | ||
| 129 | |||
| 130 | $dom->find(join(',', keys %tag_attributes))->each(sub { | ||
| 131 | my $dom = shift; | ||
| 132 | for (@{$tag_attributes{$dom->type}}) { | ||
| 133 | $dom->{$_} = $self->get_href($base, $dom->{$_}) if ($dom->{$_}); | ||
| 134 | } | ||
| 135 | }); | ||
| 136 | |||
| 137 | $dom->find('meta[http\-equiv=Refresh]')->each(sub { | ||
| 138 | my $dom = shift; | ||
| 139 | if (my $href = $dom->{content} && ($dom->{content} =~ qr{URL=(.+)}i)[0]) { | ||
| 140 | my $abs = $self->get_href($base, $1); | ||
| 141 | $dom->{content} =~ s{URL=(.+)}{ | ||
| 142 | 'URL='. $abs; | ||
| 143 | }e; | ||
| 144 | } | ||
| 145 | }); | ||
| 146 | |||
| 147 | $dom->find('base')->remove(); | ||
| 148 | |||
| 149 | $dom->find('style')->each(sub { | ||
| 150 | my $dom = shift; | ||
| 151 | my $cont = $dom->content; | ||
| 152 | $dom->content($self->flatten_css($cont, $base)); | ||
| 153 | }); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub flatten_css { | ||
| 157 | my ($self, $cont, $base) = @_; | ||
| 158 | $cont =~ s{url\(['"]?(.+?)['"]?\)}{ | ||
| 159 | 'url('. $self->get_href($base, $1). ')'; | ||
| 160 | }eg; | ||
| 161 | return $cont; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub save { | ||
| 165 | my ($self, $url, $content) = @_; | ||
| 166 | my $fullpath = $self->basedir. $self->filenames->{$url}; | ||
| 167 | open(my $OUT, utf8::is_utf8($content) ? '>:utf8' : '>', $fullpath); | ||
| 168 | print $OUT $content; | ||
| 169 | close($OUT); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _regist_asset_name { | ||
| 173 | my ($self, $uri) = @_; | ||
| 174 | if (!$self->filenames->{$uri}) { | ||
| 175 | $self->filenames->{$uri} = $self->asset_name->($uri); | ||
| 176 | my $ext = ($uri->path =~ qr{\.(\w+)$})[0] || do { | ||
| 177 | my $got = $self->ua->head($uri)->res->headers->content_type || ''; | ||
| 178 | $got =~ s/\;.*$//; | ||
| 179 | $self->types->{lc $got}; | ||
| 180 | }; | ||
| 181 | $self->filenames->{$uri} .= ".$ext" if ($ext); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | 1; | ||
| 186 | |||
| 187 | =head1 NAME | ||
| 188 | |||
| 189 | WWW::Flatten - Flatten a web pages deeply and make it portable | ||
| 190 | |||
| 191 | =head1 SYNOPSIS | ||
| 192 | |||
| 193 | use strict; | ||
| 194 | use warnings; | ||
| 195 | use utf8; | ||
| 196 | use 5.010; | ||
| 197 | use Mojo::URL; | ||
| 198 | use WWW::Flatten; | ||
| 199 | |||
| 200 | my $basedir = './github/'; | ||
| 201 | mkdir($basedir); | ||
| 202 | |||
| 203 | my $ext_regex = qr{\.(css|png|gif|jpeg|jpg|pdf|js|json)$}i; | ||
| 204 | |||
| 205 | my $bot = WWW::Flatten->new( | ||
| 206 | basedir => $basedir, | ||
| 207 | max_conn => 1, | ||
| 208 | wait_per_host => 3, | ||
| 209 | peeping_port => 3000, | ||
| 210 | depth => 3, | ||
| 211 | filenames => { | ||
| 212 | 'https://github.com' => 'index.html', | ||
| 213 | }, | ||
| 214 | is_target => sub { | ||
| 215 | my $uri = Mojo::URL->new(shift->resolved_uri); | ||
| 216 | |||
| 217 | if ($uri =~ $ext_regex) { | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | if ($uri->host eq 'assets-cdn.github.com') { | ||
| 222 | return 1; | ||
| 223 | } | ||
| 224 | |||
| 225 | return 0; | ||
| 226 | }, | ||
| 227 | normalize => sub { | ||
| 228 | my $uri = Mojo::URL->new(shift); | ||
| 229 | |||
| 230 | return $uri; | ||
| 231 | } | ||
| 232 | ); | ||
| 233 | |||
| 234 | $bot->crawl; | ||
| 235 | |||
| 236 | =head1 DESCRIPTION | ||
| 237 | |||
| 238 | WWW::Flatten is a web crawling tool for freezing pages into standalone. | ||
| 239 | |||
| 240 | This software is considered to be alpha quality and isn't recommended for regular usage. | ||
| 241 | |||
| 242 | =head1 ATTRIBUTES | ||
| 243 | |||
| 244 | =head1 METHODS | ||
| 245 | |||
| 246 | =head1 AUTHOR | ||
| 247 | |||
| 248 | Sugama Keita, E<lt>sugama@jamadam.comE<gt> | ||
| 249 | |||
| 250 | =head1 COPYRIGHT AND LICENSE | ||
| 251 | |||
| 252 | Copyright (C) jamadam | ||
| 253 | |||
| 254 | This program is free software; you can redistribute it and/or | ||
| 255 | modify it under the same terms as Perl itself. | ||
| 256 | |||
| 257 | =cut |