3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
my $q = 'AD=Croatia'; |
# Advanced search syntax: |
7 |
my $range_size = 500; |
# http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html |
|
|
|
|
my $dump = @ARGV ? 1 : 0; |
|
8 |
|
|
9 |
$q = 'TS=psychology AND AD=Croatia'; |
our $q = 'AD=Croatia'; |
10 |
|
my $range_size = 500; |
11 |
|
my $overlap = 3; # between previous and this range |
12 |
|
my $skip_results = 1; |
13 |
|
my $cites_by_year = 1; |
14 |
|
|
15 |
|
my $max_cites = 5000; # ISI limit to get cites |
16 |
|
|
17 |
|
if ( 0 ) { |
18 |
|
$q = 'TS=psychology AND AD=Croatia'; |
19 |
|
$range_size = 50; |
20 |
|
$overlap = 0; |
21 |
|
$max_cites = 50; |
22 |
|
} |
23 |
|
|
24 |
use WWW::Mechanize; |
use WWW::Mechanize; |
25 |
use Data::Dump qw/dump/; |
use Data::Dump qw(dump); |
26 |
|
use File::Path; |
27 |
|
|
28 |
our $mech = WWW::Mechanize->new( |
our $mech = WWW::Mechanize->new( |
29 |
autocheck => 1, |
autocheck => 1, |
31 |
); |
); |
32 |
|
|
33 |
our $step = 0; |
our $step = 0; |
34 |
|
our @ranges; |
35 |
|
|
36 |
|
my $dir = '/tmp/isi/'; |
37 |
|
rmtree $dir if -e $dir; |
38 |
|
mkdir $dir; |
39 |
|
|
40 |
sub save_mech { |
sub save_mech { |
41 |
my ( $mech, $path ) = @_; |
my $path = shift; |
42 |
$step++; |
$step++; |
43 |
$path ||= sprintf('/tmp/isi.%02d.%s', $step, $mech->{ct} =~ m{html}i ? 'html' : 'txt' ); |
my $base_path = sprintf('%s/%04d', $dir,$step); |
44 |
|
$path ||= $base_path; |
45 |
|
$path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt'; |
46 |
$mech->save_content( $path ); |
$mech->save_content( $path ); |
47 |
warn "# [$step] $path ", -s $path, " ", $mech->ct; |
warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n"; |
48 |
$mech->dump_all if $dump; |
open(my $dump, '>', "$base_path.dump.txt"); |
49 |
|
$mech->dump_all($dump); |
50 |
} |
} |
51 |
|
|
52 |
warn "# get session"; |
warn "# get session"; |
53 |
$mech->get( 'http://isiknowledge.com/?DestApp=WOS' ); |
$mech->get( 'http://isiknowledge.com/?DestApp=WOS' ); |
54 |
save_mech $mech; |
save_mech; |
55 |
|
|
56 |
warn "# advanced serach"; |
sub search { |
57 |
$mech->follow_link( url_regex => qr/AdvancedSearch/ ); |
warn "# advanced serach"; |
58 |
save_mech $mech; |
$mech->follow_link( url_regex => qr/AdvancedSearch/ ); |
59 |
|
save_mech; |
60 |
|
|
61 |
warn "# cookie_jar ", dump $mech->cookie_jar; |
warn "# cookie_jar ", dump $mech->cookie_jar; |
62 |
|
|
63 |
$mech->submit_form( |
my $q_this = $q; |
64 |
fields => { |
|
65 |
'value(input1)' => $q, |
if ( @ranges ) { |
66 |
|
$q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')'; |
67 |
} |
} |
|
); |
|
|
save_mech $mech; |
|
68 |
|
|
69 |
warn "# summary"; |
warn "# submit_form search: $q_this\n"; |
70 |
$mech->follow_link( url_regex => qr/summary/ ); |
$mech->submit_form( |
71 |
save_mech $mech; |
fields => { |
72 |
|
'value(input1)' => $q_this, |
73 |
|
}, |
74 |
|
); |
75 |
|
save_mech; |
76 |
|
|
77 |
|
warn "# summary"; |
78 |
|
$mech->follow_link( url_regex => qr/summary/ ); |
79 |
|
save_mech; |
80 |
|
} |
81 |
|
|
82 |
sub get_results { |
sub get_results { |
83 |
my $q = shift; |
my $desc = shift; |
84 |
my $from = 1; |
my $from = 1; |
85 |
|
|
86 |
while ( 1 ) { |
while ( 1 ) { |
87 |
|
|
88 |
my $to = $from + $range_size; |
my $to = $from + $range_size; |
89 |
|
|
90 |
|
warn "# submit_form results $from - $to\n"; |
91 |
|
|
92 |
$mech->submit_form( |
$mech->submit_form( |
93 |
form_name => 'summary_output_form', |
form_name => 'summary_output_form', |
94 |
fields => { |
fields => { |
107 |
}, |
}, |
108 |
button => 'save', |
button => 'save', |
109 |
); |
); |
110 |
save_mech $mech; |
save_mech; |
111 |
|
|
112 |
|
|
113 |
if ( $mech->content =~ m{invalid API call} ) { |
if ( $mech->content =~ m{invalid API call} ) { |
114 |
$mech->back; |
$mech->back; |
115 |
return; |
last; |
116 |
} |
} |
117 |
|
|
118 |
warn "# save_file $from - $to [$q]"; |
|
119 |
|
my $path = "/tmp/isi.$q.$from-$to"; |
120 |
|
$path .= '.' . $desc if $desc; |
121 |
|
|
122 |
|
warn "save $from - $to into $path\n"; |
123 |
$mech->follow_link( url_regex => qr/save_file/ ); |
$mech->follow_link( url_regex => qr/save_file/ ); |
124 |
save_mech $mech => "/tmp/isi.$q.$from-$to.txt"; |
save_mech $path; |
125 |
|
|
126 |
$from += $range_size; |
$from += $range_size - $overlap; |
127 |
|
|
128 |
$mech->back; |
$mech->back; |
129 |
$mech->back; |
$mech->back; |
130 |
#save_mech $mech; |
#save_mech; |
131 |
|
} |
132 |
|
} |
133 |
|
|
|
} # while |
|
134 |
|
|
135 |
|
sub citations { |
136 |
|
warn "# citation report"; |
137 |
|
$mech->follow_link( url_regex => qr/search_mode=CitationReport/ ); |
138 |
|
save_mech; |
139 |
|
|
140 |
|
warn "view citing articles"; |
141 |
|
$mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ ); |
142 |
|
save_mech; |
143 |
} |
} |
144 |
|
|
145 |
get_results $q; |
sub years { |
146 |
|
my $years_url = $mech->find_link( url_regex => qr/ra_name=/ ); |
147 |
|
if ( ! $years_url ) { |
148 |
|
warn "W: can't find ra_name link\n"; |
149 |
|
return; |
150 |
|
} |
151 |
|
$years_url = $years_url->url_abs; |
152 |
|
warn "## $years_url"; |
153 |
|
if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) { |
154 |
|
warn "W: no ra_name in $years_url\n"; |
155 |
|
return; |
156 |
|
} |
157 |
|
warn "# refine years (hidden by javascript)"; |
158 |
|
# warn "http://apps.isiknowledge.com/RAMore.do?product=WOS&search_mode=TotalCitingArticles&SID=T1o6bChdN9PGP1LN1Nh&qid=3&ra_mode=more&ra_name=PublicationYear&db_id=WOS&viewType=raMore\n$years_url\n"; |
159 |
|
$mech->get( $years_url ); |
160 |
|
save_mech; |
161 |
|
|
162 |
|
my $html = $mech->content; |
163 |
|
my $years; |
164 |
|
while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) { |
165 |
|
my ( $year, $count ) = ( $1, $2 ); |
166 |
|
$count =~ s{,}{}g; |
167 |
|
$years->{$year} = $count; |
168 |
|
} |
169 |
|
warn "# years ",dump $years; |
170 |
|
$mech->back; |
171 |
|
|
172 |
save_mech $mech; |
my @y = sort keys %$years; |
|
warn "# citations"; |
|
|
$mech->follow_link( url_regex => qr/search_mode=CitationReport/ ); |
|
|
save_mech $mech; |
|
173 |
|
|
174 |
$mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ ); |
@ranges = (); |
|
save_mech $mech; |
|
175 |
|
|
176 |
get_results $q . '.citing'; |
if ( $cites_by_year ) { |
177 |
|
push @ranges, [ $_ ] foreach @y; |
178 |
|
warn "# cites_by_year ranges ", dump @ranges; |
179 |
|
return; |
180 |
|
} |
181 |
|
|
182 |
|
my $y = shift @y; |
183 |
|
my $size = $years->{$y}; |
184 |
|
|
185 |
|
my $cites_range; |
186 |
|
$cites_range = [$y] if $y; |
187 |
|
|
188 |
|
foreach my $y ( @y ) { |
189 |
|
if ( $size + $years->{$y} > $max_cites ) { |
190 |
|
push @ranges, $cites_range; |
191 |
|
warn "# cites_range $size years ",dump( $cites_range ),$/; |
192 |
|
|
193 |
|
$cites_range = []; |
194 |
|
$size = 0; |
195 |
|
} |
196 |
|
$size += $years->{$y}; |
197 |
|
push @$cites_range, $y; |
198 |
|
} |
199 |
|
|
200 |
|
if ( $cites_range ) { |
201 |
|
push @ranges, $cites_range; |
202 |
|
warn "# cites_range $size years ",dump( $cites_range ), " FINAL\n" |
203 |
|
} |
204 |
|
|
205 |
|
warn '# ranges ', dump @ranges; |
206 |
|
@ranges = () if $#ranges == 1; # just take all |
207 |
|
|
208 |
|
return $years; |
209 |
|
} |
210 |
|
|
211 |
|
search; |
212 |
|
years; |
213 |
|
get_results unless $skip_results; |
214 |
|
|
215 |
|
|
216 |
|
citations; |
217 |
|
|
218 |
|
do { |
219 |
|
my $part; |
220 |
|
if ( @ranges ) { |
221 |
|
$part .= $ranges[0]->[0] . '.'; |
222 |
|
search; |
223 |
|
citations; |
224 |
|
} |
225 |
|
$part .= 'citing'; |
226 |
|
get_results $part; |
227 |
|
} while ( @ranges ); |
228 |
|
|