blib/lib/App/DumpChromeHistory.pm

filter: Statementstmt 27.6% 21 / 76 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 23.4% 27 / 115 CC 15 SCAR 51.5
CC
15
Cov
20%
dump_chrome_history
240.0
__ANON__
2.0
BEGIN
1.0
CRAP 171.8
↑ Summary
1 package App::DumpChromeHistory;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-04-19'; # DATE
5 1 our $DIST = 'App-DumpChromeHistory'; # DIST
6 1 our $VERSION = '0.005'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our %SPEC;
16  
17 $SPEC{dump_chrome_history} = {
18 1 v => 1.1,
19 summary => 'Dump Chrome history',
20 args => {
21 detail => {
22 schema => 'bool*',
23 cmdline_aliases => {l=>{}},
24 },
25 profiles => {
26 summary => 'Select profile(s) to dump',
27 schema => ['array*', of=>'chrome::profile_name*', 'x.perl.coerce_rules'=>['From_str::comma_sep']],
28 description => <<'_',
29  
30 You can choose to dump history for only some profiles. By default, if this
31 option is not specified, history from all profiles will be dumped.
32  
33 _
34 },
35 copy_size_limit => {
36 schema => 'posint*',
37 default => 100*1024*1024,
38 description => <<'_',
39  
40 Chrome often locks the History database for a long time. If the size of the
41 database is not too large (determine by checking against this limit), then the
42 script will copy the file to a temporary file and extract the data from the
43 copied database.
44  
45 _
46 },
47 },
48 };
49 0 ▶sub dump_chrome_history {
Subroutine dump_chrome_history: not called CC=15 SCAR=54.8
Pod documented
50 0 require Chrome::Util::Profile;
51 0 require DBI;
52 0 require List::Util;
53  
54 0 my %args = @_;
55  
56 0 ▶ my $app = $args{_app} // 'Google Chrome';
Condition0%0/2
$args{"_app"} // "Google Chrome"
l!l
00
MC/DC0%0/1
$args{"_app"} // "Google Chrome"
$args{"_app"}
Truth table0%0/2
A
A= $args{"_app"}
Aresult
11
00
57 0 my $chrome_dir = $args{_chrome_dir};
58  
59 # list all available firefox profiles
60 0 my $available_profiles;
61 {
62 0 my $res = Chrome::Util::Profile::list_chrome_profiles(
63 _chrome_dir => $chrome_dir,
64 detail => 1,
65 );
66 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
67 0 $available_profiles = $res->[2];
68 }
69  
70 0 my $num_profiles_success = 0;
71 0 ▶ my $profiles = $args{profiles} // [map {$_->{name}} @$available_profiles];
Condition0%0/2
$args{"profiles"} // [map({$_->{"name"};} @$available_profiles)]
l!l
00
MC/DC0%0/1
$args{"profiles"} // [map({$_->{"name"};} @$available_profiles)]
$args{"profiles"}
Truth table0%0/2
A
A= $args{"profiles"}
Aresult
11
00
72  
73 0 my @rows;
74 0 my $resmeta = {};
75  
76 PROFILE:
77 0 for my $profile (@$profiles) {
78 0 log_trace "Dumping history for profile %s ...", $profile;
79 0 ▶ my $profile_data = List::Util::first(sub { $_->{name} eq $profile }, @$available_profiles);
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 ▶ unless ($profile_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($profile_data) 0 0 0
81 0 log_error "Profile %s is unknown, skipped", $profile;
82 0 next PROFILE;
83 }
84  
85 0 my $profile_dir = $profile_data->{path};
86 0 ▶ unless (-d $profile_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $profile_dir) 0 0 0
87 0 log_error "Cannot find directory '%s' for profile %s, profile skipped", $profile_dir, $profile;
88 0 next PROFILE;
89 }
90  
91 0 local $CWD = $profile_dir;
92  
93 0 my $history_path = "History";
94 0 ▶ unless (-f $history_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $history_path) 0 0 0
95 0 log_error "Cannot find history database file '%s' for profile %s, profile skipped", $history_path, $profile;
96 0 next PROFILE;
97 }
98  
99 SELECT: {
100 0 eval {
101 0 my $dbh = DBI->connect("dbi:SQLite:dbname=$history_path", "", "", {RaiseError=>1});
102 0 $dbh->sqlite_busy_timeout(3*1000);
103 0 my $sth = $dbh->prepare("SELECT url,last_visit_time,visit_count FROM urls ORDER BY last_visit_time");
104 0 $sth->execute;
105 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
106 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 0 0 0
107 0 push @rows, $row;
108 } else {
109 0 push @rows, $row->{url};
110 }
111 }
112 };
113 0 my $err = $@;
114 0 ▶ if ($err && $err =~ /database is locked/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and $err =~ /database is locked/) 0 0 0
Condition0%0/3
$err and $err =~ /database is locked/
!ll&&!rl&&r
000
MC/DC0%0/2
$err and $err =~ /database is locked/
$err $err =~ /database is locked/
Truth table0%0/3
A and B
A= $errB= $err =~ /database is locked/
ABresult
0X0
100
111
115 0 ▶ if ((-s $history_path) <= $args{copy_size_limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $history_path <= $args{'copy_size_limit'}) { } 0 0 0
116 0 log_debug "Database is locked ($err), will try to copy and query the copy instead ...";
117 0 require File::Copy;
118 0 require File::Temp;
119 0 my ($temp_fh, $temp_path) = File::Temp::tempfile();
120 0 ▶ File::Copy::copy($history_path, $temp_path) or die $err;
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($history_path, $temp_path) or die $err 0 0 0
121 0 $history_path = $temp_path;
122 0 redo SELECT;
123 } else {
124 0 log_debug "Database is locked ($err) but is too big, will wait instead";
125 }
126 }
127 } # SELECT
128 } # for profile
129  
130 $resmeta->{'table.fields'} = [qw/url title last_visit_time visit_count/]
131 0 ▶ if $args{detail};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"detail"} 0 0 0
132 0 [200, "OK", \@rows, $resmeta];
133 }
134  
135 1 1;
136 # ABSTRACT: Dump Chrome history
137  
138 __END__
↑ Summary