| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Geo::Postcodes::JP::Update - update Japan Post Office postcode data |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Geo::Postcodes::JP::Update; |
|
10
|
|
|
|
|
|
|
require Exporter; |
|
11
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
12
|
|
|
|
|
|
|
@EXPORT_OK = qw/update_files/; |
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
45891
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
31
|
|
|
15
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
142
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.014'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#line 16 "Update.pm.tmpl" |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use LWP::UserAgent; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $ken_all_url = |
|
23
|
|
|
|
|
|
|
'http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip'; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $jigyosyo_url = |
|
26
|
|
|
|
|
|
|
'http://www.post.japanpost.jp/zipcode/dl/jigyosyo/zip/jigyosyo.zip'; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 update_files |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
update_files ('ken_all.zip', 'jigyosyo.zip'); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Get or update the two CSV files, KEN_ALL.CSV and JIGYOSYO.CSV from the |
|
34
|
|
|
|
|
|
|
Japan Post website. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The two arguments are the file name of the zipped KEN_ALL.CSV file and |
|
37
|
|
|
|
|
|
|
the zipped JIGYOSYO.CSV file. If these files exist, the routine tries |
|
38
|
|
|
|
|
|
|
to check whether the existing files are newer than the files on the |
|
39
|
|
|
|
|
|
|
post office website, and only downloads if the local files are older. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
People who are thinking of running this regularly might like to know |
|
42
|
|
|
|
|
|
|
that Japan Post usually updates the postcode files on the last day of |
|
43
|
|
|
|
|
|
|
the month. |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub update_files |
|
48
|
|
|
|
|
|
|
{ |
|
49
|
|
|
|
|
|
|
my ($ken_all_file, $jigyosyo_file) = @_; |
|
50
|
|
|
|
|
|
|
my $agent = LWP::UserAgent->new (); |
|
51
|
|
|
|
|
|
|
download ($agent, $ken_all_url, $ken_all_file); |
|
52
|
|
|
|
|
|
|
download ($agent, $jigyosyo_url, $jigyosyo_file); |
|
53
|
|
|
|
|
|
|
# Update the files. |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub download |
|
57
|
|
|
|
|
|
|
{ |
|
58
|
|
|
|
|
|
|
my ($agent, $url, $file) = @_; |
|
59
|
|
|
|
|
|
|
my $out = $url; |
|
60
|
|
|
|
|
|
|
$out =~ s!.*/!!; |
|
61
|
|
|
|
|
|
|
my $response; |
|
62
|
|
|
|
|
|
|
if (-f $file) { |
|
63
|
|
|
|
|
|
|
# There is a local file, so first compare the dates of the remote |
|
64
|
|
|
|
|
|
|
# file and the local file, and only download the remote file if it |
|
65
|
|
|
|
|
|
|
# is newer. |
|
66
|
|
|
|
|
|
|
print "Local file '$file' exists.\n"; |
|
67
|
|
|
|
|
|
|
my $local_date = mdate ($file); |
|
68
|
|
|
|
|
|
|
print "Local date: $local_date.\n"; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$response = $agent->head ($url); |
|
71
|
|
|
|
|
|
|
if (! $response->is_success) { |
|
72
|
|
|
|
|
|
|
warn "HEAD request for $url failed: " . $response->status; |
|
73
|
|
|
|
|
|
|
return; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
# Check for errors |
|
76
|
|
|
|
|
|
|
my $remote_date = $response->last_modified; |
|
77
|
|
|
|
|
|
|
print "Remote date: $remote_date.\n"; |
|
78
|
|
|
|
|
|
|
if ($local_date < $remote_date) { |
|
79
|
|
|
|
|
|
|
print "Remote file is newer, downloading to $out.\n"; |
|
80
|
|
|
|
|
|
|
$response = $agent->get ($url, ":content_file" => $file); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
else { |
|
83
|
|
|
|
|
|
|
print "Remote file is older, not downloading.\n"; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
else { |
|
87
|
|
|
|
|
|
|
# There is no local file, so just download it. |
|
88
|
|
|
|
|
|
|
print "Local file '$file' does not exist: putting in $out.\n"; |
|
89
|
|
|
|
|
|
|
$response = $agent->get ($url, ":content_file" => $file); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
if (! $response->is_success ()) { |
|
92
|
|
|
|
|
|
|
warn "Download failed: " . $response->status (); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Given a file name, return its modification date. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub mdate |
|
99
|
|
|
|
|
|
|
{ |
|
100
|
|
|
|
|
|
|
my ($filename) = @_; |
|
101
|
|
|
|
|
|
|
if (!-e $filename) { |
|
102
|
|
|
|
|
|
|
die "reference file '$filename' not found"; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
my @stat = stat ($filename); |
|
105
|
|
|
|
|
|
|
if (@stat == 0) { |
|
106
|
|
|
|
|
|
|
die "'stat' failed for '$filename': $@"; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
return $stat[9]; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__END__ |