#!/usr/bin/env perl use strict; use warnings; use Mojo::UserAgent; use Mojo::DOM; use Data::Dumper; use Date::Calc ( 'Date_to_Days', 'Today' ); my $agent = Mojo::UserAgent->new; $agent->transactor->name( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:48.0) Gecko/20100101 Firefox/48.0'); my $urls = [ "https://xhamster.com/users/desirette/photos", ]; my $stamp = sprintf "%d_%02d_%02d", Today; open my $output, "> ./pix_stats_$stamp.csv" or die $!; my $galleries = fetch_galleries_urls( $agent, $urls ); my $photos = process_galleries( $agent, $galleries ); process_photos( $agent, $photos, $output ); sub fetch_galleries_urls { my ( $agent, $urls ) = @_; my $galleries = []; foreach my $url ( @$urls ) { my $tx = $agent->get( $url ); if ( $tx->success ) { push @$galleries, @{ $tx->res->dom ->find( 'div.gallery a:first-child' ) ->map( attr => 'href' ) }; } else { print STDERR join( ': ', $tx->error ), $/; } } return $galleries; } sub process_galleries { my ( $agent, $galleries, $fh ) = @_; my @photos; foreach my $url ( @$galleries ) { my $tx = $agent->get( $url ); my $counter = 1; ( my $gallery = ( split ///, $url )[-1] ) =~ s/.html$//; $gallery =~ tr/_/ /; if ( $tx->success ) { push @photos, map { { gallery => $gallery, position => $counter++, url => $_ } } @{ $tx->res->dom ->find( 'div.gallery.iItem a:first-child' ) ->map( attr => 'href' ) }; } } return @photos; } sub process_photos { my ( $agent, $photos, $fh ) = @_; my @unsorted; foreach my $photo ( @$photos ) { my $tx = $agent->get( $photo->{url} ); if ( $tx->success ) { my ( $upload, undef ) = split / /, $tx->res->dom ->find( 'td#galleryUser div.item span.hint' ) ->map( attr => 'hint' )->[0]; ( my $views = ( split / /, $tx->res->dom ->find( 'td#galleryUser div.item' ) ->last->content )[-1] ) =~ s/,//g; my $days = Date_to_Days( Today ) - Date_to_Days( split /-/, $upload ); $photo->{url} =~ s/^(.*)#content$/$1/; push @unsorted, { gallery => $photo->{gallery}, position => $photo->{position}, url => $photo->{url}, upload => $upload, views => $views, days => $days, average => sprintf "%.2f", $views / $days }; } else { print STDERR join( ': ', $tx->error ), $/; return; } } printf $fh "URL;Gallery;Position;Days;Views;Average;Uploadedn"; printf $fh "%s;%s;%d;%d;%d;%.2f;%sn", @{ $_ }{ qw/ url gallery position days views average upload / } foreach sort { $b->{average} <=> $a->{average} } @unsorted; }