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