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