Last active
August 25, 2018 04:46
-
-
Save s1037989/7a0ed9f888acf70966a4a3aa61bb349f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $dry = 1; | |
# Remove query string from files on disk | |
$home->list_tree->slice(0)->each(sub{ | |
/^(.*?)\?|\@/ or return; | |
say "$_ => $1"; | |
return if $dry; | |
$_->move_to($1); | |
}); | |
# Go thru all text files on disk that aren't css or js | |
$home->list_tree->grep(sub{-T $_ && not($_->ext =~ /(css|js)/)})->slice(0)->each(sub{ | |
my $dom = x($_->slurp); | |
# Find href and replace with Location header or local URL to downloaded content | |
$dom->find('[href]')->each(sub{ | |
say $_->attr('href'); | |
my $href = Mojo::URL->new($_->attr('href')); | |
return if $href->host =~ /hubspot\.(com|net)$/; | |
follow($_->attr('href')) unless $dry; | |
}); | |
# Find href and replace with Location header or local URL to downloaded content | |
$dom->find('[src]')->each(sub{ | |
say $_->attr('src'); | |
my $src = Mojo::URL->new($_->attr('src')); | |
return if $src->host =~ /hubspot\.(com|net)$/; | |
follow($_->attr('src')) unless $dry; | |
}); | |
$_->spurt("$dom") unless $dry; | |
}); | |
# Go thru all text files on disk that are css | |
$home->list_tree->grep(sub{-T $_ && $_->ext eq 'css')})->slice(0)->each(sub{ | |
my $css = $_->slurp; | |
1 while ( $css =~ s/url\(["']?(https:\/\/.*?)["']?\)/follow($1)/eg ); | |
}); | |
# Follow a URL. Take a URL ... | |
sub follow { | |
my $url = Mojo::URL->new(shift); | |
my $res = g($url); | |
if ( $res->code == 301 || $res->code == 302 ) { | |
# and return the location header ... | |
return $res->headers->location; | |
} else { | |
# or download the link and return the local path | |
my $home = Mojo::Home->new; | |
$home->rel_file($url->path)->dirname->make_path; | |
$res->content->asset->move_to($home->rel_file($url->path)); | |
return $url->path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment