Facebook
From Funky Motmot, 6 Years ago, written in Perl.
This paste is a reply to Re: statystyki fotek from Cobalt Tamarin - view diff
Embed
Download Paste or View Raw
Hits: 406
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Mojo::UserAgent;
  7. use Mojo::DOM;
  8. use Data::Dumper;
  9. use Date::Calc ( 'Date_to_Days', 'Today' );
  10.  
  11. my $agent = Mojo::UserAgent->new;
  12.  
  13. $agent->transactor->name(
  14.     'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:48.0) Gecko/20100101 Firefox/48.0');
  15.  
  16. my $urls = [
  17.     "https://xhamster.com/users/desirette/photos",
  18. ];
  19.  
  20. my $stamp = sprintf "%d_%02d_%02d", Today;
  21.  
  22. open my $output, "> ./pix_stats_$stamp.csv" or die $!;
  23.  
  24. my $galleries = fetch_galleries_urls( $agent, $urls );
  25.  
  26. my $photos = process_galleries( $agent, $galleries );
  27.  
  28. process_photos( $agent, $photos, $output );
  29.  
  30. sub fetch_galleries_urls {
  31.     my ( $agent, $urls ) = @_;
  32.  
  33.     my $galleries = [];
  34.  
  35.     foreach my $url ( @$urls ) {
  36.         my $tx = $agent->get( $url );
  37.  
  38.         if ( $tx->success ) {
  39.             push @$galleries, @{
  40.                 $tx->res->dom
  41.                     ->find( 'div.gallery a:first-child' )
  42.                     ->map( attr => 'href' ) };
  43.         } else {
  44.             print STDERR join( ': ', $tx->error ), $/;
  45.         }
  46.     }
  47.  
  48.     return $galleries;
  49. }
  50.  
  51. sub process_galleries {
  52.     my ( $agent, $galleries, $fh ) = @_;
  53.  
  54.     my @photos;
  55.  
  56.     foreach my $url ( @$galleries ) {
  57.         my $tx = $agent->get( $url );
  58.  
  59.         my $counter = 1;
  60.         ( my $gallery = ( split ///, $url )[-1] ) =~ s/.html$//;
  61.         $gallery =~ tr/_/ /;
  62.  
  63.         if ( $tx->success ) {
  64.             push @photos, map { {
  65.                     gallery => $gallery,
  66.                     position => $counter++,
  67.                     url     => $_
  68.                 } } @{ $tx->res->dom
  69.                 ->find( 'div.gallery.iItem a:first-child' )
  70.                 ->map( attr => 'href' ) };
  71.         }
  72.     }
  73.  
  74.     return @photos;
  75. }
  76.  
  77. sub process_photos {
  78.     my ( $agent, $photos, $fh ) = @_;
  79.  
  80.     my @unsorted;
  81.  
  82.     foreach my $photo ( @$photos ) {
  83.         my $tx = $agent->get( $photo->{url} );
  84.  
  85.         if ( $tx->success ) {
  86.             my ( $upload, undef ) = split / /, $tx->res->dom
  87.                 ->find( 'td#galleryUser div.item span.hint' )
  88.                 ->map( attr => 'hint' )->[0];
  89.  
  90.             ( my $views = ( split / /, $tx->res->dom
  91.                 ->find( 'td#galleryUser div.item' )
  92.                 ->last->content )[-1] ) =~ s/,//g;
  93.  
  94.             my $days = Date_to_Days( Today )
  95.                 - Date_to_Days( split /-/, $upload );
  96.  
  97.             $photo->{url} =~ s/^(.*)#content$/$1/;
  98.  
  99.             push @unsorted, {
  100.                 gallery => $photo->{gallery},
  101.                 position => $photo->{position},
  102.                 url     => $photo->{url},
  103.                 upload  => $upload,
  104.                 views   => $views,
  105.                 days    => $days,
  106.                 average => sprintf "%.2f", $views / $days
  107.             };
  108.         } else {
  109.             print STDERR join( ': ', $tx->error ), $/;
  110.             return;
  111.         }
  112.     }
  113.  
  114.     printf $fh "URL;Gallery;Position;Days;Views;Average;Uploadedn";
  115.  
  116.     printf $fh "%s;%s;%d;%d;%d;%.2f;%sn",
  117.         @{ $_ }{ qw/ url gallery position days views average upload / }
  118.             foreach sort { $b->{average} <=> $a->{average} } @unsorted;
  119. }