|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use v5.36; |
| 4 | + |
| 5 | +use Getopt::Long; |
| 6 | +use MetaCPAN::Logger qw< :log :dlog >; |
| 7 | +use Cpanel::JSON::XS (); |
| 8 | + |
| 9 | +use MetaCPAN::ES; |
| 10 | +use MetaCPAN::Ingest qw< cpan_dir >; |
| 11 | + |
| 12 | +# args |
| 13 | +my ( $distribution, $files_only, $undo ); |
| 14 | +GetOptions( |
| 15 | + "distribution=s" => \$distribution, |
| 16 | + "files_only" => \$files_only, |
| 17 | + "undo" => \$undo, |
| 18 | +); |
| 19 | + |
| 20 | +# setup |
| 21 | +my $cpan = cpan_dir(); |
| 22 | +my $es = MetaCPAN::ES->new( type => "mirror" ); |
| 23 | + |
| 24 | + |
| 25 | +index_mirrors(); |
| 26 | + |
| 27 | +$es->index_refresh; |
| 28 | + |
| 29 | +# TODO: |
| 30 | +# cdn_purge_now( { keys => ['MIRRORS'], } ); |
| 31 | + |
| 32 | +log_info {"done"}; |
| 33 | + |
| 34 | +### |
| 35 | + |
| 36 | +sub index_mirrors () { |
| 37 | + log_info { 'Getting mirrors.json file from ' . $cpan }; |
| 38 | + |
| 39 | + my $json = $cpan->child( 'indices', 'mirrors.json' )->slurp; |
| 40 | + |
| 41 | + # Clear out everything in the index |
| 42 | + # so don't end up with old mirrors |
| 43 | + $es->clear; |
| 44 | + |
| 45 | + my $mirrors = Cpanel::JSON::XS::decode_json($json); |
| 46 | + foreach my $mirror (@$mirrors) { |
| 47 | + $mirror->{location} = { |
| 48 | + lon => delete $mirror->{longitude}, |
| 49 | + lat => delete $mirror->{latitude} |
| 50 | + }; |
| 51 | + |
| 52 | + #Dlog_trace {"Indexing $_"} $mirror; |
| 53 | + log_debug {sprintf("Indexing %s", $mirror->{name})}; |
| 54 | + |
| 55 | + my @doc = |
| 56 | + map { $_ => $mirror->{$_} } |
| 57 | + grep { defined $mirror->{$_} } |
| 58 | + keys %$mirror; |
| 59 | + |
| 60 | + $es->index( body => { @doc } ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +1; |
| 65 | + |
| 66 | +__END__ |
| 67 | +
|
| 68 | +=pod |
| 69 | +
|
| 70 | +=head1 SYNOPSIS |
| 71 | +
|
| 72 | + $ bin/mirrors.pl |
| 73 | +
|
| 74 | +=head1 SOURCE |
| 75 | +
|
| 76 | +L<http://www.cpan.org/indices/mirrors.json> |
| 77 | +
|
| 78 | +=cut |
0 commit comments