Permalink
Browse files

cpan ratings type / indexer / endpoint

  • Loading branch information...
1 parent f1ea88b commit ff7109ed69cac2bf8b0bb314e5c548221d30cbe9 @monken monken committed Jun 8, 2011
@@ -1,61 +0,0 @@
-#!/usr/bin/perl
-
-=head2 SYNOPSIS
-
-Loads module ratings into module table. Requires the following file
-in the /perl directory:
-
-http://cpanratings.perl.org/csv/all_ratings.csv
-
-=cut
-
-use Data::Dump qw( dump );
-use Find::Lib '../lib';
-use MetaCPAN;
-use feature 'say';
-use Parse::CSV;
-use Path::Class::File;
-use WWW::Mechanize::Cached;
-
-my $es = MetaCPAN->new->es;
-my $filename = '/tmp/all_ratings.csv';
-my $file = Path::Class::File->new( $filename );
-my $mech = WWW::Mechanize::Cached->new;
-
-$mech->get( 'http://cpanratings.perl.org/csv/all_ratings.csv' );
-my $fh = $file->openw();
-print $fh $mech->content;
-
-my $parser = Parse::CSV->new(
- file => $filename,
- fields => 'auto',
-);
-
-my @to_insert = ();
-
-while ( my $rating = $parser->fetch ) {
-
- my $dist_name = $rating->{distribution};
-
- my $data = {
- dist => $rating->{distribution},
- rating => $rating->{rating},
- review_count => $rating->{review_count},
- };
-
- my %es_insert = (
- index => {
- index => 'cpan',
- type => 'cpanratings',
- id => $rating->{distribution},
- data => $data
- }
- );
-
- push @to_insert, \%es_insert;
-
-}
-
-my $result = $es->bulk( \@to_insert );
-
-unlink $filename;
@@ -0,0 +1,32 @@
+package MetaCPAN::Document::Rating;
+use Moose;
+use ElasticSearchX::Model::Document;
+use ElasticSearchX::Model::Document::Types qw(:all);
+use MooseX::Types::Structured qw(Dict Tuple Optional);
+use MooseX::Types::Moose qw(Int Num Bool Str ArrayRef HashRef Undef);
+
+has user => ( required => 1, is => 'ro', isa => Str );
+has details =>
+ ( required => 0, is => 'ro', isa => Dict [ documentation => Str ] );
+has rating =>
+ ( required => 1, is => 'ro', isa => Num, builder => '_build_rating' );
+has distribution => ( required => 1, is => 'ro', isa => Str );
+has release => ( required => 1, is => 'ro', isa => Str );
+has author => ( required => 1, is => 'ro', isa => Str );
+has date =>
+ ( required => 1, isa => 'DateTime', default => sub { DateTime->now } );
+has helpful => (
+ required => 1,
+ isa => ArrayRef [ Dict [ user => Str, value => Bool ] ],
+ default => sub { [] } );
+
+sub _build_rating {
+ my $self = shift;
+ die "Provide details to calculate a rating";
+ my %details = %{ $self->details };
+ my $rating = 0;
+ $rating += $_ for ( values %details );
+ return $rating / scalar keys %details;
+}
+
+__PACKAGE__->meta->make_immutable;
@@ -0,0 +1,29 @@
+package MetaCPAN::Plack::Rating;
+use base 'MetaCPAN::Plack::Base';
+use strict;
+use warnings;
+
+sub type { 'rating' }
+
+sub handle {
+ my ( $self, $req ) = @_;
+ $self->get_source($req);
+}
+
+1;
+
+__END__
+
+=head1 METHODS
+
+=head2 type
+
+Returns C<rating>.
+
+=head2 handle
+
+Calls L<MetaCPAN::Plack::Base/get_source>.
+
+=head1 SEE ALSO
+
+L<MetaCPAN::Plack::Base>
@@ -0,0 +1,61 @@
+package MetaCPAN::Script::Ratings;
+
+use Moose;
+with 'MooseX::Getopt';
+use Log::Contextual qw( :log :dlog );
+with 'MetaCPAN::Role::Common';
+use File::Spec::Functions qw(catfile);
+use File::Temp qw(tempdir);
+use JSON ();
+use Parse::CSV ();
+use LWP::UserAgent ();
+
+has ratings =>
+ ( is => 'ro', default => 'http://cpanratings.perl.org/csv/all_ratings.csv' );
+
+sub run {
+ my $self = shift;
+ $self->index_ratings;
+ $self->index->refresh;
+}
+
+sub index_ratings {
+ my $self = shift;
+ my $ua = LWP::UserAgent->new;
+ log_info { "Downloading " . $self->ratings };
+ my $target = catfile( tempdir( CLEANUP => 1 ), 'ratings.csv' );
+ $ua->mirror( $self->ratings, $target );
+
+ my $parser = Parse::CSV->new(
+ file => $target,
+ fields => 'auto', );
+
+ my $type = $self->index->type('rating');
+ while ( my $rating = $parser->fetch ) {
+ next unless ( $rating->{review_count} );
+ my $data = {
+ distribution => $rating->{distribution},
+ release => 'PLACEHOLDER',
+ author => 'PLACEHOLDER',
+ rating => $rating->{rating},
+ user => 'CPANRatings' };
+ for ( my $i = 0 ; $i < $rating->{review_count} ; $i++ ) {
+ $type->put( Dlog_trace { $_ } $data );
+ }
+ }
+ log_info { "done" };
+}
+
+1;
+
+=pod
+
+=head1 SYNOPSIS
+
+ $ bin/metacpan mirrors
+
+=head1 SOURCE
+
+L<http://www.cpan.org/indices/mirrors.json>
+
+=cut
@@ -22,7 +22,7 @@ sub build_app {
cpan => $self->cpan,
remote => $self->remote,
index => $index );
- for ( qw(Author File Mirror Module
+ for ( qw(Author File Mirror Module Rating
Pod Release Source Login User) )
{
my $class = "MetaCPAN::Plack::" . $_;

0 comments on commit ff7109e

Please sign in to comment.