$cites_by_year as default for faster searching
[webpac2] / bin / isi-download-results.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # Advanced search syntax:
7 # http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html
8
9 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;
25 use Data::Dump qw(dump);
26 use File::Path;
27
28 our $mech = WWW::Mechanize->new(
29         autocheck => 1,
30         cookie_jar => undef,
31 );
32
33 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 {
41         my $path = shift;
42         $step++;
43         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 );
47         warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
48         open(my $dump, '>', "$base_path.dump.txt");
49         $mech->dump_all($dump);
50 }
51
52 warn "# get session";
53 $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
54 save_mech;
55
56 sub search {
57         warn "# advanced serach";
58         $mech->follow_link( url_regex => qr/AdvancedSearch/ );
59         save_mech;
60
61         warn "# cookie_jar ", dump $mech->cookie_jar;
62
63         my $q_this = $q;
64
65         if ( @ranges ) {
66                 $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
67         }
68
69         warn "# submit_form search: $q_this\n";
70         $mech->submit_form(
71                 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 {
83         my $desc = shift;
84         my $from = 1;
85
86         while ( 1 ) {
87
88                 my $to = $from + $range_size;
89
90                 warn "# submit_form results $from - $to\n";
91
92                 $mech->submit_form(
93                         form_name => 'summary_output_form',
94                         fields => {
95                                 record_select_type => 'range',
96                                 mark_from => $from,
97                                 mark_to => $to,
98                                 mark_id => 'WOS',
99
100                                 qo_fields => 'fullrecord',
101                                 citedref => 'citedref',
102
103                                 save_options => 'plain_text',
104
105                                 fields => 'Full',
106                                 format => 'save',
107                         },
108                         button => 'save',
109                 );
110                 save_mech;
111
112
113                 if ( $mech->content =~ m{invalid API call} ) {
114                         $mech->back;
115                         last;
116                 }
117
118
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/ );
124                 save_mech $path;
125
126                 $from += $range_size - $overlap;
127
128                 $mech->back;
129                 $mech->back;
130                 #save_mech;
131         }
132 }
133
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 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         my @y = sort keys %$years;
173
174         @ranges = ();
175
176         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