| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Aozora2Epub::CachedGet; |
|
2
|
6
|
|
|
6
|
|
62
|
use strict; |
|
|
6
|
|
|
|
|
16
|
|
|
|
6
|
|
|
|
|
294
|
|
|
3
|
6
|
|
|
6
|
|
29
|
use warnings; |
|
|
6
|
|
|
|
|
10
|
|
|
|
6
|
|
|
|
|
285
|
|
|
4
|
6
|
|
|
6
|
|
30
|
use utf8; |
|
|
6
|
|
|
|
|
10
|
|
|
|
6
|
|
|
|
|
34
|
|
|
5
|
6
|
|
|
6
|
|
179
|
use Carp qw/croak/; |
|
|
6
|
|
|
|
|
12
|
|
|
|
6
|
|
|
|
|
399
|
|
|
6
|
6
|
|
|
6
|
|
4563
|
use HTTP::Tiny; |
|
|
6
|
|
|
|
|
350622
|
|
|
|
6
|
|
|
|
|
305
|
|
|
7
|
6
|
|
|
6
|
|
3281
|
use Cache::FileCache; |
|
|
6
|
|
|
|
|
234427
|
|
|
|
6
|
|
|
|
|
404
|
|
|
8
|
6
|
|
|
6
|
|
2679
|
use Encode qw/decode/; |
|
|
6
|
|
|
|
|
56881
|
|
|
|
6
|
|
|
|
|
762
|
|
|
9
|
6
|
|
|
6
|
|
6136
|
use Path::Tiny; |
|
|
6
|
|
|
|
|
85519
|
|
|
|
6
|
|
|
|
|
465
|
|
|
10
|
6
|
|
|
6
|
|
4065
|
use File::HomeDir; |
|
|
6
|
|
|
|
|
37480
|
|
|
|
6
|
|
|
|
|
816
|
|
|
11
|
6
|
|
|
6
|
|
44
|
use parent 'Exporter'; |
|
|
6
|
|
|
|
|
10
|
|
|
|
6
|
|
|
|
|
50
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT = qw(http_get); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $CACHE; |
|
18
|
|
|
|
|
|
|
init_cache(); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub init_cache { |
|
21
|
6
|
|
|
6
|
0
|
19
|
my $cache_dir = $ENV{AOZORA2EPUB_CACHE}; |
|
22
|
6
|
50
|
|
|
|
30
|
unless ($cache_dir) { |
|
23
|
6
|
|
|
|
|
28
|
my $home = File::HomeDir->my_home; |
|
24
|
6
|
50
|
|
|
|
266
|
$home or die "Can't determin home directory. Please set an environment variable AOZORA2EPUB_CACHE\n"; |
|
25
|
6
|
|
|
|
|
46
|
$cache_dir = path($home, '.aozora-epub'); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
454
|
$CACHE = Cache::FileCache->new({ |
|
29
|
|
|
|
|
|
|
namespace => 'aozora', |
|
30
|
|
|
|
|
|
|
default_expires_in => '30 days', |
|
31
|
|
|
|
|
|
|
cache_root => $cache_dir, |
|
32
|
|
|
|
|
|
|
directory_umask => 077, |
|
33
|
|
|
|
|
|
|
auto_purge_interval => '1 day', |
|
34
|
|
|
|
|
|
|
}); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub http_get { |
|
38
|
0
|
|
|
0
|
0
|
|
my $url = shift; |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
if ($url->isa('URI')) { |
|
41
|
0
|
|
|
|
|
|
$url = $url->as_string; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
0
|
|
|
|
|
|
my $content = $CACHE->get($url); |
|
44
|
0
|
0
|
|
|
|
|
return $content if $content; |
|
45
|
0
|
|
|
|
|
|
my $r = HTTP::Tiny->new->get($url); |
|
46
|
0
|
0
|
|
|
|
|
croak "$url: $r->{status} $r->{reason}" unless $r->{success}; |
|
47
|
0
|
|
|
|
|
|
$content = $r->{content}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $encoding = 'utf-8'; |
|
50
|
0
|
|
|
|
|
|
my $content_type = $r->{headers}{'content-type'}; |
|
51
|
0
|
0
|
|
|
|
|
unless ($content_type =~ m{text/}) { |
|
52
|
0
|
|
|
|
|
|
$CACHE->set($url, $content); |
|
53
|
0
|
|
|
|
|
|
return $content; # binary |
|
54
|
|
|
|
|
|
|
} |
|
55
|
0
|
0
|
|
|
|
|
if ($content_type =~ /charset=([^;]+)/) { |
|
|
|
0
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$encoding = $1; |
|
57
|
|
|
|
|
|
|
} elsif ($content =~ m{
|
|
58
|
0
|
|
|
|
|
|
$encoding = $1; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
0
|
|
|
|
|
|
$content = Encode::decode($encoding, $content); |
|
61
|
0
|
|
|
|
|
|
$CACHE->set($url, $content); |
|
62
|
0
|
|
|
|
|
|
return $content; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |