Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Last active August 29, 2015 14:03
Show Gist options
  • Save bessarabov/25b268841ef83913766c to your computer and use it in GitHub Desktop.
Save bessarabov/25b268841ef83913766c to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);
use Data::Printer;
use ElasticSearch;
use LWP::Simple;
use JSON;
sub get_es {
my $es = ElasticSearch->new(
no_refresh => 1,
servers => 'api.metacpan.org',
);
return $es;
}
sub get_user_id_from_pause_id {
my ($pause_id) = @_;
my $author = get_es()->search(
index => 'v0',
type => 'author',
query => {
filtered => {
query => { match_all => {} },
filter => {
term => {
pauseid => $pause_id,
}
},
},
},
);
my $user_id = $author->{hits}->{hits}->[0]->{_source}->{user};
return $user_id;
}
sub get_distributions_liked_by_user {
my ($user_id) = @_;
my $favorite = get_es()->search(
index => 'v0',
type => 'favorite',
fields => [qw(
distribution
date
)],
query => {
filtered => {
query => { match_all => {} },
filter => {
term => {
user => $user_id,
}
},
},
},
size => 100,
);
my @d;
foreach my $el (@{$favorite->{hits}->{hits}}) {
push @d, $el->{fields}->{distribution};
}
return @d;
}
sub get_distribution_repository {
my ($d) = @_;
my $json = get("http://api.metacpan.org/v0/release/$d");
my $data = from_json($json);
my $repository_url = $data->{resources}->{repository}->{url};
return $repository_url;
}
sub main {
my $pause_id = $ARGV[0];
if (not defined $pause_id) {
say "You should run it as `$0 PAUSEID`";
exit 1;
}
my $user_id = get_user_id_from_pause_id($pause_id);
my @distributions = get_distributions_liked_by_user($user_id);
say 'namespace,name,source';
foreach my $d (@distributions) {
my $url = get_distribution_repository($d);
my $module = $d;
$module =~ s/-/::/g;
if (defined $url) {
say 'perl,'
. $module . ','
. $url
;
} else {
warn "WARNING: no info about repostitory url for $d\n";
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment