| 1 | package WWW::RobotRules::DBIC; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use base qw(WWW::RobotRules); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use WWW::RobotRules::DBIC::Schema; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use DateTime; | ||
| 7 | use DateTime::Format::Strptime; | ||
| 8 | |||
| 9 | our $VERSION = '0.01'; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my($class, @connect_info) = @_; | ||
| 13 | my $self = bless { | ||
| 14 | schema => WWW::RobotRules::DBIC::Schema->connect(@connect_info), | ||
| 15 | }, $class; | ||
| 16 | $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub agent { | ||
| 20 | my($self, $agent) = @_; | ||
| 21 | my $old = $self->{agent}; | ||
| 22 | if (defined $agent && (!$old || $agent ne $old)) { | ||
| 23 | $agent =~ s|/.*$||; # remove version number. | ||
| 24 | my $new = $self->{schema}->resultset('UserAgent')->find_or_create({ | ||
| 25 | name => $agent, | ||
| 26 | }); | ||
| 27 | $self->{agent} = $agent; | ||
| 28 | $self->{agent_id} = $new->id; | ||
| 29 | $self->{_netloc} = undef; | ||
| 30 | } | ||
| 31 | $old; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub visit { | ||
| 35 | my($self, $netloc, $time) = @_; | ||
| 36 | return unless $netloc; | ||
| 37 | $time ||= time; | ||
| 38 | my $datetime = epoch2datetime($time); | ||
| 39 | my $old = $self->_find_netloc($netloc); | ||
| 40 | if ($old) { | ||
| 41 | my $count = $old->count + 1; | ||
| 42 | $old->count($count); | ||
| 43 | $old->visited_on($datetime); | ||
| 44 | $old->update; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | $self->{schema}->resultset('Netloc')->create({ | ||
| 48 | user_agent_id => $self->{agent_id}, | ||
| 49 | netloc => $netloc, | ||
| 50 | count => 1, | ||
| 51 | visited_on => $datetime, | ||
| 52 | }); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub no_visits { | ||
| 57 | my($self, $netloc) = @_; | ||
| 58 | $netloc = $self->_find_netloc($netloc); | ||
| 59 | return 0 unless $netloc; | ||
| 60 | return $netloc->count; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub last_visit { | ||
| 64 | my($self, $netloc) = @_; | ||
| 65 | $netloc = $self->_find_netloc($netloc); | ||
| 66 | return unless $netloc && $netloc->visited_on; | ||
| 67 | return datetime2epoch($netloc->visited_on); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub fresh_until { | ||
| 71 | my ($self, $netloc, $fresh) = @_; | ||
| 72 | $netloc = $self->_find_netloc($netloc, 1); | ||
| 73 | my $old = $netloc->fresh_until; | ||
| 74 | if (defined $fresh) { | ||
| 75 | my $datetime = epoch2datetime($fresh); | ||
| 76 | $netloc->fresh_until($datetime); | ||
| 77 | $netloc->update; | ||
| 78 | } | ||
| 79 | return datetime2epoch($old) if $old; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub push_rules { | ||
| 83 | my($self, $netloc, @rules) = @_; | ||
| 84 | $netloc = $self->_find_netloc($netloc, 1); | ||
| 85 | for my $rule(@rules) { | ||
| 86 | $self->{schema}->resultset('Rule')->create({ | ||
| 87 | rule => $rule, | ||
| 88 | netloc_id => $netloc->id, | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub clear_rules { | ||
| 94 | my($self, $netloc) = @_; | ||
| 95 | $netloc = $self->_find_netloc($netloc); | ||
| 96 | if ($netloc) { | ||
| 97 | $self->{schema}->resultset('Rule')->search({netloc_id => $netloc->id})->delete; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub rules { | ||
| 102 | my($self, $netloc) = @_; | ||
| 103 | my @rules = $self->{schema}->resultset('Rule')->search({ | ||
| 104 | 'netloc.netloc' => $netloc, | ||
| 105 | 'netloc.user_agent_id' => $self->{agent_id}, | ||
| 106 | }, { | ||
| 107 | join => [qw(netloc)], | ||
| 108 | }); | ||
| 109 | return map { $_->rule } @rules; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub dump {} | ||
| 113 | |||
| 114 | sub _find_netloc { | ||
| 115 | my($self, $netloc, $create) = @_; | ||
| 116 | my $old = $self->{_netloc}; | ||
| 117 | if ($old && $old->netloc eq $netloc) { | ||
| 118 | return $old; | ||
| 119 | } | ||
| 120 | my $obj = $self->{schema}->resultset('Netloc')->find({ | ||
| 121 | netloc => $netloc, | ||
| 122 | user_agent_id => $self->{agent_id}, | ||
| 123 | }); | ||
| 124 | if (!$obj && $create) { | ||
| 125 | $obj = $self->{schema}->resultset('Netloc')->create({ | ||
| 126 | netloc => $netloc, | ||
| 127 | user_agent_id => $self->{agent_id}, | ||
| 128 | count => '0', | ||
| 129 | }); | ||
| 130 | } | ||
| 131 | $self->{_netloc} = $obj; | ||
| 132 | $obj; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub datetime2epoch { | ||
| 136 | my $str = shift; | ||
| 137 | return unless $str; | ||
| 138 | my $format = DateTime::Format::Strptime->new( | ||
| 139 | pattern => '%Y-%m-%d %H:%M:%S', | ||
| 140 | time_zone => 'local', | ||
| 141 | ); | ||
| 142 | my $dt = $format->parse_datetime($str); | ||
| 143 | return $dt->epoch if $dt; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub epoch2datetime { | ||
| 147 | my $epoch = shift; | ||
| 148 | return unless $epoch; | ||
| 149 | my $dt = DateTime->from_epoch(epoch => $epoch); | ||
| 150 | $dt->set_time_zone('local'); | ||
| 151 | return $dt->strftime('%Y-%m-%d %H:%M:%S'); | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __END__ | ||