/[webpac2]/trunk/bin/isi-download-results.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/isi-download-results.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1290 by dpavlin, Sat Sep 19 12:43:03 2009 UTC revision 1336 by dpavlin, Tue Jun 29 18:32:27 2010 UTC
# Line 3  Line 3 
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6  my $q = 'AD=Croatia';  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    $q = 'CA=BRATKO, D';
16    
17  my $range_size = 500;  my $range_size = 500;
18    my $overlap    = 3; # between previous and this range
19    
20  my $dump = @ARGV ? 1 : 0;  my $results = 0;
21    my $citations = 0;
22    my $cited_reference = 0; # html tables
23    my $citing_articles = 1; # as many files as cited articles
24    
25  $q = 'TS=psychology AND AD=Croatia';  my $cites_by_year = 0;
26    
27  use WWW::Mechanize;  my $max_cites = 5000; # ISI limit to get cites
28  use Data::Dump qw/dump/;  
29    $q = unac_string( 'utf-8', join(' ', @ARGV) ) if @ARGV;
30    
31    warn "QUERY: $q\n";
32    
33  our $mech = WWW::Mechanize->new(  our $mech = WWW::Mechanize->new(
34          autocheck => 1,          autocheck => 0, # it dies in reference download with it!
35          cookie_jar => undef,          cookie_jar => undef,
36  );  );
37    
38  our $step = 0;  our $step = 0;
39    our @ranges;
40    
41    my $dir = '/tmp/isi/';
42    #rmtree $dir if -e $dir;
43    mkdir $dir unless -d $dir;
44    
45  sub save_mech {  sub save_mech {
46          my ( $mech, $path ) = @_;          my $path = shift;
47          $step++;          $step++;
48          mkdir '/tmp/isi/' unless -e '/tmp/isi';          my $base_path = sprintf('%s/%04d', $dir,$step);
49          my $base_path = sprintf('/tmp/isi/%04d', $step);          $path ||= $base_path;
50          $path ||= $base_path . ( $mech->{ct} =~ m{html}i ? '.html' : '.txt' );          $path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt';
51          $mech->save_content( $path );          $mech->save_content( $path );
52          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
53          open(my $dump, '>', "$base_path.dump.txt");          open(my $dump, '>', "$base_path.dump.txt");
# Line 34  sub save_mech { Line 56  sub save_mech {
56    
57  warn "# get session";  warn "# get session";
58  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
59  save_mech $mech;  save_mech;
60    
61  sub search {  sub search {
         my $q = shift;  
   
62          warn "# advanced serach";          warn "# advanced serach";
63          $mech->follow_link( url_regex => qr/AdvancedSearch/ );          $mech->follow_link( url_regex => qr/AdvancedSearch/ );
64          save_mech $mech;          save_mech;
65    
66          warn "# cookie_jar ", dump $mech->cookie_jar;          warn "# cookie_jar ", dump $mech->cookie_jar;
67    
68            my $q_this = $q;
69    
70            if ( @ranges ) {
71                    $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
72            }
73    
74            warn "# submit_form search: $q_this\n";
75          $mech->submit_form(          $mech->submit_form(
76                  fields => {                  fields => {
77                          'value(input1)' => $q,                          'value(input1)' => $q_this,
78                  }                  },
79          );          );
80          save_mech $mech;          save_mech;
81    
82          warn "# summary";          warn "# summary";
83          $mech->follow_link( url_regex => qr/summary/ );          $mech->follow_link( url_regex => qr/summary/ );
84          save_mech $mech;          save_mech;
85  }  }
86    
87  sub get_results {  sub get_results {
88          my $q = shift;          my $desc = shift;
89          my $from = 1;          my $from = 1;
90    
91          while ( 1 ) {          while ( 1 ) {
92    
93                  my $to = $from + $range_size;                  my $to = $from + $range_size;
94    
95                    warn "# submit_form results $from - $to\n";
96    
97                  $mech->submit_form(                  $mech->submit_form(
98                          form_name => 'summary_output_form',                          form_name => 'summary_output_form',
99                          fields => {                          fields => {
# Line 83  sub get_results { Line 112  sub get_results {
112                          },                          },
113                          button => 'save',                          button => 'save',
114                  );                  );
115                  save_mech $mech;                  save_mech;
116    
117    
118                  if ( $mech->content =~ m{invalid API call} ) {                  if ( $mech->content =~ m{invalid API call} ) {
119                          $mech->back;                          $mech->back;
120                          last;                          last;
121                  }                  }
122    
123                  warn "range $from - $to [$q]\n";                  if ( $mech->content =~ m{Please wait while your request is processed} ) {
124                            warn "WARNING: processing request";
125                    }
126    
127    
128                    my $path = "/tmp/isi.$q.$from-$to";
129                    $path .= '.' . $desc if $desc;
130    
131                    warn "save $from - $to into $path\n";
132                  $mech->follow_link( url_regex => qr/save_file/ );                  $mech->follow_link( url_regex => qr/save_file/ );
133                  save_mech $mech => "/tmp/isi.$q.$from-$to.txt";                  save_mech $path;
134    
135                  $from += $range_size;                  $from += $range_size - $overlap;
136    
137                  $mech->back;                  $mech->back;
138                  $mech->back;                  $mech->back;
139                  #save_mech $mech;                  #save_mech;
   
140          }          }
   
141  }  }
142    
143    
144  sub citations {  sub citations {
         save_mech $mech;  
145          warn "# citation report";          warn "# citation report";
146          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
147          save_mech $mech;          save_mech;
148    
149          warn "view citing articles";          warn "view citing articles";
150          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
151          save_mech $mech;          save_mech;
152  }  }
153    
154  sub years {  sub years {
155          my $years_url = $mech->find_link( text_regex => qr/more options/ )->url_abs;          my $years_url = $mech->find_link( url_regex => qr/ra_name=/ );
156            if ( ! $years_url ) {
157                    warn "W: can't find ra_name link\n";
158                    return;
159            }
160            $years_url = $years_url->url_abs;
161          warn "## $years_url";          warn "## $years_url";
162          $years_url =~ s{ra_name=\w+}{ra_name=PublicationYear} || die "ra_name";          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
163                    warn "W: no ra_name in $years_url\n";
164                    return;
165            }
166          warn "# refine years (hidden by javascript)";          warn "# refine years (hidden by javascript)";
167          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";  #       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";
168          $mech->get( $years_url );          $mech->get( $years_url );
169          save_mech $mech;          save_mech;
170    
171          my $html = $mech->content;          my $html = $mech->content;
172          my @years;          my $years;
173          while ( $html =~ s{>(\d\d\d\d)\s\((\d+)\)</label.+?value="PublicationYear_}{} ) {          while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
174                  push @years, [ $1 => $2 ];                  my ( $year, $count ) = ( $1, $2 );
175                    $count =~ s{,}{}g;
176                    $years->{$year} = $count;
177          }          }
178          warn "# years ",dump @years;          warn "# years ",dump $years;
179          $mech->back;          $mech->back;
180          return @years;  
181            my @y = sort keys %$years;
182    
183            @ranges = ();
184    
185            if ( $cites_by_year ) {
186                    push @ranges, [ $_ ] foreach @y;
187                    warn "# cites_by_year ranges ", dump @ranges;
188                    return;
189            }
190    
191            my $y = shift @y;
192            my $size = $years->{$y};
193    
194            my $cites_range;
195            $cites_range = [$y] if $y;
196    
197            foreach my $y ( @y ) {
198                    if ( $size + $years->{$y} > $max_cites ) {
199                            push @ranges, $cites_range;
200                            warn "# cites_range $size years ",dump( $cites_range ),$/;
201    
202                            $cites_range = [];
203                            $size = 0;
204                    }
205                    $size += $years->{$y};
206                    push @$cites_range, $y;
207            }
208    
209            if ( $cites_range ) {
210                    push @ranges, $cites_range;
211                    warn "# cites_range $size years ",dump( $cites_range ), " FINAL\n"
212            }
213    
214            warn '# ranges ', dump @ranges;
215            @ranges = () if $#ranges == 1; # just take all
216    
217            return $years;
218    }
219    
220    if ( $results ) {
221            search;
222            years;
223            get_results;
224    }
225    
226    if ( $citations ) {
227    
228            citations;
229            years unless @ranges;
230    
231            do {
232                    my $part;
233                    if ( @ranges ) {
234                            $part .= $ranges[0]->[0] . '.';
235                            search;
236                            citations;
237                    }
238                    $part .= 'citing';
239                    get_results $part;
240            } while ( @ranges );
241    
242    }
243    
244    if ( $q =~ m{CA=(.+)} && $cited_reference ) {
245    
246            my $CA = $1;
247    
248            warn "# citated reference search";
249            $mech->follow_link( url_regex => qr/CitedReferenceSearch/ );
250            save_mech;
251    
252    
253            $mech->submit_form(
254                    form_name => 'WOS_CitedReferenceSearch_input_form',
255                    fields => {
256                            'value(input1)' => $CA,
257                    },
258            );
259    
260            my $page = 1;
261            my $records = $1 if $mech->content =~ m/(\d+)\s+records/;
262            warn "# found $records records\n";
263            my $last_span = 'fake';
264    
265            while (1) {
266                    save_mech "/tmp/isi.$q.citedref.$page";
267    
268                    $page++;
269    
270                    $mech->submit_form(
271                            form_name => 'summary_navigation',
272                            fields => {
273                                    'page' => $page,
274                            },
275                    );
276    
277                    $mech->form_name( 'summary_navigation' );
278                    last if $mech->value('page') < $page;
279    
280                    if ( $mech->content =~ m/(\d+\s*-\s*(\d+))/ ) {
281                            warn "span: $1\n";
282                            last if $2 == $records;
283                            last if $1 == $last_span;
284                            $last_span = $1;
285                    } elsif ( $page > 5 ) {
286                            warn "ARTIFICALLY LIMITED TO 5 PAGES WITHOUT VALID SPAN!";
287                            last;
288                    }
289    
290            }
291    
292  }  }
293    
294  search $q;  if ( $q =~ m{CA=(.+)} && $citing_articles ) {
295  years;  
296  get_results $q;          search;
297    
298  citations;          my $orig_q = $q;
299  years;          my $nr = 0;
300  get_results $q . '.citing';  
301            foreach my $link ( $mech->find_all_links( url_regex => qr/CitingArticles.do/ ) ) {
302                    $nr++;
303                    warn "link $nr\n";
304                    $mech->get( $link->url );
305                    save_mech;
306                    $q = $orig_q . '.citing_article.' . $nr;
307                    get_results;
308                    $mech->back;
309                    save_mech;
310                    $mech->back;
311                    save_mech;
312            }
313    
314            $q = $orig_q;
315    }
316    
317    warn "OVER\n";

Legend:
Removed from v.1290  
changed lines
  Added in v.1336

  ViewVC Help
Powered by ViewVC 1.1.26