Skip to content

Instantly share code, notes, and snippets.

@2shortplanks
Last active December 16, 2015 16:59
Show Gist options
  • Save 2shortplanks/5467215 to your computer and use it in GitHub Desktop.
Save 2shortplanks/5467215 to your computer and use it in GitHub Desktop.
modulename to distribtion name with MetaCPAN (using Elastic Search)
#!/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