Last active
August 29, 2015 14:01
-
-
Save bessarabov/f62fbf4e0ccc1912738b to your computer and use it in GitHub Desktop.
metacpan likes
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 strict; | |
use warnings FATAL => 'all'; | |
use feature 'say'; | |
use utf8; | |
use open qw(:std :utf8); | |
use Data::Printer; | |
use ElasticSearch; | |
sub get_es { | |
my $es = ElasticSearch->new( | |
no_refresh => 1, | |
servers => 'api.metacpan.org', | |
); | |
return $es; | |
} | |
sub get_user_ids_who_favorite { | |
my ($distribution) = @_; | |
my $favorite = get_es()->search( | |
index => 'v0', | |
type => 'favorite', | |
fields => [qw( | |
user | |
)], | |
query => { | |
filtered => { | |
query => { match_all => {} }, | |
filter => { | |
term => { | |
distribution => $distribution, | |
} | |
}, | |
}, | |
}, | |
); | |
my @users = map { $_->{fields}->{user} } @{ $favorite->{hits}->{hits} }; | |
return @users; | |
} | |
sub get_author_from_id { | |
my ($user_id) = @_; | |
my $author = get_es()->search( | |
index => 'v0', | |
type => 'author', | |
fields => [qw( | |
asciiname | |
name | |
pauseid | |
)], | |
query => { | |
filtered => { | |
query => { match_all => {} }, | |
filter => { | |
term => { | |
user => $user_id, | |
} | |
}, | |
}, | |
}, | |
); | |
my $data = $author->{hits}->{hits}->[0]->{fields}; | |
return $data; | |
} | |
sub main { | |
my $distribution = 'Test-Whitespaces'; | |
print "# Who likes '$distribution'?\n"; | |
print "\n"; | |
my @user_ids = get_user_ids_who_favorite( $distribution ); | |
foreach my $user_id (@user_ids) { | |
my $author = get_author_from_id( $user_id ); | |
if (defined $author) { | |
print " * " . $author->{pauseid} . "\n"; | |
} else { | |
print " * ???\n"; | |
} | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment