|
| 1 | +package MetaCPAN::Script::River; |
| 2 | + |
| 3 | +use Moose; |
| 4 | +use namespace::autoclean; |
| 5 | + |
| 6 | +use JSON::MaybeXS qw( decode_json ); |
| 7 | +use Log::Contextual qw( :log :dlog ); |
| 8 | +use LWP::UserAgent; |
| 9 | +use MetaCPAN::Types qw( ArrayRef Str Uri); |
| 10 | + |
| 11 | +with 'MetaCPAN::Role::Script', 'MooseX::Getopt'; |
| 12 | + |
| 13 | +has river_url => ( |
| 14 | + is => 'ro', |
| 15 | + isa => Uri, |
| 16 | + coerce => 1, |
| 17 | + required => 1, |
| 18 | + default => 'https://neilb.org/FIXME', |
| 19 | +); |
| 20 | + |
| 21 | +has _ua => ( |
| 22 | + is => 'ro', |
| 23 | + isa => 'LWP::UserAgent', |
| 24 | + default => sub { LWP::UserAgent->new }, |
| 25 | +); |
| 26 | + |
| 27 | +sub run { |
| 28 | + my $self = shift; |
| 29 | + my $summaries = $self->retrieve_river_summaries; |
| 30 | + $self->index_river_summaries($summaries); |
| 31 | + |
| 32 | + return 1; |
| 33 | +} |
| 34 | + |
| 35 | +sub index_river_summaries { |
| 36 | + my ( $self, $summaries ) = @_; |
| 37 | + $self->index->refresh; |
| 38 | + my $dists = $self->index->type('distribution'); |
| 39 | + my $bulk = $self->index->bulk( size => 300 ); |
| 40 | + for my $summary (@$summaries) { |
| 41 | + my $dist = delete $summary->{dist}; |
| 42 | + my $doc = $dists->get($dist); |
| 43 | + $doc ||= $dists->new_document( { name => $dist } ); |
| 44 | + $doc->_set_river($summary); |
| 45 | + $bulk->put($doc); |
| 46 | + } |
| 47 | + $bulk->commit; |
| 48 | +} |
| 49 | + |
| 50 | +sub retrieve_river_summaries { |
| 51 | + my $self = shift; |
| 52 | + my $resp = $self->_ua->get( $self->river_url ); |
| 53 | + |
| 54 | + $self->handle_error( $resp->status_line ) unless $resp->is_success; |
| 55 | + |
| 56 | + return decode_json $resp->content; |
| 57 | +} |
| 58 | + |
| 59 | +__PACKAGE__->meta->make_immutable; |
| 60 | + |
| 61 | +1; |
| 62 | + |
| 63 | +=pod |
| 64 | +
|
| 65 | +=head1 SYNOPSIS |
| 66 | +
|
| 67 | + # bin/metacpan river |
| 68 | +
|
| 69 | +=head1 DESCRIPTION |
| 70 | +
|
| 71 | +Retrieves the CPAN river data from its source and |
| 72 | +updates our ES information. |
| 73 | +
|
| 74 | +This can then be accessed here: |
| 75 | +
|
| 76 | +http://api.metacpan.org/distribution/Moose |
| 77 | +http://api.metacpan.org/distribution/HTTP-BrowserDetect |
| 78 | +
|
| 79 | +=cut |
| 80 | + |
0 commit comments