Permalink
Browse files

Added resume view count

  • Loading branch information...
1 parent 097e819 commit db7864f5330f71ccb7eb59a3c17866cbfa09d1eb @vti committed Aug 26, 2011
Showing with 238 additions and 169 deletions.
  1. +5 −0 config.yml
  2. +37 −169 lib/Perlresume.pm
  3. +175 −0 lib/Perlresume/MetaCPAN.pm
  4. +5 −0 public/css/style.css
  5. +14 −0 schema/00schema.sql
  6. +2 −0 views/resume.caml
View
@@ -5,3 +5,8 @@ layout: "main"
charset: "UTF-8"
template: "caml"
+
+plugins:
+ Database:
+ driver: 'SQLite'
+ database: 'perlresume.db'
View
@@ -1,214 +1,82 @@
package Perlresume;
use Dancer ':syntax';
-use MetaCPAN::API;
-use Try::Tiny;
-use Time::Piece;
-use LWP::UserAgent;
-use JSON ();
+use Dancer::Plugin::Database;
+use Perlresume::MetaCPAN;
our $VERSION = '0.1';
-my $METACPAN = 'http://api.metacpan.org/v0';
+my $mcpan = Perlresume::MetaCPAN->new;
get '/' => sub {
if (my $author = params->{author}) {
return redirect '/' . $author;
}
- my $authors;
+ my $authors = load_last_searches();
template 'index' => {authors => $authors};
};
get '/:author' => sub {
my $id = uc params->{author};
- my $author = fetch_author($id);
+ my $cpan_profile = $mcpan->fetch_author($id);
- if (!$author) {
+ if (!$cpan_profile) {
status 'not_found';
return template 'not_found';
}
- $author->{dist_count} = fetch_dist_count($id);
-
- $author->{email} = $author->{email}->[0] if defined $author->{email};
- $author->{website} = $author->{website}->[0] if defined $author->{website};
-
- $author->{contacts} = [];
- foreach my $profile (@{$author->{profile}}) {
- push @{$author->{contacts}},
- { $profile->{name} => 1,
- id => $profile->{id}
- };
- }
-
- $author->{profiles} = 1 if @{$author->{contacts}};
-
- $author->{first_release_year} = fetch_first_release_year($id);
- $author->{favorited_dist_count} = fetch_favorited_dist_count($id);
-
- $author->{dists_users_count} = fetch_dists_users_count($id);
+ my $author = find_or_create($cpan_profile);
+ $author->{updated} = time;
+ $author->{views}++;
+ my $views = $author->{views};
+ update_author($author);
template 'resume' => {
- %$author,
- title => $author->{asciiname} ? $author->{asciiname} : $author->{name}
+ title => $cpan_profile->{asciiname}
+ ? $cpan_profile->{asciiname}
+ : $cpan_profile->{name},
+ %$cpan_profile,
+ %$author
};
};
true;
-sub fetch_author {
- my ($id) = @_;
-
- my $mcpan = MetaCPAN::API->new;
-
- return try { $mcpan->author($id) };
-}
-
-sub fetch_dist_count {
- my ($id) = @_;
-
- my $mcpan = MetaCPAN::API->new;
-
- my $result = $mcpan->fetch(
- 'release/_search',
- q => "author:$id AND status:latest",
- size => 0
- );
-
- return $result->{hits}->{total};
-}
-
-sub fetch_favorited_dist_count {
- my ($id) = @_;
-
- my $mcpan = MetaCPAN::API->new;
-
- my $result = $mcpan->fetch(
- 'favorite/_search',
- q => "author:$id",
- size => 0
- );
-
- return $result->{hits}->{total};
-}
-
-sub fetch_first_release_year {
- my ($id) = @_;
-
- my $mcpan = MetaCPAN::API->new;
-
- my $result = $mcpan->fetch(
- 'release/_search',
- q => "author:$id",
- sort => 'date',
- size => 1
- );
-
- my $date = $result->{hits}->{hits}->[0]->{_source}->{date};
- return 0 unless defined $date;
-
- $date = Time::Piece->strptime($date, '%Y-%m-%dT%H:%M:%S');
-
- return $date->year;
-}
-
-sub fetch_dists_users_count {
- my ($id) = @_;
-
- my $ua = LWP::UserAgent->new;
-
- my $response = $ua->post(
- "$METACPAN/release/_search",
- Content => JSON::encode_json(
- { query => {
- filtered => {
- query => {"match_all" => {}},
- filter => {
- and => [
- {term => {'release.status' => 'latest'}},
- {term => {'release.authorized' => \1}},
- {term => {"release.author" => $id}}
- ]
- }
- }
- },
- fields => ['distribution'],
- size => 999,
- from => 0,
- sort => [{date => 'desc'}],
- }
- )
- );
- die $response->status_line unless $response->is_success;
-
- my $res = JSON::decode_json($response->decoded_content);
+sub find_or_create {
+ my ($author) = @_;
- my @modules;
- foreach my $module (@{$res->{hits}{hits}}) {
- my $name = $module->{fields}{distribution};
- $name =~ s/-/::/g;
- push @modules, $name;
+ if (my $author =
+ database->quick_select('resume', {pauseid => $author->{pauseid}}))
+ {
+ return $author;
}
- return 0 unless @modules;
-
- $response = $ua->post(
- "$METACPAN/release/_search",
- Content => JSON::encode_json {
- query => {
- filtered => {
- query => {"match_all" => {}},
- filter => {
- and => [
- {term => {'release.status' => 'latest'}},
- {term => {'release.authorized' => \1}},
- { terms =>
- {"release.dependency.module" => \@modules}
- }
- ]
- }
- }
- },
- size => 0,
- #,from => 0
- }
- );
-
- die $response->status_line unless $response->is_success;
+ my $name = $author->{asciiname} ? $author->{asciiname} : $author->{name};
- $res = JSON::decode_json($response->decoded_content);
+ database->quick_insert('resume',
+ {pauseid => $author->{pauseid}, name => $name, updated => time});
- return $res->{hits}->{total};
+ return {pauseid => $author->{pauseid}, views => 0};
}
-sub save_last_search {
+sub update_author {
my ($author) = @_;
- my $name = $author->{asciiname} ? $author->{asciiname} : $author->{name};
-
- open my $fh, '>>', path('searches');
- print $fh "$author->{pauseid}:$name", "\n" or die $!;
- close $fh;
+ database->quick_update('resume', {pauseid => $author->{pauseid}},
+ $author);
}
sub load_last_searches {
- my $authors = [];
- if (-e path('searches')) {
- open my $fh, '<', path('searches') or die $!;
- while (defined(my $line = <$fh>)) {
- chomp $line;
- my ($id, $name) = split ':' => $line;
- push @$authors, {id => $id, name => $name};
- }
- close $fh;
-
- $authors = [reverse @$authors];
- if (@$authors > 10) {
- $authors = [@$authors[0 .. 10]];
- }
- }
+ my $sth = database->prepare(
+ 'SELECT pauseid, name FROM resume ORDER BY updated DESC LIMIT 10',
+ );
+ $sth->execute;
+
+ my $authors =
+ [map { {pauseid => $_->[0], name => $_->[1]} }
+ @{$sth->fetchall_arrayref}];
return $authors;
}
Oops, something went wrong.

0 comments on commit db7864f

Please sign in to comment.