Skip to content

Instantly share code, notes, and snippets.

@pavelsr
Created May 13, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavelsr/90181be7217e6957aa126b02c7c28d42 to your computer and use it in GitHub Desktop.
Save pavelsr/90181be7217e6957aa126b02c7c28d42 to your computer and use it in GitHub Desktop.
put it in same level with .htaccess
use strict;
use warnings;
use File::Slurper 'read_lines';
use LWP::UserAgent ();
use JSON;
use feature 'say';
use Term::Table;
use List::Util qw(uniq);
use Data::Dumper;
die "No url provided" if !defined $ARGV[0];
my $filename = '.htaccess';
my @content = read_lines($filename, 'cp-1251');
my $lwp = LWP::UserAgent->new;
$lwp->agent('Mozilla/5.0');
$lwp->show_progress(1);
push @{ $lwp->requests_redirectable }, 'POST';
say "Checking for htaccess url : ".$ARGV[0];
my $url = $ARGV[0];
my $htaccess = $content[0];
my @rows;
my @all;
my $i = 1;
my $total = scalar @content;
say "Total lines in .htaccess : ".$total;
for my $htaccess_line (@content) {
my $h = check($url, $htaccess_line);
if ( $h->{output_url} ) {
push @rows, [ $h->{output_url}, $htaccess_line ];
}
push @all, $h->{output_url};
say "$i/$total";
$i++;
}
my $table = Term::Table->new(
header => ['output_url', 'rule'],
rows => \@rows
);
say "Uniq urls : ".Dumper [ uniq @all ];
say $_ for $table->render;
sub check {
my ( $url, $htaccess ) = @_;
my $req = HTTP::Request->new( 'POST', 'https://htaccess.madewithlove.be/api' );
$req->header( 'Content-Type' => 'application/json' );
my $data = {
url => $url,
htaccess => $htaccess
};
$req->content( encode_json $data );
my $response = $lwp->request($req);
my $result_hash = decode_json ($response->decoded_content);
return $result_hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment