From 45f381b96d893461bce92b0e6357262615e557f5 Mon Sep 17 00:00:00 2001 From: RodAgrto Date: Thu, 2 Apr 2020 03:07:03 -0300 Subject: [PATCH] Update CPAN.pm Now Padre CPAN Tasks work with the metacpan api v1 --- lib/Padre/Task/CPAN.pm | 76 +++++++++++++----------------------------- 1 file changed, 23 insertions(+), 53 deletions(-) diff --git a/lib/Padre/Task/CPAN.pm b/lib/Padre/Task/CPAN.pm index 3e65813cb..75316a333 100644 --- a/lib/Padre/Task/CPAN.pm +++ b/lib/Padre/Task/CPAN.pm @@ -88,54 +88,22 @@ sub metacpan_autocomplete { # Create an array of query keywords that are separated by spaces my @query = split( /\s+/, $query ); - # The documentation Module-Name that should be analyzed - my $should = [ - map { - ( { field => { 'documentation.analyzed' => "$_*" } }, - { field => { 'documentation.camelcase' => "$_*" } } - ) - } - grep { - $_ - } @query - ]; - push @{$should}, map { ( { field => { 'author' => "$_" } }, ) } map { uc $_ } grep {$_} @query; - # The distribution we do not want in our search + my @ROGUE_DISTRIBUTIONS = qw(kurila perl_debug perl-5.005_02+apache1.3.3+modperl pod2texi perlbench spodcxx); + + # Create String Search + my $criteria = '('.join ' AND ', @query.' )' ; + my $exclude = 'AND NOT ('.join ' OR ', @ROGUE_DISTRIBUTIONS.' )' ; # The ElasticSearch query in Perl + my %payload = ( - track_scores => 1, - query => { - filtered => { - query => { - bool => { should => $should } - - # ToDo see #1488 comment:7 itcharlie++ - # custom_score => { - # query => { bool => { should => $should } }, - # script => "_score - doc['documentation'].stringValue.length()/100" - # }, - }, - filter => { - and => [ - { not => { - filter => { - or => [ - map { - { term => { 'file.distribution' => $_ } } - } @ROGUE_DISTRIBUTIONS - ] - } - } - }, - { exists => { field => 'documentation' } }, - { term => { 'file.indexed' => \1 } }, - { term => { 'file.status' => 'latest' } }, - { not => { filter => { term => { 'file.authorized' => \0 } } } } - ] - } + query => { + query_string => { + query => "$criteria AND (status:latest) AND (indexed:true) $exclude", + fields => ["distribution","documentation.camelcase", "documentation.analyzed","author" ], + analyze_wildcard => 'true', } }, sort => [ @@ -145,6 +113,7 @@ sub metacpan_autocomplete { documentation => { order => "asc" } } ], + track_scores => 1, fields => [qw(documentation release author distribution)], size => MAX_RESULTS, ); @@ -162,7 +131,7 @@ sub metacpan_autocomplete { $ua->env_proxy unless Padre::Constant::WIN32; my $response = $ua->post( - 'http://api.metacpan.org/v0/file/_search', + 'https://fastapi.metacpan.org/v1/file/_search', Content => $json_request, ); @@ -192,7 +161,7 @@ sub metacpan_pod { my $ua = LWP::UserAgent->new( agent => "Padre/$VERSION" ); $ua->timeout(10); $ua->env_proxy unless Padre::Constant::WIN32; - my $url = "http://api.metacpan.org/v0/pod/$module?content-type=text/x-pod"; + my $url = "https://fastapi.metacpan.org/v1/pod/$module?content-type=text/x-pod"; my $response = $ua->get($url); unless ( $response->is_success ) { TRACE( sprintf( "Got '%s for %s", $response->status_line, $url ) ) @@ -244,7 +213,7 @@ sub metacpan_recent { $ua->timeout(10); $ua->env_proxy unless Padre::Constant::WIN32; my $url = - "http://api.metacpan.org/v0/release/?sort=date:desc" + "https://fastapi.metacpan.org/v1/release/?sort=date:desc" . "&size=" . MAX_RESULTS . "&fields=name,distribution,abstract,download_url"; @@ -272,10 +241,10 @@ sub metacpan_recent { sub metacpan_favorite { my $self = shift; - my %payload = ( + my %payload = ( "query" => { "match_all" => {} }, - "facets" => { - "leaderboard" => { + "aggs" => { + "dist" => { "terms" => { "field" => "distribution", "size" => MAX_RESULTS, @@ -283,7 +252,6 @@ sub metacpan_favorite { }, }, size => 0, - ); # Convert ElasticSearch Perl query to a JSON request @@ -296,10 +264,11 @@ sub metacpan_favorite { $ua->timeout(10); $ua->env_proxy unless Padre::Constant::WIN32; my $response = $ua->post( - 'http://api.metacpan.org/v0/favorite/_search', + 'https://fastapi.metacpan.org/v1/favorite/_search', Content => $json_request, ); - + + unless ( $response->is_success ) { TRACE( sprintf( "Got '%s' from metacpan.org", $response->status_line ) ) if DEBUG; return []; @@ -307,7 +276,8 @@ sub metacpan_favorite { # Decode json response then cleverly map it for the average joe :) my $data = JSON::XS::decode_json( $response->decoded_content ); - my @results = map {$_} @{ $data->{facets}->{leaderboard}->{terms} || [] }; + my @results = map {{ term => $_->{key}, count => $_->{doc_count} }} + @{$data->{aggregations}->{dist}->{buckets} || [] }; return \@results; }