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