Last active
December 16, 2015 16:59
-
-
Save 2shortplanks/5467215 to your computer and use it in GitHub Desktop.
modulename to distribtion name with MetaCPAN (using Elastic Search)
This file contains 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
#!/usr/bin/env perl | |
use 5.016; | |
use warnings; | |
use LWP::UserAgent; | |
use HTTP::Request::Common; | |
use JSON::PP qw(encode_json decode_json); | |
sub mod2dist { | |
my $ua = LWP::UserAgent->new; | |
$ua->agent("Mod2Dist/0.01"); | |
$ua->timeout(10); | |
$ua->env_proxy; | |
my $response = $ua->request( | |
POST "http://api.metacpan.org/v0/module/_search", Content => encode_json({ | |
'fields' => ['distribution'], | |
'query' => { | |
bool => { | |
must => [ | |
{ | |
match => { "module.name" => shift } | |
}, | |
{ | |
match => { "status" => "latest" } | |
} | |
] | |
} | |
} | |
}), | |
); | |
my $data = decode_json($response->content); | |
return $data->{hits}{hits}[0]{fields}{distribution}; | |
} | |
say mod2dist(shift); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment