-
Notifications
You must be signed in to change notification settings - Fork 2
/
logdb_add_ua_isbot.pl
executable file
·61 lines (49 loc) · 1.31 KB
/
logdb_add_ua_isbot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env perl
# -*- coding: utf-8 -*-
use strict;
use warnings;
use HTTP::BrowserDetect;
use DBIx::TransactionManager;
use DBIx::Sunny;
{
my $dbname = shift;
# I don't want to use WAL mode here for SQLite.
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname"
, undef, undef
, {RootClass => 'DBIx::Sunny'
, PrintError => 0
, RaiseError => 1
, AutoCommit => 1});
if (not has_column($dbh, qw(ua is_bot))) {
$dbh->do(q(alter table ua add column is_bot integer not null default 0));
}
my $tm = DBIx::TransactionManager->new($dbh);
{
my $txn = $tm->txn_scope;
my $set_is_bot = do {
my $sth = $dbh->prepare("update ua set is_bot = ? where ua_id = ?");
sub { $sth->execute(@_) };
};
foreach my $row (lexpand($dbh->select_all(q(select * from ua)))) {
my $is_bot = do {
if ($row->{ua} =~ /Google Favicon$/ or $row->{ua} !~ m{\w+/}) {
1;
} else {
my $detector = HTTP::BrowserDetect->new($row->{ua});
$detector->robot;
}
};
$set_is_bot->($is_bot ? 1 : 0, $row->{ua_id});
}
$txn->commit;
}
}
sub lexpand {
(defined $_[0] && ref $_[0]) ? @{$_[0]} : ()
}
sub has_column {
my ($dbh, $table, $column) = @_;
my @info = $dbh->column_info(undef, undef, $table, $column)->fetchrow_array
or return;
wantarray ? @info : 1;
}