line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Grok::Common; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
29
|
$App::Grok::Common::AUTHORITY = 'cpan:HINRIK'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
{ |
6
|
|
|
|
|
|
|
$App::Grok::Common::VERSION = '0.26'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
10
|
1
|
|
|
1
|
|
6
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
11
|
1
|
|
|
1
|
|
1942
|
use File::HomeDir qw<my_data>; |
|
1
|
|
|
|
|
8884
|
|
|
1
|
|
|
|
|
382
|
|
12
|
1
|
|
|
1
|
|
11
|
use File::Spec::Functions qw<catdir>; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
63
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
5
|
use base qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1197
|
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw(download data_dir); |
16
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub data_dir { |
19
|
1
|
|
|
1
|
1
|
6
|
my $data_dir = catdir(my_data(), '.grok'); |
20
|
1
|
50
|
|
|
|
68
|
if (!-d $data_dir) { |
21
|
0
|
0
|
|
|
|
0
|
mkdir $data_dir or die "Can't create $data_dir: $!\n"; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
16
|
my $res_dir = catdir($data_dir, 'resources'); |
25
|
1
|
50
|
|
|
|
15
|
if (!-d $res_dir) { |
26
|
0
|
0
|
|
|
|
0
|
mkdir $res_dir or die "Can't create $res_dir: $!\n"; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
7
|
return $data_dir; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub download { |
33
|
0
|
|
|
0
|
1
|
|
my ($title, $url) = @_; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
require LWP::UserAgent; |
36
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
37
|
0
|
|
|
|
|
|
$ua->timeout(10); |
38
|
0
|
|
|
|
|
|
$ua->env_proxy; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
eval 'require Term::ProgressBar'; |
41
|
0
|
0
|
|
|
|
|
if ($@) { |
42
|
0
|
|
|
|
|
|
print $title, "\n"; |
43
|
0
|
|
|
|
|
|
my $response = $ua->get($url); |
44
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
45
|
0
|
|
|
|
|
|
return $response->decoded_content; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
0
|
|
|
|
|
|
die 'Download failed: '.$response->status_line."\n"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my $bar = Term::ProgressBar->new({ |
53
|
|
|
|
|
|
|
name => $title, |
54
|
|
|
|
|
|
|
count => 1024, |
55
|
|
|
|
|
|
|
ETA => 'linear', |
56
|
|
|
|
|
|
|
}); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $content; |
59
|
0
|
|
|
|
|
|
my $output = 0; |
60
|
0
|
|
|
|
|
|
my $target_is_set = 0; |
61
|
0
|
|
|
|
|
|
my $next_so_far = 0; |
62
|
|
|
|
|
|
|
$ua->get( |
63
|
|
|
|
|
|
|
$url, |
64
|
|
|
|
|
|
|
":content_cb" => sub { |
65
|
0
|
|
|
0
|
|
|
my ($chunk, $response, $proto) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
if (!$target_is_set) { |
68
|
0
|
0
|
|
|
|
|
if (my $cl = $response->content_length) { |
69
|
0
|
|
|
|
|
|
$bar->target($cl); |
70
|
0
|
|
|
|
|
|
$target_is_set = 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
0
|
|
|
|
|
|
$bar->target($output + 2 * length $chunk); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$output += length $chunk; |
78
|
0
|
|
|
|
|
|
$content .= $chunk; |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if ($output >= $next_so_far) { |
81
|
0
|
|
|
|
|
|
$next_so_far = $bar->update($output); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#$bar->target($output); |
85
|
|
|
|
|
|
|
#$bar->update($output); |
86
|
|
|
|
|
|
|
}, |
87
|
0
|
|
|
|
|
|
); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $content; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=encoding utf8 |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 NAME |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
App::Grok::Common - Common functions used in grok |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use strict; |
103
|
|
|
|
|
|
|
use warnings; |
104
|
|
|
|
|
|
|
use App::Grok::Common qw<:ALL>; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# download a file, with a progress bar |
107
|
|
|
|
|
|
|
my $url = 'http://foo.bar/baz'; |
108
|
|
|
|
|
|
|
my $content = download('My file', $url); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
This module provides common utility functions used in App::Grok. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 FUNCTIONS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 C<download> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Downloads a file from the web and returns the contents. Prints a progress bar |
119
|
|
|
|
|
|
|
(if L<Term::ProgressBar|Term::ProgressBar> is installed) as while doing so. |
120
|
|
|
|
|
|
|
It takes two arguments, a title string and the url. Returns the downloaded |
121
|
|
|
|
|
|
|
content. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 C<data_dir> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Creates (if necessary) and then returns the name of the directory where grok |
126
|
|
|
|
|
|
|
stores its data (e.g. F<~/.grok>). |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |