3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
use WWW::Mechanize; |
7 |
|
use Data::Dump qw(dump); |
8 |
|
use File::Path; |
9 |
|
use Text::Unaccent; |
10 |
|
|
11 |
# Advanced search syntax: |
# Advanced search syntax: |
12 |
# http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html |
# http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html |
13 |
|
|
14 |
our $q = 'AD=Croatia'; |
our $q = 'AD=Croatia'; |
15 |
|
$q = 'CA=BRATKO, D'; |
16 |
|
|
17 |
my $range_size = 500; |
my $range_size = 500; |
18 |
my $overlap = 10; # between previous and this range |
my $overlap = 3; # between previous and this range |
19 |
|
|
20 |
$q = 'TS=psychology AND AD=Croatia' if @ARGV; # FIXME debug |
my $results = 0; |
21 |
|
my $citations = 0; |
22 |
|
my $cited_reference = 0; # html tables |
23 |
|
my $citing_articles = 1; # as many files as cited articles |
24 |
|
|
25 |
|
my $cites_by_year = 0; |
26 |
|
|
27 |
my $max_cites = 5000; # ISI limit to get cites |
my $max_cites = 5000; # ISI limit to get cites |
|
our @ranges; |
|
28 |
|
|
29 |
use WWW::Mechanize; |
$q = unac_string( 'utf-8', join(' ', @ARGV) ) if @ARGV; |
30 |
use Data::Dump qw(dump); |
|
31 |
use File::Path; |
warn "QUERY: $q\n"; |
32 |
|
|
33 |
our $mech = WWW::Mechanize->new( |
our $mech = WWW::Mechanize->new( |
34 |
autocheck => 1, |
autocheck => 0, # it dies in reference download with it! |
35 |
cookie_jar => undef, |
cookie_jar => undef, |
36 |
); |
); |
37 |
|
|
38 |
our $step = 0; |
our $step = 0; |
39 |
|
our @ranges; |
40 |
|
|
41 |
my $dir = '/tmp/isi/'; |
my $dir = '/tmp/isi/'; |
42 |
rmtree $dir if -e $dir; |
#rmtree $dir if -e $dir; |
43 |
mkdir $dir; |
mkdir $dir unless -d $dir; |
44 |
|
|
45 |
sub save_mech { |
sub save_mech { |
46 |
my $path = shift; |
my $path = shift; |
120 |
last; |
last; |
121 |
} |
} |
122 |
|
|
123 |
|
if ( $mech->content =~ m{Please wait while your request is processed} ) { |
124 |
|
warn "WARNING: processing request"; |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
my $path = "/tmp/isi.$q.$from-$to"; |
my $path = "/tmp/isi.$q.$from-$to"; |
129 |
$path .= '.' . $desc if $desc; |
$path .= '.' . $desc if $desc; |
180 |
|
|
181 |
my @y = sort keys %$years; |
my @y = sort keys %$years; |
182 |
|
|
183 |
|
@ranges = (); |
184 |
|
|
185 |
|
if ( $cites_by_year ) { |
186 |
|
push @ranges, [ $_ ] foreach @y; |
187 |
|
warn "# cites_by_year ranges ", dump @ranges; |
188 |
|
return; |
189 |
|
} |
190 |
|
|
191 |
my $y = shift @y; |
my $y = shift @y; |
192 |
my $size = $years->{$y}; |
my $size = $years->{$y}; |
193 |
|
|
|
@ranges = (); |
|
194 |
my $cites_range; |
my $cites_range; |
195 |
$cites_range = [$y] if $y; |
$cites_range = [$y] if $y; |
196 |
|
|
217 |
return $years; |
return $years; |
218 |
} |
} |
219 |
|
|
|
search; |
|
|
years; |
|
|
get_results; |
|
220 |
|
|
221 |
|
our $page = 1; |
222 |
|
sub next_page { |
223 |
|
$page++; |
224 |
|
warn "next_page $page\n"; |
225 |
|
|
226 |
citations; |
$mech->submit_form( |
227 |
|
form_name => 'summary_navigation', |
228 |
|
fields => { |
229 |
|
'page' => $page, |
230 |
|
}, |
231 |
|
); |
232 |
|
|
233 |
do { |
save_mech; |
234 |
my $part; |
|
235 |
if ( @ranges ) { |
$mech->form_name( 'summary_navigation' ); |
236 |
$part .= $ranges[0]->[0] . '.'; |
my $is_next_page = $mech->value('page') == $page; |
237 |
search; |
warn "no next_page" unless $is_next_page; |
238 |
|
return $is_next_page; |
239 |
|
} |
240 |
|
|
241 |
|
if ( $results ) { |
242 |
|
search; |
243 |
|
years; |
244 |
|
get_results; |
245 |
|
} |
246 |
|
|
247 |
|
if ( $citations ) { |
248 |
|
|
249 |
|
citations; |
250 |
|
years unless @ranges; |
251 |
|
|
252 |
|
do { |
253 |
|
my $part; |
254 |
|
if ( @ranges ) { |
255 |
|
$part .= $ranges[0]->[0] . '.'; |
256 |
|
search; |
257 |
|
citations; |
258 |
|
} |
259 |
|
$part .= 'citing'; |
260 |
|
get_results $part; |
261 |
|
} while ( @ranges ); |
262 |
|
|
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
if ( $q =~ m{CA=(.+)} && $cited_reference ) { |
268 |
|
|
269 |
|
my $CA = $1; |
270 |
|
|
271 |
|
warn "# citated reference search"; |
272 |
|
$mech->follow_link( url_regex => qr/CitedReferenceSearch/ ); |
273 |
|
save_mech; |
274 |
|
|
275 |
|
|
276 |
|
$mech->submit_form( |
277 |
|
form_name => 'WOS_CitedReferenceSearch_input_form', |
278 |
|
fields => { |
279 |
|
'value(input1)' => $CA, |
280 |
|
}, |
281 |
|
); |
282 |
|
|
283 |
|
my $page = 1; |
284 |
|
my $records = $1 if $mech->content =~ m/(\d+)\s+records/; |
285 |
|
warn "# found $records records\n"; |
286 |
|
my $last_span = 'fake'; |
287 |
|
|
288 |
|
while (1) { |
289 |
|
save_mech "/tmp/isi.$q.citedref.$page"; |
290 |
|
|
291 |
|
last unless next_page(); |
292 |
|
|
293 |
|
if ( $mech->content =~ m/(\d+\s*-\s*(\d+))/ ) { |
294 |
|
warn "span: $1\n"; |
295 |
|
last if $2 == $records; |
296 |
|
last if $1 == $last_span; |
297 |
|
$last_span = $1; |
298 |
|
} elsif ( $page > 5 ) { |
299 |
|
warn "ARTIFICALLY LIMITED TO 5 PAGES WITHOUT VALID SPAN!"; |
300 |
|
last; |
301 |
|
} |
302 |
|
|
303 |
|
} |
304 |
|
|
305 |
|
} |
306 |
|
|
307 |
|
if ( $q =~ m{CA=(.+)} && $citing_articles ) { |
308 |
|
|
309 |
|
search; |
310 |
|
|
311 |
|
my $orig_q = $q; |
312 |
|
my $nr = 0; |
313 |
|
|
314 |
|
do { |
315 |
|
|
316 |
|
foreach my $link ( $mech->find_all_links( url_regex => qr/CitingArticles.do/ ) ) { |
317 |
|
$nr++; |
318 |
|
warn "link $nr\n"; |
319 |
|
$mech->get( $link->url ); |
320 |
|
save_mech; |
321 |
|
$q = $orig_q . '.citing_article.' . $nr; |
322 |
|
get_results; |
323 |
|
$mech->back; |
324 |
|
$mech->back; |
325 |
|
|
326 |
|
#last if $nr > 3; # FIXME only for development |
327 |
} |
} |
|
$part .= 'citing'; |
|
|
get_results $part; |
|
|
} while ( @ranges ); |
|
328 |
|
|
329 |
|
} while next_page; |
330 |
|
|
331 |
|
$q = $orig_q; |
332 |
|
} |
333 |
|
|
334 |
|
warn "OVER\n"; |