-
Notifications
You must be signed in to change notification settings - Fork 0
/
inescrape
executable file
·221 lines (204 loc) · 5.04 KB
/
inescrape
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl
use Tree::MultiNode;
use WWW::Mechanize;
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use Storable qw(nstore retrieve);
use Getopt::Long;
use Data::Dump qw(pp);
use JSON::XS;
use File::Slurp;
# This file is part of INE4all.
#
# INE4all is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# INE4all is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with INE4all. If not, see <http://www.gnu.org/licenses/>.
#
# Find more information about INE4all in
# http://DatosEnCrudo.org/abredatos/
my %opt;
GetOptions(
\%opt,
'retrieve=s',
'store=s',
'incremental-store=s',
);
my $mech = WWW::Mechanize->new();
my $tree = new Tree::MultiNode;
my $handle = new Tree::MultiNode::Handle($tree);
if(my $file = $opt{'retrieve'}) {
$tree = retrieve($file);
$handle = new Tree::MultiNode::Handle($tree);
} else {
my @LEVEL=qw(diezcarne diez diezboldine N2);
my %LEVEL;
for(my $i=0;$i<@LEVEL;++$i) {
$LEVEL_FOR{$LEVEL[$i]}=$i;
}
mkdir('html');
mkdir('px');
die("Can't create cache dirs") unless (-d 'html' && -d 'px');
$mech->get('http://www.ine.es/inebmenu/indice.htm');
my $html = $mech->content();
my($table,@data);
$html=~m,(<table summary="Tabla con la lista completa.*?</table>),igs;
$table=$1 || die("Did not get expected content");
my @td = $table=~m,(<td.*?</td>),igs;
my(@level,$first,$n);
unshift @level, $LEVEL[0];
foreach my $td (@td) {
++$n;
print "\n";
my($class,$href,$name);
$td=~s,[\n\r],,igs;
$td=~s,\s+, ,igs;
$class=$1 if($td=~m,<td class="(.*?)",i);
if($td=~m,<a href="(.*?)">(.*?)</a>,igs) {
$href=$1,
$name=$2;
}
if(!$name) {
if($td=~m,<td.*?>(.*?)</td>,igs) {
$name=$1;
$name=~s,(<.*?>),,igs;
}
}
print join(":",$LEVEL_FOR{$class},$class,$href,$name),"\n";
print join("<-",@level),"\n";
my $head = $level[0];
$handle->last();
if($head eq $class) {
# leave current untouched
} else {
if($LEVEL_FOR{$class}<$LEVEL_FOR{$head}) {
if(@level) {
do {
shift @level;
$handle->up();
print "Level up\n";
} until(@level && ($LEVEL_FOR{$level[0]}<=$LEVEL_FOR{$class}));
}
} else {
print "Level down\n";
$handle->down();
unshift @level, $class;
}
}
print join("<-",@level),"\n";
my $id;
if(!$href) {
$id = md5_hex($name);
} else {
if($href=~m,path=(.*?)&,) {
$id = uri_unescape($1);
$id=~s,^/,,g;
$id=~s,/,-,g;
} else {
$id = md5_hex($href);
}
}
my $datasets = {};
if($href=~m,type=pcaxis,) {
get_pcaxis($href,$datasets,2);
}
my $data = {'name'=>$name,'href'=>$href,'datasets'=>$datasets };
$handle->add_child($id,$data);
if(scalar keys %{$datasets}) {
foreach my $id (keys %{$datasets}) {
$handle->add_child($id,{name=>$datasets->{$id}});
}
}
# Incremental store
if(my $file = $opt{'incremental-store'}) {
nstore($tree,$file);
}
}
# print Dumper($tree);
$handle->top();
if(my $file = $opt{'store'}) {
nstore($tree,$file);
}
}
my $jdata = {'data'=>'INEbase',attributes=>{'id'=>'ROOT'},'children'=>[]};
$handle->top();
build_json($handle,$jdata);
pp($jdata);
my $json = JSON::XS->new();
write_file('index.json',$json->encode($jdata));
sub build_json {
my($handle,$data,$path)=@_;
$path||=[];
my $ref = $handle->get_value();
my $id = $handle->get_key();
print "Recursed into $id\n";
$data->{attributes} = {
'id' => $id,
'path' => $path,
};
$data->{data} = $ref->{name};
for(my $i=0; $i<scalar($handle->children()); ++$i) {
$handle->down($i);
$cdata = {};
push @{$data->{children}}, $cdata;
build_json($handle,$cdata,[@{$path},$id]);
pp($data);
$handle->up();
}
}
exit;
sub get_pcaxis {
my($url,$info,$maxdepth)=@_;
print "Trying to get ". ref($url) eq 'WWW::Mechanize::Link' ? $url->url() : $url, "\n";
$mech->get($url);
my $content = $mech->content();
my $urlfile = $url;
$urlfile=~s,/,-,igs;
$mech->save_content("html/$urlfile");
foreach my $link (
$mech->find_all_links(url_regex=>qr/\d+.px$/)
) {
my $px = $link->url();
my $pxfile = $px;
$pxfile=~s,^(.*/),,;
my $file = $px;
$file=~s,/+,/,igs;
$file=~s,^/,,igs;
$file=~s,/,-,igs;
$file=~s,^pcaxisdl-,,igs;
$file=~s,\.px$,,igs;
my $name = $file;
if($content=~m,<a.*?href=".*?file=$pxfile&.*?".*?>(.*?)</a>,igs) {
$name = $1;
$name=~s,[\n\r],,igs;
$name=~s,\s+, ,igs;
$name=~s,^\s+,,igs;
$name=~s,\s+$,,igs;
}
print "Got name $name\n";
$info->{$file} = $name;
if(-f "px/$file") {
print "$file already exists";
} else {
print "Getting $px -> $file\n";
$mech->get($px);
$mech->save_content("px/$file");
}
}
if($maxdepth>0) {
foreach my $link (
$mech->find_all_links(url_regex=>qr/type=pcaxis/i)
) {
get_pcaxis($link,$info,$maxdepth-1);
}
}
}